Merge 0.9->0.10
[prosody.git] / util / termcolours.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local t_concat, t_insert = table.concat, table.insert;
11 local char, format = string.char, string.format;
12 local tonumber = tonumber;
13 local ipairs = ipairs;
14 local io_write = io.write;
15
16 local windows;
17 if os.getenv("WINDIR") then
18         windows = require "util.windows";
19 end
20 local orig_color = windows and windows.get_consolecolor and windows.get_consolecolor();
21
22 local _ENV = nil;
23
24 local stylemap = {
25                         reset = 0; bright = 1, dim = 2, underscore = 4, blink = 5, reverse = 7, hidden = 8;
26                         black = 30; red = 31; green = 32; yellow = 33; blue = 34; magenta = 35; cyan = 36; white = 37;
27                         ["black background"] = 40; ["red background"] = 41; ["green background"] = 42; ["yellow background"] = 43; ["blue background"] = 44; ["magenta background"] = 45; ["cyan background"] = 46; ["white background"] = 47;
28                         bold = 1, dark = 2, underline = 4, underlined = 4, normal = 0;
29                 }
30
31 local winstylemap = {
32         ["0"] = orig_color, -- reset
33         ["1"] = 7+8, -- bold
34         ["1;33"] = 2+4+8, -- bold yellow
35         ["1;31"] = 4+8 -- bold red
36 }
37
38 local cssmap = {
39         [1] = "font-weight: bold", [2] = "opacity: 0.5", [4] = "text-decoration: underline", [8] = "visibility: hidden",
40         [30] = "color:black", [31] = "color:red", [32]="color:green", [33]="color:#FFD700",
41         [34] = "color:blue", [35] = "color: magenta", [36] = "color:cyan", [37] = "color: white",
42         [40] = "background-color:black", [41] = "background-color:red", [42]="background-color:green",
43         [43]="background-color:yellow", [44] = "background-color:blue", [45] = "background-color: magenta",
44         [46] = "background-color:cyan", [47] = "background-color: white";
45 };
46
47 local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m";
48 local function getstring(style, text)
49         if style then
50                 return format(fmt_string, style, text);
51         else
52                 return text;
53         end
54 end
55
56 local function getstyle(...)
57         local styles, result = { ... }, {};
58         for i, style in ipairs(styles) do
59                 style = stylemap[style];
60                 if style then
61                         t_insert(result, style);
62                 end
63         end
64         return t_concat(result, ";");
65 end
66
67 local last = "0";
68 local function setstyle(style)
69         style = style or "0";
70         if style ~= last then
71                 io_write("\27["..style.."m");
72                 last = style;
73         end
74 end
75
76 if windows then
77         function setstyle(style)
78                 style = style or "0";
79                 if style ~= last then
80                         windows.set_consolecolor(winstylemap[style] or orig_color);
81                         last = style;
82                 end
83         end
84         if not orig_color then
85                 function setstyle(style) end
86         end
87 end
88
89 local function ansi2css(ansi_codes)
90         if ansi_codes == "0" then return "</span>"; end
91         local css = {};
92         for code in ansi_codes:gmatch("[^;]+") do
93                 t_insert(css, cssmap[tonumber(code)]);
94         end
95         return "</span><span style='"..t_concat(css, ";").."'>";
96 end
97
98 local function tohtml(input)
99         return input:gsub("\027%[(.-)m", ansi2css);
100 end
101
102 return {
103         getstring = getstring;
104         getstyle = getstyle;
105         setstyle = setstyle;
106         tohtml = tohtml;
107 };