forked from crystal-lang/crystal
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorize.cr
338 lines (295 loc) · 7.5 KB
/
colorize.cr
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# With Colorize you can change the fore- and background colors and text decorations when rendering text
# on terminals supporting ANSI escape codes. It adds the `colorize` method to `Object` and thus all classes
# as its main interface, which calls `to_s` and surrounds it with the necessary escape codes
# when it comes to obtaining a string representation of the object.
#
# Its first argument changes the foreground color:
# ```
# require "colorize"
#
# "foo".colorize(:green)
# 100.colorize(:red)
# [1, 2, 3].colorize(:blue)
# ```
#
# There are alternative ways to change the foreground color:
# ```
# "foo".colorize.fore(:green)
# "foo".colorize.green
# ```
#
# To change the background color, the following methods are available:
# ```
# "foo".colorize.back(:green)
# "foo".colorize.on(:green)
# "foo".colorize.on_green
# ```
#
# It's also possible to change the text decoration:
# ```
# "foo".colorize.mode(:underline)
# "foo".colorize.underline
# ```
#
# The `colorize` method returns a `Colorize::Object` instance,
# which allows chaining methods together:
# ```
# "foo".colorize.fore(:yellow).back(:blue).mode(:underline)
# ```
#
# With the `toggle` method you can temporarily disable adding the escape codes.
# Settings of the instance are preserved however and can be turned back on later:
# ```
# "foo".colorize(:red).toggle(false)
# # => "foo" without color
# "foo".colorize(:red).toggle(false).toggle(true)
# # => "foo" in red
# ```
#
# Available colors are:
# ```
# :black
# :red
# :green
# :yellow
# :blue
# :magenta
# :cyan
# :light_gray
# :dark_gray
# :light_red
# :light_green
# :light_yellow
# :light_blue
# :light_magenta
# :light_cyan
# :white
# ```
#
# Available text decorations are:
# ```
# :bold
# :bright
# :dim
# :underline
# :blink
# :reverse
# :hidden
# ```
module Colorize
# If this value is `true`, `Colorize::Object` is enabled by default.
# But if this value is `false`, `Colorize::Object` is disabled.
#
# The default value is `true`.
#
# ```
# Colorize.enabled = true
# "hello".colorize.red.to_s # => "\e[31mhello\e[0m"
#
# Colorize.enabled = false
# "hello".colorize.red.to_s # => "hello"
# ```
class_property? enabled : Bool = true
# Make `Colorize.enabled` `true` if and only if both of `STDOUT.tty?` and `STDERR.tty?` are `true`.
def self.on_tty_only!
self.enabled = STDOUT.tty? && STDERR.tty?
end
def self.reset(io = STDOUT)
io << "\e[0m" if enabled?
end
end
def with_color
"".colorize
end
def with_color(color : Symbol)
"".colorize(color)
end
module Colorize::ObjectExtensions
def colorize
Colorize::Object.new(self)
end
def colorize(fore)
Colorize::Object.new(self).fore(fore)
end
end
class Object
include Colorize::ObjectExtensions
end
struct Colorize::Object(T)
private FORE_DEFAULT = "39"
private FORE_BLACK = "30"
private FORE_RED = "31"
private FORE_GREEN = "32"
private FORE_YELLOW = "33"
private FORE_BLUE = "34"
private FORE_MAGENTA = "35"
private FORE_CYAN = "36"
private FORE_LIGHT_GRAY = "37"
private FORE_DARK_GRAY = "90"
private FORE_LIGHT_RED = "91"
private FORE_LIGHT_GREEN = "92"
private FORE_LIGHT_YELLOW = "93"
private FORE_LIGHT_BLUE = "94"
private FORE_LIGHT_MAGENTA = "95"
private FORE_LIGHT_CYAN = "96"
private FORE_WHITE = "97"
private BACK_DEFAULT = "49"
private BACK_BLACK = "40"
private BACK_RED = "41"
private BACK_GREEN = "42"
private BACK_YELLOW = "43"
private BACK_BLUE = "44"
private BACK_MAGENTA = "45"
private BACK_CYAN = "46"
private BACK_LIGHT_GRAY = "47"
private BACK_DARK_GRAY = "100"
private BACK_LIGHT_RED = "101"
private BACK_LIGHT_GREEN = "102"
private BACK_LIGHT_YELLOW = "103"
private BACK_LIGHT_BLUE = "104"
private BACK_LIGHT_MAGENTA = "105"
private BACK_LIGHT_CYAN = "106"
private BACK_WHITE = "107"
private MODE_DEFAULT = '0'
private MODE_BOLD = '1'
private MODE_BRIGHT = '1'
private MODE_DIM = '2'
private MODE_UNDERLINE = '4'
private MODE_BLINK = '5'
private MODE_REVERSE = '7'
private MODE_HIDDEN = '8'
private MODE_BOLD_FLAG = 1
private MODE_BRIGHT_FLAG = 1
private MODE_DIM_FLAG = 2
private MODE_UNDERLINE_FLAG = 4
private MODE_BLINK_FLAG = 8
private MODE_REVERSE_FLAG = 16
private MODE_HIDDEN_FLAG = 32
private COLORS = %w(black red green yellow blue magenta cyan light_gray dark_gray light_red light_green light_yellow light_blue light_magenta light_cyan white)
private MODES = %w(bold bright dim underline blink reverse hidden)
def initialize(@object : T)
@fore = FORE_DEFAULT
@back = BACK_DEFAULT
@mode = 0
@enabled = Colorize.enabled?
end
{% for name in COLORS %}
def {{name.id}}
@fore = FORE_{{name.upcase.id}}
self
end
def on_{{name.id}}
@back = BACK_{{name.upcase.id}}
self
end
{% end %}
{% for name in MODES %}
def {{name.id}}
@mode |= MODE_{{name.upcase.id}}_FLAG
self
end
{% end %}
def fore(color : Symbol)
{% for name in COLORS %}
if color == :{{name.id}}
@fore = FORE_{{name.upcase.id}}
return self
end
{% end %}
raise ArgumentError.new "Unknown color: #{color}"
end
def back(color : Symbol)
{% for name in COLORS %}
if color == :{{name.id}}
@back = BACK_{{name.upcase.id}}
return self
end
{% end %}
raise ArgumentError.new "Unknown color: #{color}"
end
def mode(mode : Symbol)
{% for name in MODES %}
if mode == :{{name.id}}
@mode |= MODE_{{name.upcase.id}}_FLAG
return self
end
{% end %}
raise ArgumentError.new "Unknown mode: #{mode}"
end
def on(color : Symbol)
back color
end
def toggle(flag)
@enabled = !!flag
self
end
def to_s(io)
surround(io) do
io << @object
end
end
def inspect(io)
surround(io) do
@object.inspect(io)
end
end
def surround(io = STDOUT)
must_append_end = append_start(io)
yield io
append_end(io) if must_append_end
end
STACK = [] of Colorize::Object(String)
def push(io = STDOUT)
last_color = STACK.last?
append_start(io, !!last_color)
STACK.push self
yield io
STACK.pop
if last_color
last_color.append_start(io, true)
else
append_end(io)
end
end
protected def append_start(io, reset = false)
return false unless @enabled
fore_is_default = @fore == FORE_DEFAULT
back_is_default = @back == BACK_DEFAULT
mode_is_default = @mode == 0
if fore_is_default && back_is_default && mode_is_default && !reset
false
else
io << "\e["
printed = false
if reset
io << MODE_DEFAULT
printed = true
end
unless fore_is_default
io << ';' if printed
io << @fore
printed = true
end
unless back_is_default
io << ';' if printed
io << @back
printed = true
end
unless mode_is_default
# Can't reuse MODES constant because it has bold/bright duplicated
{% for name in %w(bold dim underline blink reverse hidden) %}
if @mode.bits_set? MODE_{{name.upcase.id}}_FLAG
io << ';' if printed
io << MODE_{{name.upcase.id}}
printed = true
end
{% end %}
end
io << 'm'
true
end
end
protected def append_end(io)
Colorize.reset(io)
end
end