Skip to content

Commit 17355f1

Browse files
author
Markus Klotzbuecher
committed
add ansicolors module
1 parent 6feaf2a commit 17355f1

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

ansicolors.lua

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
-- Copyright (c) 2009 Rob Hoelz <[email protected]>
2+
--
3+
-- Permission is hereby granted, free of charge, to any person obtaining a copy
4+
-- of this software and associated documentation files (the "Software"), to deal
5+
-- in the Software without restriction, including without limitation the rights
6+
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
-- copies of the Software, and to permit persons to whom the Software is
8+
-- furnished to do so, subject to the following conditions:
9+
--
10+
-- The above copyright notice and this permission notice shall be included in
11+
-- all copies or substantial portions of the Software.
12+
--
13+
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
-- THE SOFTWARE.
20+
21+
local pairs = pairs
22+
local tostring = tostring
23+
local setmetatable = setmetatable
24+
local schar = string.char
25+
26+
module 'ansicolors'
27+
28+
local colormt = {}
29+
30+
function colormt:__tostring()
31+
return self.value
32+
end
33+
34+
function colormt:__concat(other)
35+
return tostring(self) .. tostring(other)
36+
end
37+
38+
function colormt:__call(s)
39+
return self .. s .. _M.reset
40+
end
41+
42+
colormt.__metatable = {}
43+
44+
local function makecolor(value)
45+
return setmetatable({ value = schar(27) .. '[' .. tostring(value) .. 'm' }, colormt)
46+
end
47+
48+
local colors = {
49+
-- attributes
50+
reset = 0,
51+
clear = 0,
52+
bright = 1,
53+
dim = 2,
54+
underscore = 4,
55+
blink = 5,
56+
reverse = 7,
57+
hidden = 8,
58+
59+
-- foreground
60+
black = 30,
61+
red = 31,
62+
green = 32,
63+
yellow = 33,
64+
blue = 34,
65+
magenta = 35,
66+
cyan = 36,
67+
white = 37,
68+
69+
-- background
70+
onblack = 40,
71+
onred = 41,
72+
ongreen = 42,
73+
onyellow = 43,
74+
onblue = 44,
75+
onmagenta = 45,
76+
oncyan = 46,
77+
onwhite = 47,
78+
}
79+
80+
for c, v in pairs(colors) do
81+
_M[c] = makecolor(v)
82+
end

0 commit comments

Comments
 (0)