Software / code / prosody
Comparison
util/termcolours.lua @ 6791:e813e8cf6046
Merge 0.10->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 20 Aug 2015 13:05:22 +0200 |
| parent | 6777:5de6b93d0190 |
| child | 7198:48d167f652ad |
comparison
equal
deleted
inserted
replaced
| 6776:4412a2307c89 | 6791:e813e8cf6046 |
|---|---|
| 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 }; |