l43_simple_color 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/simple_color +8 -0
- data/lib/l43/simple_color/cli.rb +15 -0
- data/lib/l43/simple_color/color_data.rb +294 -0
- data/lib/l43/simple_color/colorize.rb +54 -0
- data/lib/l43/simple_color/file_colorizer.rb +44 -0
- data/lib/l43/simple_color/version.rb +1 -1
- data/lib/l43/simple_color.rb +27 -45
- metadata +8 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 10eeebfdefb2b76a9368c0e9dff8af58c2a4e2898b53212b0fb1df37541be809
|
|
4
|
+
data.tar.gz: b98c3bf606f3421855b385dba90dd3ed41aac941c65656053e4c627d18e263bc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bc0f9f77944abe101cad1851f5c81a4b14ed7aa02719b494ad374714e47cdce11060467a15fe57e9bd63158ac3907697a1c524247db7b9eba59dec349bc2777b
|
|
7
|
+
data.tar.gz: dda276f2001d5e90932606dda820d8df70f59be2263b6d73139c6acbf08fa612f9b5dbd0b5aedfae50824d1d571b1c113be5b356c0a2a534120fe3e323ce3754
|
data/bin/simple_color
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'file_colorizer'
|
|
4
|
+
module L43
|
|
5
|
+
module SimpleColor
|
|
6
|
+
module Cli extend self
|
|
7
|
+
def run(argv)
|
|
8
|
+
SimpleColor.xcolorize_file(argv.first).each do |line, _|
|
|
9
|
+
puts line
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module L43
|
|
4
|
+
module SimpleColor
|
|
5
|
+
BadColor = Class.new(RuntimeError)
|
|
6
|
+
|
|
7
|
+
module ColorData
|
|
8
|
+
ANSI_CODE_SPLITTING_STRING = "__"
|
|
9
|
+
|
|
10
|
+
CSI = "\e["
|
|
11
|
+
|
|
12
|
+
Bold = "\x1b[1m"
|
|
13
|
+
Dim = "\x1b[2m"
|
|
14
|
+
Reset = "\x1b[0m"
|
|
15
|
+
Underline = "\x1b[4m"
|
|
16
|
+
|
|
17
|
+
Colors = {
|
|
18
|
+
black: "\x1b[30m",
|
|
19
|
+
blue: "\x1b[34m",
|
|
20
|
+
cyan: "\x1b[36m",
|
|
21
|
+
green: "\x1b[32m",
|
|
22
|
+
magenta: "\x1b[35m",
|
|
23
|
+
red: "\x1b[31m",
|
|
24
|
+
white: "\x1b[37m",
|
|
25
|
+
yellow: "\x1b[33m",
|
|
26
|
+
|
|
27
|
+
bgblack: "\x1b[40m",
|
|
28
|
+
bgblue: "\x1b[44m",
|
|
29
|
+
bgcyan: "\x1b[46m",
|
|
30
|
+
bggreen: "\x1b[42m",
|
|
31
|
+
bgmagenta: "\x1b[45m",
|
|
32
|
+
bgred: "\x1b[41m",
|
|
33
|
+
bgwhite: "\x1b[47m",
|
|
34
|
+
bgyellow: "\x1b[43m",
|
|
35
|
+
|
|
36
|
+
b: Bold,
|
|
37
|
+
bold: Bold,
|
|
38
|
+
d: Dim,
|
|
39
|
+
dim: Dim,
|
|
40
|
+
r: Reset,
|
|
41
|
+
reset: Reset,
|
|
42
|
+
ul: Underline,
|
|
43
|
+
underline: Underline,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
RGB_COLORS = {
|
|
47
|
+
aqua: [0, 255, 255],
|
|
48
|
+
aquamarine1: [95, 255, 215],
|
|
49
|
+
aquamarine3: [95, 215, 175],
|
|
50
|
+
azure1: [240, 255, 255],
|
|
51
|
+
blue1: [0, 0, 255],
|
|
52
|
+
blue3: [0, 0, 175],
|
|
53
|
+
blue_violet: [95, 0, 255],
|
|
54
|
+
cadet_blue: [95, 175, 135],
|
|
55
|
+
chartreuse1: [135, 255, 0],
|
|
56
|
+
chartreuse2: [95, 255, 0],
|
|
57
|
+
chartreuse3: [95, 175, 0],
|
|
58
|
+
chartreuse4: [95, 135, 0],
|
|
59
|
+
cornflower_blue: [95, 135, 255],
|
|
60
|
+
cornsilk1: [255, 255, 215],
|
|
61
|
+
cyan1: [0, 255, 255],
|
|
62
|
+
cyan2: [0, 255, 215],
|
|
63
|
+
cyan3: [0, 215, 175],
|
|
64
|
+
dark_blue: [0, 0, 135],
|
|
65
|
+
dark_cyan: [0, 175, 135],
|
|
66
|
+
dark_goldenrod: [175, 135, 0],
|
|
67
|
+
dark_green: [0, 95, 0],
|
|
68
|
+
dark_khaki: [175, 175, 95],
|
|
69
|
+
dark_magenta: [135, 0, 135],
|
|
70
|
+
dark_olive_green1: [215, 255, 95],
|
|
71
|
+
dark_olive_green2: [175, 255, 95],
|
|
72
|
+
dark_olive_green3: [135, 175, 95],
|
|
73
|
+
dark_orange3: [175, 95, 0],
|
|
74
|
+
dark_orange: [255, 135, 0],
|
|
75
|
+
dark_red: [95, 0, 0],
|
|
76
|
+
dark_sea_green1: [175, 255, 215],
|
|
77
|
+
dark_sea_green2: [175, 215, 175],
|
|
78
|
+
dark_sea_green3: [135, 215, 175],
|
|
79
|
+
dark_sea_green4: [95, 135, 95],
|
|
80
|
+
dark_sea_green: [135, 175, 135],
|
|
81
|
+
dark_slate_gray1: [135, 255, 255],
|
|
82
|
+
dark_slate_gray2: [95, 255, 255],
|
|
83
|
+
dark_slate_gray3: [135, 215, 215],
|
|
84
|
+
dark_turquoise: [0, 215, 215],
|
|
85
|
+
dark_violet: [135, 0, 215],
|
|
86
|
+
deep_pink1: [255, 0, 135],
|
|
87
|
+
deep_pink2: [255, 0, 95],
|
|
88
|
+
deep_pink3: [215, 0, 95],
|
|
89
|
+
deep_pink4: [95, 0, 95],
|
|
90
|
+
deep_sky_blue1: [0, 175, 255],
|
|
91
|
+
deep_sky_blue2: [0, 175, 215],
|
|
92
|
+
deep_sky_blue3: [0, 135, 175],
|
|
93
|
+
deep_sky_blue4: [0, 95, 95],
|
|
94
|
+
dodger_blue1: [0, 135, 255],
|
|
95
|
+
dodger_blue2: [0, 95, 255],
|
|
96
|
+
dodger_blue3: [0, 95, 215],
|
|
97
|
+
fuchsia: [255, 0, 255],
|
|
98
|
+
gold1: [255, 215, 0],
|
|
99
|
+
gold3: [175, 175, 0],
|
|
100
|
+
green1: [0, 255, 0],
|
|
101
|
+
green3: [0, 175, 0],
|
|
102
|
+
green4: [0, 135, 0],
|
|
103
|
+
green_yellow: [175, 255, 0],
|
|
104
|
+
grey0: [0, 0, 0],
|
|
105
|
+
grey100: [255, 255, 255],
|
|
106
|
+
grey11: [28, 28, 28],
|
|
107
|
+
grey15: [38, 38, 38],
|
|
108
|
+
grey19: [48, 48, 48],
|
|
109
|
+
grey23: [58, 58, 58],
|
|
110
|
+
grey27: [68, 68, 68],
|
|
111
|
+
grey30: [78, 78, 78],
|
|
112
|
+
grey35: [88, 88, 88],
|
|
113
|
+
grey37: [95, 95, 95],
|
|
114
|
+
grey39: [98, 98, 98],
|
|
115
|
+
grey3: [8, 8, 8],
|
|
116
|
+
grey42: [108, 108, 108],
|
|
117
|
+
grey46: [118, 118, 118],
|
|
118
|
+
grey50: [128, 128, 128],
|
|
119
|
+
grey53: [135, 135, 135],
|
|
120
|
+
grey54: [138, 138, 138],
|
|
121
|
+
grey58: [148, 148, 148],
|
|
122
|
+
grey62: [158, 158, 158],
|
|
123
|
+
grey63: [175, 135, 175],
|
|
124
|
+
grey66: [168, 168, 168],
|
|
125
|
+
grey69: [175, 175, 175],
|
|
126
|
+
grey70: [178, 178, 178],
|
|
127
|
+
grey74: [188, 188, 188],
|
|
128
|
+
grey78: [198, 198, 198],
|
|
129
|
+
grey7: [18, 18, 18],
|
|
130
|
+
grey82: [208, 208, 208],
|
|
131
|
+
grey84: [215, 215, 215],
|
|
132
|
+
grey85: [218, 218, 218],
|
|
133
|
+
grey89: [228, 228, 228],
|
|
134
|
+
grey93: [238, 238, 238],
|
|
135
|
+
grey: [128, 128, 128],
|
|
136
|
+
honeydew2: [215, 255, 215],
|
|
137
|
+
hot_pink2: [215, 95, 175],
|
|
138
|
+
hot_pink3: [175, 95, 135],
|
|
139
|
+
hot_pink: [255, 95, 175],
|
|
140
|
+
indian_red1: [255, 95, 95],
|
|
141
|
+
indian_red: [175, 95, 95],
|
|
142
|
+
khaki1: [255, 255, 135],
|
|
143
|
+
khaki3: [215, 215, 95],
|
|
144
|
+
light_coral: [255, 135, 135],
|
|
145
|
+
light_cyan1: [215, 255, 255],
|
|
146
|
+
light_cyan3: [175, 215, 215],
|
|
147
|
+
light_goldenrod1: [255, 255, 95],
|
|
148
|
+
light_goldenrod2: [215, 215, 135],
|
|
149
|
+
light_goldenrod3: [215, 175, 95],
|
|
150
|
+
light_green: [135, 255, 95],
|
|
151
|
+
light_pink1: [255, 175, 175],
|
|
152
|
+
light_pink3: [215, 135, 135],
|
|
153
|
+
light_pink4: [135, 95, 95],
|
|
154
|
+
light_salmon1: [255, 175, 135],
|
|
155
|
+
light_salmon3: [175, 135, 95],
|
|
156
|
+
light_sea_green: [0, 175, 175],
|
|
157
|
+
light_sky_blue1: [175, 215, 255],
|
|
158
|
+
light_sky_blue3: [135, 175, 175],
|
|
159
|
+
light_slate_blue: [135, 135, 255],
|
|
160
|
+
light_slate_grey: [135, 135, 175],
|
|
161
|
+
light_steel_blue1: [215, 215, 255],
|
|
162
|
+
light_steel_blue3: [175, 175, 215],
|
|
163
|
+
light_steel_blue: [175, 175, 255],
|
|
164
|
+
light_yellow3: [215, 215, 175],
|
|
165
|
+
lime: [0, 255, 0],
|
|
166
|
+
magenta1: [255, 0, 255],
|
|
167
|
+
magenta2: [215, 0, 255],
|
|
168
|
+
magenta3: [175, 0, 175],
|
|
169
|
+
maroon: [128, 0, 0],
|
|
170
|
+
medium_orchid1: [215, 95, 255],
|
|
171
|
+
medium_orchid3: [175, 95, 175],
|
|
172
|
+
medium_orchid: [175, 95, 215],
|
|
173
|
+
medium_purple1: [175, 135, 255],
|
|
174
|
+
medium_purple2: [175, 95, 255],
|
|
175
|
+
medium_purple3: [135, 95, 175],
|
|
176
|
+
medium_purple4: [95, 95, 135],
|
|
177
|
+
medium_purple: [135, 135, 215],
|
|
178
|
+
medium_spring_green: [0, 255, 175],
|
|
179
|
+
medium_turquoise: [95, 215, 215],
|
|
180
|
+
medium_violet_red: [175, 0, 135],
|
|
181
|
+
misty_rose1: [255, 215, 215],
|
|
182
|
+
misty_rose3: [215, 175, 175],
|
|
183
|
+
navajo_white1: [255, 215, 175],
|
|
184
|
+
navajo_white3: [175, 175, 135],
|
|
185
|
+
navy: [0, 0, 128],
|
|
186
|
+
navy_blue: [0, 0, 95],
|
|
187
|
+
olive: [128, 128, 0],
|
|
188
|
+
orange: [255, 128, 0],
|
|
189
|
+
orange1: [255, 175, 0],
|
|
190
|
+
orange3: [215, 135, 0],
|
|
191
|
+
orange4: [95, 95, 0],
|
|
192
|
+
orange_red1: [255, 95, 0],
|
|
193
|
+
orchid1: [255, 135, 255],
|
|
194
|
+
orchid2: [255, 135, 215],
|
|
195
|
+
orchid: [215, 95, 215],
|
|
196
|
+
pale_green1: [135, 255, 175],
|
|
197
|
+
pale_green3: [95, 215, 95],
|
|
198
|
+
pale_turquoise1: [175, 255, 255],
|
|
199
|
+
pale_turquoise4: [95, 135, 135],
|
|
200
|
+
pale_violet_red1: [255, 135, 175],
|
|
201
|
+
pink1: [255, 175, 215],
|
|
202
|
+
pink3: [215, 135, 175],
|
|
203
|
+
plum1: [255, 175, 255],
|
|
204
|
+
plum2: [215, 175, 255],
|
|
205
|
+
plum3: [215, 135, 215],
|
|
206
|
+
plum4: [135, 95, 135],
|
|
207
|
+
purple3: [95, 0, 215],
|
|
208
|
+
purple4: [95, 0, 135],
|
|
209
|
+
purple: [128, 0, 128],
|
|
210
|
+
red1: [255, 0, 0],
|
|
211
|
+
red3: [175, 0, 0],
|
|
212
|
+
rosy_brown: [175, 135, 135],
|
|
213
|
+
royal_blue1: [95, 95, 255],
|
|
214
|
+
salmon1: [255, 135, 95],
|
|
215
|
+
sandy_brown: [255, 175, 95],
|
|
216
|
+
sea_green1: [95, 255, 135],
|
|
217
|
+
sea_green2: [95, 255, 95],
|
|
218
|
+
sea_green3: [95, 215, 135],
|
|
219
|
+
silver: [192, 192, 192],
|
|
220
|
+
sky_blue1: [135, 215, 255],
|
|
221
|
+
sky_blue2: [135, 175, 255],
|
|
222
|
+
sky_blue3: [95, 175, 215],
|
|
223
|
+
slate_blue1: [135, 95, 255],
|
|
224
|
+
slate_blue3: [95, 95, 175],
|
|
225
|
+
spring_green1: [0, 255, 135],
|
|
226
|
+
spring_green2: [0, 215, 135],
|
|
227
|
+
spring_green3: [0, 175, 95],
|
|
228
|
+
spring_green4: [0, 135, 95],
|
|
229
|
+
steel_blue1: [95, 175, 255],
|
|
230
|
+
steel_blue3: [95, 135, 215],
|
|
231
|
+
steel_blue: [95, 135, 175],
|
|
232
|
+
tan: [215, 175, 135],
|
|
233
|
+
teal: [0, 128, 128],
|
|
234
|
+
thistle1: [255, 215, 255],
|
|
235
|
+
thistle3: [215, 175, 215],
|
|
236
|
+
turquoise2: [0, 215, 255],
|
|
237
|
+
turquoise4: [0, 135, 135],
|
|
238
|
+
violet: [215, 135, 255],
|
|
239
|
+
wheat1: [255, 255, 175],
|
|
240
|
+
wheat4: [135, 135, 95],
|
|
241
|
+
yellow1: [255, 255, 0],
|
|
242
|
+
yellow2: [215, 255, 0],
|
|
243
|
+
yellow3: [175, 215, 0],
|
|
244
|
+
yellow4: [135, 135, 0]
|
|
245
|
+
}.map do |k, rgb|
|
|
246
|
+
[k, [CSI, "38;2;", *rgb.join(";"), "m"].join]
|
|
247
|
+
end.to_h
|
|
248
|
+
|
|
249
|
+
ExtendedColors =
|
|
250
|
+
(0..255)
|
|
251
|
+
.map {["color#{it}".to_sym, [CSI, "38;5;", it, "m"].join ]}
|
|
252
|
+
.to_h
|
|
253
|
+
.merge(RGB_COLORS)
|
|
254
|
+
.freeze
|
|
255
|
+
|
|
256
|
+
def get_chunk(sym, extended: false)
|
|
257
|
+
code = Colors.fetch(sym.to_sym) do
|
|
258
|
+
if extended
|
|
259
|
+
ExtendedColors.fetch(sym.to_sym) {
|
|
260
|
+
[:error, "bad color code :#{sym}"]
|
|
261
|
+
}
|
|
262
|
+
else
|
|
263
|
+
[:error, "bad color code :#{sym}"]
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
use_color? ? code : ""
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def get_chunk!(sym, extended: false)
|
|
270
|
+
case get_chunk(sym, extended:)
|
|
271
|
+
in :error, msg
|
|
272
|
+
raise BadColor, msg
|
|
273
|
+
in result
|
|
274
|
+
result
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def get_chunks(sym, extended:)
|
|
279
|
+
sym.to_s.split(ANSI_CODE_SPLITTING_STRING).map do
|
|
280
|
+
case get_chunk(it, extended:)
|
|
281
|
+
in [:error, _messag] => error
|
|
282
|
+
return error
|
|
283
|
+
in encoded
|
|
284
|
+
encoded
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
.join
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def use_color? = @__use_color__ ||= !ENV['NO_COLOR']
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'l43/simple_color/color_data'
|
|
4
|
+
module L43
|
|
5
|
+
module SimpleColor
|
|
6
|
+
module Colorize extend self
|
|
7
|
+
include ColorData
|
|
8
|
+
class Parser
|
|
9
|
+
private
|
|
10
|
+
def initialize(str, reset: true)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
ColorSpecRgx = %r{(?:\s|\A):[__[:alnum:]]+(?:\s|\z)}
|
|
15
|
+
Meta = %r{[:\s]+}
|
|
16
|
+
|
|
17
|
+
def colorize(str, reset: true, extended: false, raises: false)
|
|
18
|
+
case _colorize(str, extended:)
|
|
19
|
+
in String => encoded
|
|
20
|
+
reset ? encoded + get_chunk(:reset) : encoded
|
|
21
|
+
in [:error, msg] => error
|
|
22
|
+
raises ? raise(BadColor, msg) : error
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def colorize!(str, reset: true, extended: false) = colorize(str, raises: true, reset:, extended:)
|
|
27
|
+
def xcolorize(str, reset: true) = colorize(str, reset:, extended: true)
|
|
28
|
+
def xcolorize!(str, reset: true) = colorize(str, reset:, extended: true, raises: true)
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
def _colorize(str, reset: true, extended: false)
|
|
32
|
+
case _replace_color_codes(str, extended:)
|
|
33
|
+
in [:error, _msg] => error
|
|
34
|
+
error
|
|
35
|
+
in coded
|
|
36
|
+
coded.gsub("::", ":")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def _replace_color_codes(str, extended: false)
|
|
41
|
+
str.gsub ColorSpecRgx do |match|
|
|
42
|
+
spec = match.gsub(Meta, "")
|
|
43
|
+
case get_chunks(spec, extended:)
|
|
44
|
+
in [:error, _message] => error
|
|
45
|
+
return error
|
|
46
|
+
in code
|
|
47
|
+
code
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'l43/simple_color/colorize'
|
|
4
|
+
module L43
|
|
5
|
+
module SimpleColor
|
|
6
|
+
class FileColorizer
|
|
7
|
+
include Colorize
|
|
8
|
+
attr_reader :extended, :fn, :reset, :stream
|
|
9
|
+
|
|
10
|
+
def colorize_file(&blk)
|
|
11
|
+
return stream unless blk
|
|
12
|
+
|
|
13
|
+
stream.map(&blk)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
def initialize(fn, reset:, extended: false)
|
|
18
|
+
@extended = extended
|
|
19
|
+
@fn = fn
|
|
20
|
+
@reset = reset
|
|
21
|
+
@stream = (fn ? File.open(fn) : $stdin).each(chomp: true).with_index.lazy.map(&colorize_line)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def colorize_line
|
|
25
|
+
-> line, idx do
|
|
26
|
+
# puts("LINE: #{line}")
|
|
27
|
+
case colorize(line, reset:, extended:)
|
|
28
|
+
in :error, msg
|
|
29
|
+
raise BadColor, msg + "\n\tin file:#{fn || "<:stdin>"}, line #{line.inspect}:#{idx.succ})"
|
|
30
|
+
in colored
|
|
31
|
+
[colored, idx.succ]
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def colorize_file(fn=nil, reset: true, &blk) =
|
|
38
|
+
FileColorizer.new(fn, reset:).colorize_file(&blk)
|
|
39
|
+
|
|
40
|
+
def xcolorize_file(fn=nil, reset: true, &blk) =
|
|
41
|
+
FileColorizer.new(fn, reset:, extended: true).colorize_file(&blk)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
data/lib/l43/simple_color.rb
CHANGED
|
@@ -1,74 +1,56 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'l43/simple_color/color_data'
|
|
4
|
+
require 'l43/simple_color/colorize'
|
|
3
5
|
module L43
|
|
4
6
|
module SimpleColor extend self
|
|
5
|
-
|
|
6
|
-
Dim = "\x1b[2m"
|
|
7
|
-
Reset = "\x1b[0m"
|
|
8
|
-
Underline = "\x1b[4m"
|
|
7
|
+
include ColorData
|
|
9
8
|
|
|
10
|
-
Colors = {
|
|
11
|
-
black: "\x1b[30m",
|
|
12
|
-
blue: "\x1b[34m",
|
|
13
|
-
cyan: "\x1b[36m",
|
|
14
|
-
green: "\x1b[32m",
|
|
15
|
-
magenta: "\x1b[35m",
|
|
16
|
-
red: "\x1b[31m",
|
|
17
|
-
white: "\x1b[37m",
|
|
18
|
-
yellow: "\x1b[33m",
|
|
19
9
|
|
|
20
|
-
bgblack: "\x1b[40m",
|
|
21
|
-
bgblue: "\x1b[44m",
|
|
22
|
-
bgcyan: "\x1b[46m",
|
|
23
|
-
bggreen: "\x1b[42m",
|
|
24
|
-
bgmagenta: "\x1b[45m",
|
|
25
|
-
bgred: "\x1b[41m",
|
|
26
|
-
bgwhite: "\x1b[47m",
|
|
27
|
-
bgyellow: "\x1b[43m",
|
|
28
10
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
d: Dim,
|
|
32
|
-
dim: Dim,
|
|
33
|
-
r: Reset,
|
|
34
|
-
reset: Reset,
|
|
35
|
-
ul: Underline,
|
|
36
|
-
underline: Underline,
|
|
37
|
-
}
|
|
11
|
+
def xcolor(*chunks, reset: true) = color(*chunks, extended: true, reset:)
|
|
12
|
+
def xcolor!(*chunks, reset: true) = color!(*chunks, extended: true, reset:)
|
|
38
13
|
|
|
39
|
-
def color(*chunks, reset: true)
|
|
14
|
+
def color(*chunks, reset: true, extended: false)
|
|
40
15
|
reset = reset ? :reset : ""
|
|
41
16
|
[*chunks.flatten, reset].map do |chunk|
|
|
42
|
-
format_chunk(chunk)
|
|
43
|
-
|
|
17
|
+
result = format_chunk(chunk, extended:)
|
|
18
|
+
case result
|
|
19
|
+
in [:error, _message] => error
|
|
20
|
+
return error
|
|
21
|
+
in encoded
|
|
22
|
+
encoded
|
|
23
|
+
end
|
|
24
|
+
end.join
|
|
44
25
|
end
|
|
45
26
|
|
|
46
|
-
def
|
|
47
|
-
|
|
48
|
-
|
|
27
|
+
def color!(*chunks, reset: true, extended: false)
|
|
28
|
+
case color(*chunks, reset:, extended:)
|
|
29
|
+
in :error, msg
|
|
30
|
+
raise BadColor, msg
|
|
31
|
+
in colored
|
|
32
|
+
colored
|
|
33
|
+
end
|
|
49
34
|
end
|
|
50
35
|
|
|
36
|
+
def putcol(*chunks, reset: true, device: $stderr, extended: false) = device.puts color!(chunks, reset:, extended:)
|
|
37
|
+
def xputcol(*chunks, reset: true, device: $stderr) = device.puts color!(chunks, reset:, extended: true)
|
|
38
|
+
|
|
51
39
|
private
|
|
52
|
-
def format_chunk(chunk)
|
|
40
|
+
def format_chunk(chunk, extended:)
|
|
53
41
|
case chunk
|
|
54
42
|
when String
|
|
55
43
|
chunk
|
|
56
44
|
when Integer
|
|
57
45
|
" " * chunk
|
|
58
46
|
when nil
|
|
59
|
-
"\n" + get_chunk
|
|
47
|
+
"\n" + get_chunk(:reset)
|
|
60
48
|
else
|
|
61
|
-
|
|
49
|
+
get_chunks(chunk, extended:) if use_color?
|
|
62
50
|
end
|
|
63
51
|
end
|
|
64
52
|
|
|
65
|
-
def format_color(sym)
|
|
66
|
-
sym.to_s.split("_").map { get_chunk!(it) }.join
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
def get_chunk!(sym) = Colors.fetch(sym.to_sym) { raise ArgumentError, "bad color spec: #{sym.inspect}" }
|
|
70
53
|
|
|
71
|
-
def use_color? = @__use_color__ ||= !ENV['NO_COLOR']
|
|
72
54
|
end
|
|
73
55
|
end
|
|
74
56
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: l43_simple_color
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Robert Dober
|
|
@@ -27,14 +27,20 @@ description: 'Colorize chunks of strings with ANSI colors.
|
|
|
27
27
|
|
|
28
28
|
'
|
|
29
29
|
email: robert.dober@gmail.com
|
|
30
|
-
executables:
|
|
30
|
+
executables:
|
|
31
|
+
- simple_color
|
|
31
32
|
extensions: []
|
|
32
33
|
extra_rdoc_files: []
|
|
33
34
|
files:
|
|
34
35
|
- LICENSE
|
|
35
36
|
- README.md
|
|
37
|
+
- bin/simple_color
|
|
36
38
|
- lib/l43.rb
|
|
37
39
|
- lib/l43/simple_color.rb
|
|
40
|
+
- lib/l43/simple_color/cli.rb
|
|
41
|
+
- lib/l43/simple_color/color_data.rb
|
|
42
|
+
- lib/l43/simple_color/colorize.rb
|
|
43
|
+
- lib/l43/simple_color/file_colorizer.rb
|
|
38
44
|
- lib/l43/simple_color/version.rb
|
|
39
45
|
homepage: https://www.codeberg.org/lab419/l43_simple_color.git
|
|
40
46
|
licenses:
|