Skip to content

Instantly share code, notes, and snippets.

@floringogianu
Created November 29, 2016 15:25
Show Gist options
  • Save floringogianu/28d31ca1c0151398b10fa00d0f5a8a9b to your computer and use it in GitHub Desktop.
Save floringogianu/28d31ca1c0151398b10fa00d0f5a8a9b to your computer and use it in GitHub Desktop.
Colored strings in Lua
-- credit to Jesse Paroz (@jparoz)
local FG = {
black = 30,
red = 31,
green = 32,
yellow = 33,
blue = 34,
magenta = 35,
cyan = 36,
white = 37,
brblack = 90,
brred = 91,
brgreen = 92,
bryellow = 93,
brblue = 94,
brmagenta = 95,
brcyan = 96,
brwhite = 97,
}
local BG = {
black = 40,
red = 41,
green = 42,
yellow = 43,
blue = 44,
magenta = 45,
cyan = 46,
white = 47,
brblack = 100,
brred = 101,
brgreen = 102,
bryellow = 103,
brblue = 104,
brmagenta = 105,
brcyan = 106,
brwhite = 107,
}
local function escape(n)
return string.char(27)..'['..tostring(n)..'m'
end
string.color = function(s, f, b, bold, underline, swap)
local fg = FG[f] or 39
local bg = BG[b] or 49
s = escape(fg) .. escape(bg) .. s
if bold then s = escape(1) .. s end
if underline then s = escape(4) .. s end
if swap then s = escape(7) .. s end
s = s .. escape(0)
return s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment