-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcolorize.cr
564 lines (504 loc) · 12.7 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# 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.
#
# NOTE: To use `Colorize`, you must explicitly import it with `require "colorize"`
#
# 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:
#
# ```
# require "colorize"
#
# "foo".colorize.fore(:green)
# "foo".colorize.green
# ```
#
# To change the background color, the following methods are available:
#
# ```
# require "colorize"
#
# "foo".colorize.back(:green)
# "foo".colorize.on(:green)
# "foo".colorize.on_green
# ```
#
# You can also pass an RGB color to `colorize`:
#
# ```
# require "colorize"
#
# "foo".colorize(0, 255, 255) # => "foo" in aqua
# "foo".colorize.fore(0, 255, 255) # => "foo" in aqua
#
# # This is the same as:
#
# "foo".colorize(Colorize::ColorRGB.new(0, 255, 255)) # => "foo" in aqua
# "foo".colorize.fore(Colorize::ColorRGB.new(0, 255, 255)) # => "foo" in aqua
# ```
#
# Or an 8-bit color:
#
# ```
# require "colorize"
#
# "foo".colorize(Colorize::Color256.new(208)) # => "foo" in orange
# "foo".colorize.fore(Colorize::Color256.new(208)) # => "foo" in orange
# ```
#
# It's also possible to change the text decoration:
#
# ```
# require "colorize"
#
# "foo".colorize.mode(:underline)
# "foo".colorize.underline
# ```
#
# The `colorize` method returns a `Colorize::Object` instance,
# which allows chaining methods together:
#
# ```
# require "colorize"
#
# "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:
#
# ```
# require "colorize"
#
# "foo".colorize(:red).toggle(false) # => "foo" without color
# "foo".colorize(:red).toggle(false).toggle(true) # => "foo" in red
# ```
#
# The color `:default` leaves the object's representation as it is but the object is a `Colorize::Object` then
# which is handy in conditions such as:
#
# ```
# require "colorize"
#
# "foo".colorize(Random::DEFAULT.next_bool ? :green : :default)
# ```
#
# Available colors are:
# ```
# :default
# :black
# :red
# :green
# :yellow
# :blue
# :magenta
# :cyan
# :light_gray
# :dark_gray
# :light_red
# :light_green
# :light_yellow
# :light_blue
# :light_magenta
# :light_cyan
# :white
# ```
#
# See `Colorize::Mode` for available text decorations.
module Colorize
# Objects will only be colored if this is `true`.
#
# ```
# require "colorize"
#
# Colorize.enabled = true
# "hello".colorize.red.to_s # => "\e[31mhello\e[0m"
#
# Colorize.enabled = false
# "hello".colorize.red.to_s # => "hello"
# ```
#
# NOTE: This is by default disabled if the environment variable `NO_COLOR` contains any value.
class_property? enabled : Bool { !ENV.has_key?("NO_COLOR") }
# Makes `Colorize.enabled` `true` if and only if both of `STDOUT.tty?`
# and `STDERR.tty?` are `true` and the tty is not considered a dumb terminal.
# This is determined by the environment variable called `TERM`.
# If `TERM=dumb`, color won't be enabled.
# If `NO_COLOR` contains any value color won't be enabled conforming to https://no-color.org
def self.on_tty_only!
self.enabled = STDOUT.tty? && STDERR.tty? && ENV["TERM"]? != "dumb" && !ENV.has_key?("NO_COLOR")
end
# Resets the color and text decoration of the *io*.
#
# ```
# io = IO::Memory.new
# Colorize.with.green.surround(io) do
# io << "green"
# Colorize.reset(io)
# io << " default"
# end
# ```
def self.reset(io = STDOUT)
io << "\e[0m" if enabled?
end
# Helper method to use colorize with `IO`.
#
# ```
# io = IO::Memory.new
# io << "not-green"
# Colorize.with.green.bold.surround(io) do
# io << "green and bold if Colorize.enabled"
# end
# ```
def self.with : Colorize::Object(String)
"".colorize
end
end
module Colorize::ObjectExtensions
# Turns `self` into a `Colorize::Object`.
def colorize : Colorize::Object
Colorize::Object.new(self)
end
# Wraps `self` in a `Colorize::Object` and colors it with the given `Color256`
# made up from the single *fore* byte.
def colorize(fore : UInt8)
Colorize::Object.new(self).fore(fore)
end
# Wraps `self` in a `Colorize::Object` and colors it with the given `ColorRGB` made
# up from the given *r*ed, *g*reen and *b*lue values.
def colorize(r : UInt8, g : UInt8, b : UInt8)
Colorize::Object.new(self).fore(r, g, b)
end
# Wraps `self` in a `Colorize::Object` and colors it with the given *fore* `Color`.
def colorize(fore : Color)
Colorize::Object.new(self).fore(fore)
end
# Wraps `self` in a `Colorize::Object` and colors it with the given *fore* color.
def colorize(fore : Symbol)
Colorize::Object.new(self).fore(fore)
end
end
class Object
include Colorize::ObjectExtensions
end
module Colorize
alias Color = ColorANSI | Color256 | ColorRGB
# One color of a fixed set of colors.
enum ColorANSI
Default = 39
Black = 30
Red = 31
Green = 32
Yellow = 33
Blue = 34
Magenta = 35
Cyan = 36
LightGray = 37
DarkGray = 90
LightRed = 91
LightGreen = 92
LightYellow = 93
LightBlue = 94
LightMagenta = 95
LightCyan = 96
White = 97
def fore(io : IO) : Nil
to_i.to_s io
end
def back(io : IO) : Nil
(to_i + 10).to_s io
end
end
# An 8-bit color.
record Color256,
value : UInt8 do
def fore(io : IO) : Nil
io << "38;5;"
value.to_s io
end
def back(io : IO) : Nil
io << "48;5;"
value.to_s io
end
end
# An RGB color.
record ColorRGB,
red : UInt8,
green : UInt8,
blue : UInt8 do
def fore(io : IO) : Nil
io << "38;2;"
io << red << ";"
io << green << ";"
io << blue
end
def back(io : IO) : Nil
io << "48;2;"
io << red << ";"
io << green << ";"
io << blue
end
end
# A text decoration.
#
# Note that not all text decorations are supported in all terminals.
# When a text decoration is not supported, it will leave the text unaffected.
@[Flags]
enum Mode
# Makes the text bold.
#
# Same as `Bright`.
Bold = 1
# Makes the text color bright.
#
# Same as `Bold`.
Bright = 1
# Dims the text color.
Dim
# Draws a line below the text.
Underline
# Makes the text blink slowly.
Blink
# Swaps the foreground and background colors of the text.
Reverse
# Makes the text invisible.
Hidden
# Italicizes the text.
Italic
# Makes the text blink quickly.
BlinkFast
# Crosses out the text.
Strikethrough
# Draws two lines below the text.
DoubleUnderline
# Draws a line above the text.
Overline
end
end
private def each_code(mode : Colorize::Mode, &)
yield "1" if mode.bold?
yield "2" if mode.dim?
yield "3" if mode.italic?
yield "4" if mode.underline?
yield "5" if mode.blink?
yield "6" if mode.blink_fast?
yield "7" if mode.reverse?
yield "8" if mode.hidden?
yield "9" if mode.strikethrough?
yield "21" if mode.double_underline?
yield "53" if mode.overline?
end
# A colorized object. Colors and text decorations can be modified.
struct Colorize::Object(T)
private COLORS = %w(default black red green yellow blue magenta cyan light_gray dark_gray light_red light_green light_yellow light_blue light_magenta light_cyan white)
@fore : Color
@back : Color
def initialize(@object : T)
@fore = ColorANSI::Default
@back = ColorANSI::Default
@mode = Mode::None
@enabled = Colorize.enabled?
end
{% for name in COLORS %}
def {{name.id}}
@fore = ColorANSI::{{name.camelcase.id}}
self
end
def on_{{name.id}}
@back = ColorANSI::{{name.camelcase.id}}
self
end
{% end %}
{% for mode in Mode.constants.reject { |constant| constant == "All" || constant == "None" } %}
# Apply text decoration `Mode::{{ mode }}`.
def {{mode.underscore.id}}
mode Mode::{{mode.id}}
end
{% end %}
def fore(color : Symbol) : self
{% for name in COLORS %}
if color == :{{name.id}}
@fore = ColorANSI::{{name.camelcase.id}}
return self
end
{% end %}
raise ArgumentError.new "Unknown color: #{color}"
end
def fore(@fore : Color) : self
self
end
def fore(fore : UInt8)
@fore = Color256.new(fore)
self
end
def fore(r : UInt8, g : UInt8, b : UInt8)
@fore = ColorRGB.new(r, g, b)
self
end
def back(color : Symbol) : self
{% for name in COLORS %}
if color == :{{name.id}}
@back = ColorANSI::{{name.camelcase.id}}
return self
end
{% end %}
raise ArgumentError.new "Unknown color: #{color}"
end
def back(@back : Color) : self
self
end
def back(back : UInt8)
@back = Color256.new(back)
self
end
def back(r : UInt8, g : UInt8, b : UInt8)
@back = ColorRGB.new(r, g, b)
self
end
# Adds *mode* to the text's decorations.
def mode(mode : Mode) : self
@mode |= mode
self
end
def on(color : Symbol)
back color
end
# Enables or disables colors and text decoration on this object.
def toggle(flag)
@enabled = !!flag
self
end
# Appends this object colored and with text decoration to *io*.
def to_s(io : IO) : Nil
surround(io) do
io << @object
end
end
# Inspects this object and makes the ANSI escape codes visible.
def inspect(io : IO) : Nil
surround(io) do
@object.inspect(io)
end
end
# Surrounds *io* by the ANSI escape codes and lets you build colored strings:
#
# ```
# require "colorize"
#
# io = IO::Memory.new
#
# Colorize.with.red.surround(io) do
# io << "colorful"
# Colorize.with.green.bold.surround(io) do
# io << " hello "
# end
# Colorize.with.blue.surround(io) do
# io << "world"
# end
# io << " string"
# end
#
# io.to_s # returns a colorful string where "colorful" is red, "hello" green, "world" blue and " string" red again
# ```
def surround(io = STDOUT, &)
return yield io unless @enabled
Object.surround(io, to_named_tuple) do |io|
yield io
end
end
# Prints the ANSI escape codes for an object. Note that this has no effect on a `Colorize::Object` with content,
# only the escape codes.
#
# ```
# require "colorize"
#
# Colorize.with.red.ansi_escape # => "\e[31m"
# "hello world".green.bold.ansi_escape # => "\e[32;1m"
# ```
def ansi_escape : String
String.build do |io|
ansi_escape io
end
end
# Same as `ansi_escape` but writes to a given *io*.
def ansi_escape(io : IO) : Nil
self.class.ansi_escape(io, to_named_tuple)
end
private def to_named_tuple
{
fore: @fore,
back: @back,
mode: @mode,
}
end
@@last_color = {
fore: ColorANSI::Default.as(Color),
back: ColorANSI::Default.as(Color),
mode: Mode::None,
}
protected def self.ansi_escape(io : IO, color : {fore: Color, back: Color, mode: Mode}) : Nil
last_color = @@last_color
append_start(io, color)
@@last_color = last_color
end
protected def self.surround(io, color, &)
last_color = @@last_color
must_append_end = append_start(io, color)
@@last_color = color
begin
yield io
ensure
append_start(io, last_color) if must_append_end
@@last_color = last_color
end
end
private def self.append_start(io, color)
last_color_is_default =
@@last_color[:fore] == ColorANSI::Default &&
@@last_color[:back] == ColorANSI::Default &&
@@last_color[:mode].none?
fore = color[:fore]
back = color[:back]
mode = color[:mode]
fore_is_default = fore == ColorANSI::Default
back_is_default = back == ColorANSI::Default
if fore_is_default && back_is_default && mode.none? && last_color_is_default || @@last_color == color
false
else
io << "\e["
printed = false
unless last_color_is_default
io << '0'
printed = true
end
unless fore_is_default
io << ';' if printed
fore.fore io
printed = true
end
unless back_is_default
io << ';' if printed
back.back io
printed = true
end
each_code(mode) do |code|
io << ';' if printed
io << code
printed = true
end
io << 'm'
true
end
end
end