Skip to content

Instantly share code, notes, and snippets.

@CoderPuppy
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save CoderPuppy/9906482 to your computer and use it in GitHub Desktop.

Select an option

Save CoderPuppy/9906482 to your computer and use it in GitHub Desktop.
ComputerCraft Text Escapes API
-- Text Escapes API by CoderPuppy
-- Usage:
-- textEscapes.print(textEscapes.textLime .. "Hello World!" .. textEscapes.textWhite)
-- Prints "Hello World!" in lime
-- textEscapes.print(textEscapes.textGrey .. textEscapes.backYellow .. "HI!!!" .. textEscapes.reset)
-- Prints "HI!!!" in grey with a yellow background
defaultOpts = {
textColor = true;
backColor = true;
moveCursor = true;
scroll = true;
blink = true;
clear = true;
clearLine = true;
}
function write(text, _opts)
local opts = {}
for k, v in pairs(defaultOpts) do
opts[k] = v
end
if type(_opts) == "table" then
for k, v in pairs(_opts) do
opts[k] = v
end
end
text = tostring(text)
local offset = text:find(":[", 1, true)
if offset == nil then
offset = #text + 1
end
_G.write(text:sub(1, offset - 1))
for code in text:gmatch(":%[([^%]]+)%]") do
offset = offset + 2 + #code + 1
local match
match = {code:match("^tc([0-9a-f])")}
if opts.textColor and #match > 0 then
local n = match[1]:byte()
if n >= 97 then
-- "a":byte() = 97
-- a = 10
n = n - 97 + 10
else
-- "0":byte() = 48
n = n - 48
end
term.setTextColor(math.pow(2, n))
end
match = {code:match("^bc([0-9a-f])$")}
if opts.backColor and #match > 0 then
local n = match[1]:byte()
if n >= 97 then
-- "a":byte() = 97
-- a = 10
n = n - 97 + 10
else
-- "0":byte() = 48
n = n - 48
end
term.setBackgroundColor(math.pow(2, n))
end
match = {code:match("^mc(%d+),(%d+)$")}
if opts.moveCursor and #match > 0 then
local x = tonumber(match[1])
local y = tonumber(match[2])
term.setCursorPos(x, y)
end
if opts.blink and code == "bn" then
term.setCursorBlink(true)
end
if opts.blink and code == "bf" then
term.setCursorBlink(false)
end
if opts.clear and code == "c" then
term.clear()
end
if opts.clearLine and code == "cl" then
term.clearLine()
end
match = {code:match("^s([%d%-]+)$")}
if opts.scroll and #match > 0 then
term.scroll(tonumber(match[1]))
end
local nextEscape = text:find(":[", offset, true)
if nextEscape == nil then
nextEscape = #text + 1
end
_G.write(text:sub(offset, nextEscape - 1))
offset = nextEscape
end
_G.write(text:sub(offset))
end
function print(text, opts)
write(tostring(text) .. "\n", opts)
end
function hexColor(color)
local n = math.log(color) / math.log(2)
if n >= 10 then
n = string.char(97 + n - 10)
end
return tostring(n)
end
function textColor(color)
return ":[tc" .. hexColor(color) .. "]"
end
function backColor(color)
return ":[bc" .. hexColor(color) .. "]"
end
function moveCursor(x, y)
return ":[mc" .. tostring(x) .. "," .. tostring(y) .. "]"
end
function scroll(n)
return ":[s" .. tostring(n) .. "]"
end
local env = getfenv()
for name, v in pairs(colors) do
if type(v) == "number" then
env["text" .. name:sub(1, 1):upper() .. name:sub(2)] = textColor(v)
env["back" .. name:sub(1, 1):upper() .. name:sub(2)] = backColor(v)
end
end
blinkOn = ":[bn]"
blinkOff = ":[bf]"
clear = ":[c]"
clearLine = ":[cl]"
reset = textWhite .. backBlack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment