aboutsummaryrefslogtreecommitdiffstats
path: root/util/termcolours.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2016-02-27 16:47:12 +0100
committerKim Alvefur <zash@zash.se>2016-02-27 16:47:12 +0100
commit6fc40230c33b28576f30fa7681eeac33ac013edf (patch)
tree58194f7da60e96eb70dd7958a532267f1791a72b /util/termcolours.lua
parentf828dae360cb528ce67ff83ce9bf4476252b2643 (diff)
downloadprosody-6fc40230c33b28576f30fa7681eeac33ac013edf.tar.gz
prosody-6fc40230c33b28576f30fa7681eeac33ac013edf.zip
util.termcolours: Add 256 color support
Diffstat (limited to 'util/termcolours.lua')
-rw-r--r--util/termcolours.lua25
1 files changed, 25 insertions, 0 deletions
diff --git a/util/termcolours.lua b/util/termcolours.lua
index 279ff601..5d06fd35 100644
--- a/util/termcolours.lua
+++ b/util/termcolours.lua
@@ -14,6 +14,7 @@ local char, format = string.char, string.format;
local tonumber = tonumber;
local ipairs = ipairs;
local io_write = io.write;
+local m_floor = math.floor;
local windows;
if os.getenv("WINDIR") then
@@ -55,6 +56,30 @@ local function getstring(style, text)
end
end
+local function gray(n)
+ return m_floor(n*3/32)+0xe8;
+end
+local function color(r,g,b)
+ if r == g and g == b then
+ return gray(r);
+ end
+ r = m_floor(r*3/128);
+ g = m_floor(g*3/128);
+ b = m_floor(b*3/128);
+ return 0x10 + ( r * 36 ) + ( g * 6 ) + ( b );
+end
+local function hex2rgb(hex)
+ local r = tonumber(hex:sub(1,2),16);
+ local g = tonumber(hex:sub(3,4),16);
+ local b = tonumber(hex:sub(5,6),16);
+ return r,g,b;
+end
+
+setmetatable(stylemap, { __index = function(_, style)
+ local g = style:sub(7) == " background" and "48;5;" or "38;5;";
+ return g .. color(hex2rgb(style));
+end } );
+
local function getstyle(...)
local styles, result = { ... }, {};
for i, style in ipairs(styles) do