2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
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;
17 if os.getenv("WINDIR") then
18 windows = require "util.windows";
20 local orig_color = windows and windows.get_consolecolor and windows.get_consolecolor();
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;
32 ["0"] = orig_color, -- reset
34 ["1;33"] = 2+4+8, -- bold yellow
35 ["1;31"] = 4+8 -- bold red
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";
47 local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m";
48 local function getstring(style, text)
50 return format(fmt_string, style, text);
56 local function getstyle(...)
57 local styles, result = { ... }, {};
58 for i, style in ipairs(styles) do
59 style = stylemap[style];
61 t_insert(result, style);
64 return t_concat(result, ";");
68 local function setstyle(style)
71 io_write("\27["..style.."m");
77 function setstyle(style)
80 windows.set_consolecolor(winstylemap[style] or orig_color);
84 if not orig_color then
85 function setstyle(style) end
89 local function ansi2css(ansi_codes)
90 if ansi_codes == "0" then return "</span>"; end
92 for code in ansi_codes:gmatch("[^;]+") do
93 t_insert(css, cssmap[tonumber(code)]);
95 return "</span><span style='"..t_concat(css, ";").."'>";
98 local function tohtml(input)
99 return input:gsub("\027%[(.-)m", ansi2css);
103 getstring = getstring;