Skip to content

Instantly share code, notes, and snippets.

@viluon
Last active August 21, 2016 20:08
Show Gist options
  • Save viluon/a5a56036989620e63a7ab7690d6c68a7 to your computer and use it in GitHub Desktop.
Save viluon/a5a56036989620e63a7ab7690d6c68a7 to your computer and use it in GitHub Desktop.
A way of encoding ComputerCraft terminal colours inside strings.
-- Made by viluon, copyright 2016 under the MIT License (https://opensource.org/licenses/MIT)
local str = [[
@t5Hello@ta World!
@tf@b0How are you people?
I am viluon;@b1@t0 <@tdviluon@t0\@@tbespiv.net@t0>]]
local pos, escape = 1
local set_fg = term.setTextColour
local set_bg = term.setBackgroundColour
while pos <= #str do
local char = str:sub( pos, pos )
if not escape and char == "@" then
pos = pos + 1
local command = str:sub( pos, pos )
pos = pos + 1
local colour = 2 ^ tonumber( str:sub( pos, pos ), 16 )
if command == "t" then
set_fg( colour )
elseif command == "b" then
set_bg( colour )
end
elseif not escape and char == "\\" then
escape = true
else
write( char )
escape = false
end
pos = pos + 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment