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
|
# frozen_string_literal: true
module Jekyll
module Utils
module Ansi
extend self
ESCAPE = format("%c", 27)
MATCH = %r!#{ESCAPE}\[(?:\d+)(?:;\d+)*(j|k|m|s|u|A|B|G)|\e\(B\e\[m!ix.freeze
COLORS = {
:red => 31,
:green => 32,
:black => 30,
:magenta => 35,
:yellow => 33,
:white => 37,
:blue => 34,
:cyan => 36,
}.freeze
# Strip ANSI from the current string. It also strips cursor stuff,
# well some of it, and it also strips some other stuff that a lot of
# the other ANSI strippers don't.
def strip(str)
str.gsub MATCH, ""
end
#
def has?(str)
!!(str =~ MATCH)
end
# Reset the color back to the default color so that you do not leak any
# colors when you move onto the next line. This is probably normally
# used as part of a wrapper so that we don't leak colors.
def reset(str = "")
@ansi_reset ||= format("%c[0m", 27)
"#{@ansi_reset}#{str}"
end
# SEE: `self::COLORS` for a list of methods. They are mostly
# standard base colors supported by pretty much any xterm-color, we do
# not need more than the base colors so we do not include them.
# Actually... if I'm honest we don't even need most of the
# base colors.
COLORS.each do |color, num|
define_method color do |str|
"#{format("%c", 27)}[#{num}m#{str}#{reset}"
end
end
end
end
end
|