-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorable.rb
86 lines (71 loc) · 1.26 KB
/
colorable.rb
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
# frozen_string_literal: true
# Modify output with colors and formatting
module Colorable
FOREGROUND_COLORS = {
bl: 30,
r: 31,
g: 32,
y: 33,
b: 34,
m: 35,
c: 36,
w: 37
}.freeze
BACKGROUND_COLORS = {
bl: 40,
r: 41,
g: 42,
y: 43,
b: 44,
m: 45,
c: 46,
w: 47
}.freeze
COLOR_TO_NUMBER = {
r: 1,
g: 2,
y: 3,
b: 4,
m: 5,
c: 6
}.freeze
NUMBER_TO_COLOR = {
1 => :r,
2 => :g,
3 => :y,
4 => :b,
5 => :m,
6 => :c
}.freeze
SYMBOLS_TO_NAME = {
r: 'Red',
g: 'Green',
y: 'Yellow',
b: 'Blue',
m: 'Magenta',
c: 'Cyan'
}.freeze
KEY_PEG_CHARACTER = "\u29bb"
CODE_PEG_CHARACTER = ' '
SAMPLE_COLORS = %i[r g y b].freeze
KEY_COLORS = proc { |color, _value| %i[bl w].freeze.include? color }
BOLD = 1
def colors(pattern)
pattern.map(&:color)
end
def code_peg_numbers
COLOR_TO_NUMBER.map { |_key, value| value }
end
def code_peg_colors
FOREGROUND_COLORS.each_key.reject(&KEY_COLORS)
end
def key_peg_colors
FOREGROUND_COLORS.each_key.select(&KEY_COLORS)
end
def colorize(color, string)
"\e[#{color}m#{string}\e[0m"
end
def stylize(style, string)
"\e[#{style}m#{string}\e[0m"
end
end