util/termcolours.lua
changeset 6780 5de6b93d0190
parent 5776 bd0ff8ae98a8
child 7201 48d167f652ad
equal deleted inserted replaced
6777:3965662ae091 6780:5de6b93d0190
    17 if os.getenv("WINDIR") then
    17 if os.getenv("WINDIR") then
    18 	windows = require "util.windows";
    18 	windows = require "util.windows";
    19 end
    19 end
    20 local orig_color = windows and windows.get_consolecolor and windows.get_consolecolor();
    20 local orig_color = windows and windows.get_consolecolor and windows.get_consolecolor();
    21 
    21 
    22 module "termcolours"
    22 local _ENV = nil;
    23 
    23 
    24 local stylemap = {
    24 local stylemap = {
    25 			reset = 0; bright = 1, dim = 2, underscore = 4, blink = 5, reverse = 7, hidden = 8;
    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;
    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;
    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;
    43 	[43]="background-color:yellow",	[44] = "background-color:blue", [45] = "background-color: magenta",
    43 	[43]="background-color:yellow",	[44] = "background-color:blue", [45] = "background-color: magenta",
    44 	[46] = "background-color:cyan", [47] = "background-color: white";
    44 	[46] = "background-color:cyan", [47] = "background-color: white";
    45 };
    45 };
    46 
    46 
    47 local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m";
    47 local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m";
    48 function getstring(style, text)
    48 local function getstring(style, text)
    49 	if style then
    49 	if style then
    50 		return format(fmt_string, style, text);
    50 		return format(fmt_string, style, text);
    51 	else
    51 	else
    52 		return text;
    52 		return text;
    53 	end
    53 	end
    54 end
    54 end
    55 
    55 
    56 function getstyle(...)
    56 local function getstyle(...)
    57 	local styles, result = { ... }, {};
    57 	local styles, result = { ... }, {};
    58 	for i, style in ipairs(styles) do
    58 	for i, style in ipairs(styles) do
    59 		style = stylemap[style];
    59 		style = stylemap[style];
    60 		if style then
    60 		if style then
    61 			t_insert(result, style);
    61 			t_insert(result, style);
    63 	end
    63 	end
    64 	return t_concat(result, ";");
    64 	return t_concat(result, ";");
    65 end
    65 end
    66 
    66 
    67 local last = "0";
    67 local last = "0";
    68 function setstyle(style)
    68 local function setstyle(style)
    69 	style = style or "0";
    69 	style = style or "0";
    70 	if style ~= last then
    70 	if style ~= last then
    71 		io_write("\27["..style.."m");
    71 		io_write("\27["..style.."m");
    72 		last = style;
    72 		last = style;
    73 	end
    73 	end
    93 		t_insert(css, cssmap[tonumber(code)]);
    93 		t_insert(css, cssmap[tonumber(code)]);
    94 	end
    94 	end
    95 	return "</span><span style='"..t_concat(css, ";").."'>";
    95 	return "</span><span style='"..t_concat(css, ";").."'>";
    96 end
    96 end
    97 
    97 
    98 function tohtml(input)
    98 local function tohtml(input)
    99 	return input:gsub("\027%[(.-)m", ansi2css);
    99 	return input:gsub("\027%[(.-)m", ansi2css);
   100 end
   100 end
   101 
   101 
   102 return _M;
   102 return {
       
   103 	getstring = getstring;
       
   104 	getstyle = getstyle;
       
   105 	setstyle = setstyle;
       
   106 	tohtml = tohtml;
       
   107 };