Repository navigation
Expand file tree
/
Copy pathcolor.lua
More file actions
167 lines (138 loc) · 5.21 KB
/
Copy pathcolor.lua
File metadata and controls
167 lines (138 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
-- color.lua
--------------------------------------------------------------------------------
-- A super-simple way to make colored text output in Lua.
-- To use, simply print out things from this module, then print out some text.
--
-- Example:
-- print(color.bg.green .. color.fg.RED .. "This is bright red on green")
-- print(color.invert .. "This is inverted..." .. color.reset .. " And this isn't.")
-- print(color.fg(0xDE) .. color.bg(0xEE) .. "You can use xterm-256 colors too!" .. color.reset)
-- print("And also " .. color.bold .. "BOLD" .. color.normal .. " if you want.")
-- print(color.bold .. color.fg.BLUE .. color.bg.blue .. "Miss your " .. color.fg.RED .. "C-64" .. color.fg.BLUE .. "?" .. color.reset)
--
-- You can see all these examples in action by calling color.test()
--
-- Can't pick a good color scheme? Look at a handy chart:
-- print(color.chart())
--
-- If you want to add anything to this, check out the Wikipedia page on ANSI control codes:
-- http://en.wikipedia.org/wiki/ANSI_escape_code
--------------------------------------------------------------------------------
-- Copyright (C) 2012 Ross Andrews
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
--------------------------------------------------------------------------------
-- A note about licensing:
--
-- The LGPL isn't really intended to be used with non-compiled libraries. The way
-- I interpret derivative works of this library is this: if you don't modify this
-- file, and the program it's embedded in doesn't modify the Lua table it defines,
-- then you can distribute it with a program under any license. If you do either
-- of those things, then you've created a derivative work of this library and you
-- have to release the modifications you made under this same license.
module(..., package.seeall)
local ansi = not os.is("windows") or os.getenv('CMDER_ROOT') -- go, go, Cmder!
local esc = string.char(27, 91)
local names = {'black', 'red', 'green', 'yellow', 'blue', 'pink', 'cyan', 'white'}
local hi_names = {'BLACK', 'RED', 'GREEN', 'YELLOW', 'BLUE', 'PINK', 'CYAN', 'WHITE'}
fg, bg = {}, {}
if ansi then
for i, name in ipairs(names) do
fg[name] = ''
_M[name] = ''
bg[name] = ''
end
90CC
for i, name in ipairs(hi_names) do
fg[name] = ''
_M[name] = ''
bg[name] = ''
end
else
for i, name in ipairs(names) do
fg[name] = esc .. tostring(30+i-1) .. 'm'
_M[name] = fg[name]
bg[name] = esc .. tostring(40+i-1) .. 'm'
end
for i, name in ipairs(hi_names) do
fg[name] = esc .. tostring(90+i-1) .. 'm'
_M[name] = fg[name]
bg[name] = esc .. tostring(100+i-1) .. 'm'
end
end
if ansi then
reset = ''
clear = ''
bold = ''
faint = ''
normal = ''
invert = ''
underline = ''
hide = esc .. ''
show = esc .. ''
else
reset = esc .. '0m'
clear = esc .. '2J'
bold = esc .. '1m'
faint = esc .. '2m'
normal = esc .. '22m'
invert = esc .. '7m'
underline = esc .. '4m'
hide = esc .. '?25l'
show = esc .. '?25h'
end
local function fg256(_,n)
if ansi then
return ''
else
return esc .. "38;5;" .. n .. 'm'
end
end
local function bg256(_,n)
if ansi then
return ''
else
return esc .. "48;5;" .. n .. 'm'
end
end
setmetatable(fg, {__call = fg256})
setmetatable(bg, {__call = bg256})
function move(x, y)
return esc .. y .. ';' .. x .. 'H'
end
home = move(1, 1)
--------------------------------------------------
function chart(ch,col)
local cols = '0123456789abcdef'
ch = ch or ' '
col = col or fg.black
local str = color.reset .. color.bg.WHITE .. col
for y = 0, 15 do
for x = 0, 15 do
local lbl = cols:sub(x+1, x+1)
if x == 0 then lbl = cols:sub(y+1, y+1) end
str = str .. color.bg.black .. color.fg.WHITE .. lbl
str = str .. color.bg(x+y*16) .. col .. ch
end
str = str .. color.bg.black .. "\n"
end
return str .. color.reset
end
function test()
print(color.reset .. color.bg.green .. color.fg.RED .. "This is bright red on green" .. color.reset)
print(color.invert .. "This is inverted..." .. color.reset .. " And this isn't.")
print(color.fg(0xDE) .. color.bg(0xEE) .. "You can use xterm-256 colors too!" .. color.reset)
print("And also " .. color.bold .. "BOLD" .. color.normal .. " if you want.")
print(color.bold .. color.fg.BLUE .. color.bg.blue .. "Miss your " .. color.fg.RED .. "C-64" .. color.fg.BLUE .. "?" .. color.reset)
print("Try printing " .. color.underline .. _M._NAME .. ".chart()" .. color.reset)
end