util/termcolours.lua
author Kim Alvefur <zash@zash.se>
Tue, 14 May 2024 17:07:47 +0200
changeset 13494 6f840763fc73
parent 12979 d10957394a3c
permissions -rw-r--r--
net.server_epoll: Add support for systemd socket activation Allows creating listening sockets and accepting client connections before Prosody starts. This is unlike normal Prosody dynamic resource management, where ports may added and removed at any time, and the ports defined by the config. Weird things happen if these are closed (e.g. due to reload) so here we prevent closing and ensure sockets are reused when opened again.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4698
diff changeset
     4
--
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 262
diff changeset
     7
--
7201
48d167f652ad util.termcolours: Silence luacheck warning
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
     8
--
48d167f652ad util.termcolours: Silence luacheck warning
Kim Alvefur <zash@zash.se>
parents: 6780
diff changeset
     9
-- luacheck: ignore 213/i
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 262
diff changeset
    10
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 262
diff changeset
    11
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local t_concat, t_insert = table.concat, table.insert;
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
local char, format = string.char, string.format;
4698
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
    14
local tonumber = tonumber;
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
local ipairs = ipairs;
3749
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    16
local io_write = io.write;
7206
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    17
local m_floor = math.floor;
7208
a6c4530c1bc0 util.termcolours: Validate color codes, fixes traceback
Kim Alvefur <zash@zash.se>
parents: 7207
diff changeset
    18
local type = type;
a6c4530c1bc0 util.termcolours: Validate color codes, fixes traceback
Kim Alvefur <zash@zash.se>
parents: 7207
diff changeset
    19
local setmetatable = setmetatable;
a6c4530c1bc0 util.termcolours: Validate color codes, fixes traceback
Kim Alvefur <zash@zash.se>
parents: 7207
diff changeset
    20
local pairs = pairs;
3749
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    21
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    22
local windows;
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    23
if os.getenv("WINDIR") then
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 10427
diff changeset
    24
	windows = require "prosody.util.windows";
3749
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    25
end
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    26
local orig_color = windows and windows.get_consolecolor and windows.get_consolecolor();
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    27
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
    28
local _ENV = nil;
8558
4f0f5b49bb03 vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8371
diff changeset
    29
-- luacheck: std none
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
local stylemap = {
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
			reset = 0; bright = 1, dim = 2, underscore = 4, blink = 5, reverse = 7, hidden = 8;
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
			black = 30; red = 31; green = 32; yellow = 33; blue = 34; magenta = 35; cyan = 36; white = 37;
8371
3629639d5fa9 util.termcolours: Split a long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7209
diff changeset
    34
			["black background"] = 40; ["red background"] = 41; ["green background"] = 42; ["yellow background"] = 43;
3629639d5fa9 util.termcolours: Split a long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7209
diff changeset
    35
			["blue background"] = 44; ["magenta background"] = 45; ["cyan background"] = 46; ["white background"] = 47;
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
			bold = 1, dark = 2, underline = 4, underlined = 4, normal = 0;
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
		}
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
3749
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    39
local winstylemap = {
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    40
	["0"] = orig_color, -- reset
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    41
	["1"] = 7+8, -- bold
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    42
	["1;33"] = 2+4+8, -- bold yellow
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    43
	["1;31"] = 4+8 -- bold red
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    44
}
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    45
4698
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
    46
local cssmap = {
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
    47
	[1] = "font-weight: bold", [2] = "opacity: 0.5", [4] = "text-decoration: underline", [8] = "visibility: hidden",
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
    48
	[30] = "color:black", [31] = "color:red", [32]="color:green", [33]="color:#FFD700",
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
    49
	[34] = "color:blue", [35] = "color: magenta", [36] = "color:cyan", [37] = "color: white",
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
    50
	[40] = "background-color:black", [41] = "background-color:red", [42]="background-color:green",
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
    51
	[43]="background-color:yellow",	[44] = "background-color:blue", [45] = "background-color: magenta",
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
    52
	[46] = "background-color:cyan", [47] = "background-color: white";
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
    53
};
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
    54
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m";
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
    56
local function getstring(style, text)
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
	if style then
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
		return format(fmt_string, style, text);
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
	else
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
		return text;
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
	end
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
end
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
7206
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    64
local function gray(n)
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    65
	return m_floor(n*3/32)+0xe8;
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    66
end
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    67
local function color(r,g,b)
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    68
	if r == g and g == b then
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    69
		return gray(r);
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    70
	end
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    71
	r = m_floor(r*3/128);
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    72
	g = m_floor(g*3/128);
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    73
	b = m_floor(b*3/128);
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    74
	return 0x10 + ( r * 36 ) + ( g * 6 ) + ( b );
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    75
end
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    76
local function hex2rgb(hex)
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    77
	local r = tonumber(hex:sub(1,2),16);
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    78
	local g = tonumber(hex:sub(3,4),16);
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    79
	local b = tonumber(hex:sub(5,6),16);
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    80
	return r,g,b;
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    81
end
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    82
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    83
setmetatable(stylemap, { __index = function(_, style)
7208
a6c4530c1bc0 util.termcolours: Validate color codes, fixes traceback
Kim Alvefur <zash@zash.se>
parents: 7207
diff changeset
    84
	if type(style) == "string" and style:find("%x%x%x%x%x%x") == 1 then
a6c4530c1bc0 util.termcolours: Validate color codes, fixes traceback
Kim Alvefur <zash@zash.se>
parents: 7207
diff changeset
    85
		local g = style:sub(7) == " background" and "48;5;" or "38;5;";
10427
317bcc4f25f5 util.termcolours: Use explicit number formatting instead of coercion on concatenation
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    86
		return format("%s%d", g, color(hex2rgb(style)));
7208
a6c4530c1bc0 util.termcolours: Validate color codes, fixes traceback
Kim Alvefur <zash@zash.se>
parents: 7207
diff changeset
    87
	end
7206
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    88
end } );
be8b88ad35a3 util.termcolours: Add 256 color support
Kim Alvefur <zash@zash.se>
parents: 7202
diff changeset
    89
7207
ab2b7496a617 util.termcolours: Add some CSS color names
Kim Alvefur <zash@zash.se>
parents: 7206
diff changeset
    90
local csscolors = {
ab2b7496a617 util.termcolours: Add some CSS color names
Kim Alvefur <zash@zash.se>
parents: 7206
diff changeset
    91
	red = "ff0000"; fuchsia = "ff00ff"; green = "008000"; white = "ffffff";
ab2b7496a617 util.termcolours: Add some CSS color names
Kim Alvefur <zash@zash.se>
parents: 7206
diff changeset
    92
	lime = "00ff00"; yellow = "ffff00"; purple = "800080"; blue = "0000ff";
ab2b7496a617 util.termcolours: Add some CSS color names
Kim Alvefur <zash@zash.se>
parents: 7206
diff changeset
    93
	aqua = "00ffff"; olive  = "808000"; black  = "000000"; navy = "000080";
ab2b7496a617 util.termcolours: Add some CSS color names
Kim Alvefur <zash@zash.se>
parents: 7206
diff changeset
    94
	teal = "008080"; silver = "c0c0c0"; maroon = "800000"; gray = "808080";
ab2b7496a617 util.termcolours: Add some CSS color names
Kim Alvefur <zash@zash.se>
parents: 7206
diff changeset
    95
}
7209
1c005878db55 util.termcolours: Rename loop variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7208
diff changeset
    96
for colorname, rgb in pairs(csscolors) do
1c005878db55 util.termcolours: Rename loop variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7208
diff changeset
    97
	stylemap[colorname] = stylemap[colorname] or stylemap[rgb];
1c005878db55 util.termcolours: Rename loop variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7208
diff changeset
    98
	colorname, rgb = colorname .. " background", rgb .. " background"
1c005878db55 util.termcolours: Rename loop variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7208
diff changeset
    99
	stylemap[colorname] = stylemap[colorname] or stylemap[rgb];
7207
ab2b7496a617 util.termcolours: Add some CSS color names
Kim Alvefur <zash@zash.se>
parents: 7206
diff changeset
   100
end
ab2b7496a617 util.termcolours: Add some CSS color names
Kim Alvefur <zash@zash.se>
parents: 7206
diff changeset
   101
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   102
local function getstyle(...)
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
	local styles, result = { ... }, {};
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
	for i, style in ipairs(styles) do
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
		style = stylemap[style];
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
		if style then
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
			t_insert(result, style);
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
		end
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
	end
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
	return t_concat(result, ";");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
end
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
3749
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   113
local last = "0";
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   114
local function setstyle(style)
3749
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   115
	style = style or "0";
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   116
	if style ~= last then
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   117
		io_write("\27["..style.."m");
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   118
		last = style;
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   119
	end
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   120
end
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   121
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   122
if windows then
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   123
	function setstyle(style)
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   124
		style = style or "0";
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   125
		if style ~= last then
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   126
			windows.set_consolecolor(winstylemap[style] or orig_color);
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   127
			last = style;
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   128
		end
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   129
	end
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   130
	if not orig_color then
7202
5846e0bca4ff util.termcolours: Remove argument from noop function [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7201
diff changeset
   131
		function setstyle() end
3749
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   132
	end
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   133
end
588c09d7903c util.termcolours: Added setstyle(str), which works on Windows too.
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   134
4698
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   135
local function ansi2css(ansi_codes)
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   136
	if ansi_codes == "0" then return "</span>"; end
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   137
	local css = {};
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   138
	for code in ansi_codes:gmatch("[^;]+") do
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   139
		t_insert(css, cssmap[tonumber(code)]);
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   140
	end
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   141
	return "</span><span style='"..t_concat(css, ";").."'>";
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   142
end
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   143
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   144
local function tohtml(input)
4698
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   145
	return input:gsub("\027%[(.-)m", ansi2css);
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   146
end
739f7ae1a01e util.termcolours: tohtml() for converting output to HTML. I don't know.
Matthew Wild <mwild1@gmail.com>
parents: 3749
diff changeset
   147
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   148
return {
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   149
	getstring = getstring;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   150
	getstyle = getstyle;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   151
	setstyle = setstyle;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   152
	tohtml = tohtml;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   153
};