A treat for Linux users ;)
[prosody.git] / util / termcolours.lua
1 local t_concat, t_insert = table.concat, table.insert;
2 local char, format = string.char, string.format;
3 local ipairs = ipairs;
4 module "termcolours"
5
6 local stylemap = {
7                         reset = 0; bright = 1, dim = 2, underscore = 4, blink = 5, reverse = 7, hidden = 8;
8                         black = 30; red = 31; green = 32; yellow = 33; blue = 34; magenta = 35; cyan = 36; white = 37;
9                         ["black background"] = 40; ["red background"] = 41; ["green background"] = 42; ["yellow background"] = 43; ["blue background"] = 44; ["magenta background"] = 45; ["cyan background"] = 46; ["white background"] = 47;
10                         bold = 1, dark = 2, underline = 4, underlined = 4, normal = 0;
11                 }
12
13 local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m";
14 function getstring(style, text)
15         if style then
16                 return format(fmt_string, style, text);
17         else
18                 return text;
19         end
20 end
21
22 function getstyle(...)
23         local styles, result = { ... }, {};
24         for i, style in ipairs(styles) do
25                 style = stylemap[style];
26                 if style then
27                         t_insert(result, style);
28                 end
29         end
30         return t_concat(result, ";");
31 end
32
33 return _M;