Comparison

util/termcolours.lua @ 4698:739f7ae1a01e

util.termcolours: tohtml() for converting output to HTML. I don't know.
author Matthew Wild <mwild1@gmail.com>
date Thu, 26 Apr 2012 05:57:44 +0100
parent 3749:588c09d7903c
child 5776:bd0ff8ae98a8
comparison
equal deleted inserted replaced
4697:778eb9405a98 4698:739f7ae1a01e
7 -- 7 --
8 8
9 9
10 local t_concat, t_insert = table.concat, table.insert; 10 local t_concat, t_insert = table.concat, table.insert;
11 local char, format = string.char, string.format; 11 local char, format = string.char, string.format;
12 local tonumber = tonumber;
12 local ipairs = ipairs; 13 local ipairs = ipairs;
13 local io_write = io.write; 14 local io_write = io.write;
14 15
15 local windows; 16 local windows;
16 if os.getenv("WINDIR") then 17 if os.getenv("WINDIR") then
31 ["0"] = orig_color, -- reset 32 ["0"] = orig_color, -- reset
32 ["1"] = 7+8, -- bold 33 ["1"] = 7+8, -- bold
33 ["1;33"] = 2+4+8, -- bold yellow 34 ["1;33"] = 2+4+8, -- bold yellow
34 ["1;31"] = 4+8 -- bold red 35 ["1;31"] = 4+8 -- bold red
35 } 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 };
36 46
37 local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m"; 47 local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m";
38 function getstring(style, text) 48 function getstring(style, text)
39 if style then 49 if style then
40 return format(fmt_string, style, text); 50 return format(fmt_string, style, text);
74 if not orig_color then 84 if not orig_color then
75 function setstyle(style) end 85 function setstyle(style) end
76 end 86 end
77 end 87 end
78 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 function tohtml(input)
99 return input:gsub("\027%[(.-)m", ansi2css);
100 end
101
79 return _M; 102 return _M;