colorize 0.5.7 → 1.0.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 +7 -0
- data/CHANGELOG.md +76 -0
- data/LICENSE +339 -0
- data/README.md +152 -0
- data/Rakefile +12 -14
- data/colorize.gemspec +30 -42
- data/lib/colorize/class_methods.rb +203 -0
- data/lib/colorize/errors.rb +9 -0
- data/lib/colorize/instance_methods.rb +144 -0
- data/lib/colorize/version.rb +8 -0
- data/lib/colorize.rb +14 -185
- data/lib/colorized_string.rb +25 -0
- data/test/test_colorize.rb +209 -6
- data/test/test_colorized_string.rb +216 -0
- metadata +40 -47
- data/.gitignore +0 -6
- data/CHANGELOG.rdoc +0 -15
- data/README.rdoc +0 -55
- data/VERSION +0 -1
- data/test/test_helper.rb +0 -2
data/colorize.gemspec
CHANGED
|
@@ -1,49 +1,37 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
# -*- encoding: utf-8 -*-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require File.expand_path('lib/colorize/version', File.dirname(__FILE__))
|
|
5
4
|
|
|
6
5
|
Gem::Specification.new do |s|
|
|
7
|
-
s.name =
|
|
8
|
-
s.version =
|
|
6
|
+
s.name = 'colorize'
|
|
7
|
+
s.version = Colorize::VERSION
|
|
8
|
+
s.required_ruby_version = '>= 2.6'
|
|
9
|
+
|
|
10
|
+
s.authors = ['Michał Kalbarczyk']
|
|
11
|
+
s.email = 'fazibear@gmail.com'
|
|
12
|
+
|
|
13
|
+
s.homepage = 'http://github.com/fazibear/colorize'
|
|
14
|
+
s.description = 'Extends String class or add a ColorizedString with methods to set text color, background color and text effects.'
|
|
15
|
+
s.summary = 'Ruby gem for colorizing text using ANSI escape sequences.'
|
|
16
|
+
s.license = 'GPL-2.0'
|
|
17
|
+
|
|
18
|
+
s.require_paths = ['lib']
|
|
9
19
|
|
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
|
-
s.authors = ["fazibear"]
|
|
12
|
-
s.date = %q{2009-11-24}
|
|
13
|
-
s.description = %q{Ruby string class extension. It add some methods to set color, background color and text effect on console easier. Uses ANSI escape sequences.}
|
|
14
|
-
s.email = %q{fazibear@gmail.com}
|
|
15
|
-
s.extra_rdoc_files = [
|
|
16
|
-
"README.rdoc"
|
|
17
|
-
]
|
|
18
20
|
s.files = [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
s.rubygems_version = %q{1.3.5}
|
|
33
|
-
s.summary = %q{Add colors methods to string class}
|
|
34
|
-
s.test_files = [
|
|
35
|
-
"test/test_helper.rb",
|
|
36
|
-
"test/test_colorize.rb"
|
|
21
|
+
'LICENSE',
|
|
22
|
+
'CHANGELOG.md',
|
|
23
|
+
'README.md',
|
|
24
|
+
'Rakefile',
|
|
25
|
+
'colorize.gemspec',
|
|
26
|
+
'lib/colorize.rb',
|
|
27
|
+
'lib/colorized_string.rb',
|
|
28
|
+
'lib/colorize/errors.rb',
|
|
29
|
+
'lib/colorize/class_methods.rb',
|
|
30
|
+
'lib/colorize/instance_methods.rb',
|
|
31
|
+
'lib/colorize/version.rb',
|
|
32
|
+
'test/test_colorize.rb',
|
|
33
|
+
'test/test_colorized_string.rb',
|
|
37
34
|
]
|
|
38
35
|
|
|
39
|
-
|
|
40
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
41
|
-
s.specification_version = 3
|
|
42
|
-
|
|
43
|
-
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
44
|
-
else
|
|
45
|
-
end
|
|
46
|
-
else
|
|
47
|
-
end
|
|
36
|
+
s.metadata['rubygems_mfa_required'] = 'true'
|
|
48
37
|
end
|
|
49
|
-
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Colorize
|
|
4
|
+
module ClassMethods
|
|
5
|
+
@@color_codes ||= {
|
|
6
|
+
black: 30,
|
|
7
|
+
red: 31,
|
|
8
|
+
green: 32,
|
|
9
|
+
yellow: 33,
|
|
10
|
+
blue: 34,
|
|
11
|
+
purple: 35,
|
|
12
|
+
cyan: 36,
|
|
13
|
+
white: 37,
|
|
14
|
+
default: 39,
|
|
15
|
+
light_black: 90,
|
|
16
|
+
light_red: 91,
|
|
17
|
+
light_green: 92,
|
|
18
|
+
light_yellow: 93,
|
|
19
|
+
light_blue: 94,
|
|
20
|
+
light_purple: 95,
|
|
21
|
+
light_cyan: 96,
|
|
22
|
+
light_white: 97
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@@mode_codes ||= {
|
|
26
|
+
default: 0, # Turn off all attributes
|
|
27
|
+
bold: 1,
|
|
28
|
+
dim: 2,
|
|
29
|
+
italic: 3,
|
|
30
|
+
underline: 4,
|
|
31
|
+
blink: 5,
|
|
32
|
+
blink_slow: 5,
|
|
33
|
+
blink_fast: 6,
|
|
34
|
+
invert: 7,
|
|
35
|
+
hide: 8,
|
|
36
|
+
strike: 9,
|
|
37
|
+
double_underline: 20,
|
|
38
|
+
reveal: 28,
|
|
39
|
+
overlined: 53
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
#
|
|
43
|
+
# Property for disable colorization
|
|
44
|
+
#
|
|
45
|
+
def disable_colorization(value = nil)
|
|
46
|
+
if value.nil?
|
|
47
|
+
if defined?(@@disable_colorization)
|
|
48
|
+
@@disable_colorization || false
|
|
49
|
+
else
|
|
50
|
+
false
|
|
51
|
+
end
|
|
52
|
+
else
|
|
53
|
+
@@disable_colorization = (value || false)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
#
|
|
58
|
+
# Setter for disable colorization
|
|
59
|
+
#
|
|
60
|
+
def disable_colorization=(value)
|
|
61
|
+
@@disable_colorization = (value || false)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
#
|
|
65
|
+
# Property for prevent recolorization
|
|
66
|
+
#
|
|
67
|
+
def prevent_colors(value = nil)
|
|
68
|
+
if value.nil?
|
|
69
|
+
if defined?(@@prevent_colors)
|
|
70
|
+
@@prevent_colors || false
|
|
71
|
+
else
|
|
72
|
+
false
|
|
73
|
+
end
|
|
74
|
+
else
|
|
75
|
+
@@prevent_colors = (value || false)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
#
|
|
80
|
+
# Setter for prevent recolorization
|
|
81
|
+
#
|
|
82
|
+
def prevent_colors=(value)
|
|
83
|
+
@@prevent_colors = (value || false)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
#
|
|
87
|
+
# Return array of available colors used by colorize
|
|
88
|
+
#
|
|
89
|
+
def colors
|
|
90
|
+
color_codes.keys
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
#
|
|
94
|
+
# Return array of available modes used by colorize
|
|
95
|
+
#
|
|
96
|
+
def modes
|
|
97
|
+
mode_codes.keys
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
#
|
|
101
|
+
# Display color samples
|
|
102
|
+
#
|
|
103
|
+
def color_samples
|
|
104
|
+
colors.permutation(2).each do |background, color|
|
|
105
|
+
sample_text = "#{color.inspect.rjust(15)} on #{background.inspect.ljust(15)}"
|
|
106
|
+
puts "#{new(sample_text).colorize(:color => color, :background => background)} #{sample_text}"
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
#
|
|
111
|
+
# Add color alias
|
|
112
|
+
#
|
|
113
|
+
def add_color_alias(*params)
|
|
114
|
+
parse_color_alias_params(params).each do |_alias_, _color_|
|
|
115
|
+
check_if_color_available!(_alias_)
|
|
116
|
+
check_if_color_exist!(_color_)
|
|
117
|
+
|
|
118
|
+
add_color_code(_alias_, color_codes[_color_])
|
|
119
|
+
add_color_method(_alias_)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# private
|
|
124
|
+
|
|
125
|
+
#
|
|
126
|
+
# Color codes hash
|
|
127
|
+
#
|
|
128
|
+
def color_codes
|
|
129
|
+
@@color_codes
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def add_color_code(code, color)
|
|
133
|
+
@@color_codes[code] = color
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
#
|
|
137
|
+
# Mode codes hash
|
|
138
|
+
#
|
|
139
|
+
def mode_codes
|
|
140
|
+
@@mode_codes
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
#
|
|
144
|
+
# Generate color and on_color methods
|
|
145
|
+
#
|
|
146
|
+
def color_methods
|
|
147
|
+
colors.each do |key|
|
|
148
|
+
next if key == :default
|
|
149
|
+
|
|
150
|
+
add_color_method(key)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
#
|
|
155
|
+
# Generate color and on_color method
|
|
156
|
+
#
|
|
157
|
+
def add_color_method(key)
|
|
158
|
+
define_method key do
|
|
159
|
+
colorize(:color => key)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
define_method "on_#{key}" do
|
|
163
|
+
colorize(:background => key)
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
#
|
|
168
|
+
# Generate modes methods
|
|
169
|
+
#
|
|
170
|
+
def modes_methods
|
|
171
|
+
modes.each do |key|
|
|
172
|
+
next if key == :default
|
|
173
|
+
|
|
174
|
+
define_method key do
|
|
175
|
+
colorize(:mode => key)
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def parse_color_alias_params(params)
|
|
181
|
+
return [params] if params.is_a?(Array) && params.length == 2
|
|
182
|
+
|
|
183
|
+
params.flat_map do |param|
|
|
184
|
+
next param if param.is_a?(Array) && param.length == 2
|
|
185
|
+
next param.to_a if param.is_a?(Hash)
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
#
|
|
190
|
+
# Check if color exists
|
|
191
|
+
#
|
|
192
|
+
def check_if_color_available!(color)
|
|
193
|
+
color_codes[color] && fail(::Colorize::ColorAlreadyExist, "Colorize: color named :#{color} already exist!")
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
#
|
|
197
|
+
# Check if color is missing
|
|
198
|
+
#
|
|
199
|
+
def check_if_color_exist!(color)
|
|
200
|
+
color_codes[color] || fail(::Colorize::ColorDontExist, "Colorize: color :#{color} don't exist!")
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
end
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Colorize
|
|
4
|
+
module InstanceMethods
|
|
5
|
+
#
|
|
6
|
+
# Change color of string
|
|
7
|
+
#
|
|
8
|
+
# Examples:
|
|
9
|
+
#
|
|
10
|
+
# puts "This is blue".colorize(:blue)
|
|
11
|
+
# puts "This is light blue".colorize(:light_blue)
|
|
12
|
+
# puts "This is also blue".colorize(:color => :blue)
|
|
13
|
+
# puts "This is light blue with red background".colorize(:color => :light_blue, :background => :red)
|
|
14
|
+
# puts "This is light blue with red background".colorize(:light_blue ).colorize( :background => :red)
|
|
15
|
+
# puts "This is blue text on red".blue.on_red
|
|
16
|
+
# puts "This is red on blue".colorize(:red).on_blue
|
|
17
|
+
# puts "This is red on blue and underline".colorize(:red).on_blue.underline
|
|
18
|
+
# puts "This is blue text on red".blue.on_red.blink
|
|
19
|
+
# puts "This is uncolorized".blue.on_red.uncolorize
|
|
20
|
+
#
|
|
21
|
+
def colorize(params)
|
|
22
|
+
return self if self.class.disable_colorization
|
|
23
|
+
|
|
24
|
+
scan_for_colors.inject(self.class.new) do |str, match|
|
|
25
|
+
colors_from_params(match, params)
|
|
26
|
+
defaults_colors(match)
|
|
27
|
+
str << "\001\033[#{match[0]};#{match[1]};#{match[2]}m\002#{match[3]}\001\033[0m\002"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
#
|
|
32
|
+
# Return uncolorized string
|
|
33
|
+
#
|
|
34
|
+
def uncolorize
|
|
35
|
+
scan_for_colors.inject(self.class.new) do |str, match|
|
|
36
|
+
str << match[3]
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
#
|
|
41
|
+
# Return true if string is colorized
|
|
42
|
+
#
|
|
43
|
+
def colorized?
|
|
44
|
+
scan_for_colors.inject([]) do |colors, match|
|
|
45
|
+
colors << match.tap(&:pop)
|
|
46
|
+
end.flatten.compact.any?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
#
|
|
52
|
+
# Set default colors
|
|
53
|
+
#
|
|
54
|
+
def defaults_colors(match)
|
|
55
|
+
match[0] ||= mode(:default)
|
|
56
|
+
match[1] ||= color(:default)
|
|
57
|
+
match[2] ||= background_color(:default)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
#
|
|
61
|
+
# Set color from params
|
|
62
|
+
#
|
|
63
|
+
def colors_from_params(match, params)
|
|
64
|
+
case params
|
|
65
|
+
when Hash then colors_from_hash(match, params)
|
|
66
|
+
when Symbol then color_from_symbol(match, params)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
#
|
|
71
|
+
# Set colors from params hash
|
|
72
|
+
#
|
|
73
|
+
def colors_from_hash(match, hash)
|
|
74
|
+
if self.class.prevent_colors
|
|
75
|
+
match[0] ||= mode(hash[:mode]) if mode(hash[:mode])
|
|
76
|
+
match[1] ||= color(hash[:color]) if color(hash[:color])
|
|
77
|
+
match[2] ||= background_color(hash[:background]) if background_color(hash[:background])
|
|
78
|
+
else
|
|
79
|
+
match[0] = mode(hash[:mode]) if mode(hash[:mode])
|
|
80
|
+
match[1] = color(hash[:color]) if color(hash[:color])
|
|
81
|
+
match[2] = background_color(hash[:background]) if background_color(hash[:background])
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
#
|
|
86
|
+
# Set color from params symbol
|
|
87
|
+
#
|
|
88
|
+
def color_from_symbol(match, symbol)
|
|
89
|
+
if self.class.prevent_colors && color(symbol)
|
|
90
|
+
match[1] ||= color(symbol)
|
|
91
|
+
else
|
|
92
|
+
match[1] = color(symbol)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
#
|
|
97
|
+
# Color for foreground
|
|
98
|
+
#
|
|
99
|
+
def color(color)
|
|
100
|
+
self.class.color_codes[color]
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
#
|
|
104
|
+
# Color for background (offset 10)
|
|
105
|
+
#
|
|
106
|
+
def background_color(color)
|
|
107
|
+
self.class.color_codes[color] + 10 if self.class.color_codes[color]
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
#
|
|
111
|
+
# Mode
|
|
112
|
+
#
|
|
113
|
+
def mode(mode)
|
|
114
|
+
self.class.mode_codes[mode]
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
#
|
|
118
|
+
# Scan for colorized string
|
|
119
|
+
#
|
|
120
|
+
def scan_for_colors
|
|
121
|
+
scan(/\001?\033\[([0-9;]+)m\002?(.+?)\001?\033\[0m\002?|([^\001?\033]+)/m).map do |match|
|
|
122
|
+
split_colors(match)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def split_colors(match)
|
|
127
|
+
colors = (match[0] || '').split(';')
|
|
128
|
+
array = Array.new(3)
|
|
129
|
+
array[0], array[1], array[2] = colors if colors.length == 3
|
|
130
|
+
array[1] = colors if colors.length == 1
|
|
131
|
+
array[3] = match[1] || match[2]
|
|
132
|
+
array
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
#
|
|
136
|
+
# Require windows libs
|
|
137
|
+
#
|
|
138
|
+
def require_windows_libs
|
|
139
|
+
require 'Win32/Console/ANSI' if RUBY_VERSION < '2.0.0' && RUBY_PLATFORM.include?('win32')
|
|
140
|
+
rescue LoadError
|
|
141
|
+
raise 'You must gem install win32console to use colorize on Windows'
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
data/lib/colorize.rb
CHANGED
|
@@ -1,191 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require File.expand_path('colorize/version', File.dirname(__FILE__))
|
|
4
|
+
require File.expand_path('colorize/errors', File.dirname(__FILE__))
|
|
5
|
+
require File.expand_path('colorize/class_methods', File.dirname(__FILE__))
|
|
6
|
+
require File.expand_path('colorize/instance_methods', File.dirname(__FILE__))
|
|
7
|
+
|
|
1
8
|
#
|
|
2
|
-
#
|
|
9
|
+
# String class extension.
|
|
3
10
|
#
|
|
4
11
|
class String
|
|
12
|
+
extend Colorize::ClassMethods
|
|
13
|
+
include Colorize::InstanceMethods
|
|
5
14
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
#
|
|
9
|
-
COLORS = {
|
|
10
|
-
:black => 0,
|
|
11
|
-
:red => 1,
|
|
12
|
-
:green => 2,
|
|
13
|
-
:yellow => 3,
|
|
14
|
-
:blue => 4,
|
|
15
|
-
:magenta => 5,
|
|
16
|
-
:cyan => 6,
|
|
17
|
-
:white => 7,
|
|
18
|
-
:default => 9,
|
|
19
|
-
|
|
20
|
-
:light_black => 10,
|
|
21
|
-
:light_red => 11,
|
|
22
|
-
:light_green => 12,
|
|
23
|
-
:light_yellow => 13,
|
|
24
|
-
:light_blue => 14,
|
|
25
|
-
:light_magenta => 15,
|
|
26
|
-
:light_cyan => 16,
|
|
27
|
-
:light_white => 17
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
#
|
|
31
|
-
# Modes Hash
|
|
32
|
-
#
|
|
33
|
-
MODES = {
|
|
34
|
-
:default => 0, # Turn off all attributes
|
|
35
|
-
#:bright => 1, # Set bright mode
|
|
36
|
-
:underline => 4, # Set underline mode
|
|
37
|
-
:blink => 5, # Set blink mode
|
|
38
|
-
:swap => 7, # Exchange foreground and background colors
|
|
39
|
-
:hide => 8 # Hide text (foreground color would be the same as background)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
protected
|
|
43
|
-
|
|
44
|
-
#
|
|
45
|
-
# Set color values in new string intance
|
|
46
|
-
#
|
|
47
|
-
def set_color_parameters( params )
|
|
48
|
-
if (params.instance_of?(Hash))
|
|
49
|
-
@color = params[:color]
|
|
50
|
-
@background = params[:background]
|
|
51
|
-
@mode = params[:mode]
|
|
52
|
-
@uncolorized = params[:uncolorized]
|
|
53
|
-
self
|
|
54
|
-
else
|
|
55
|
-
nil
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
public
|
|
60
|
-
|
|
61
|
-
#
|
|
62
|
-
# Change color of string
|
|
63
|
-
#
|
|
64
|
-
# Examples:
|
|
65
|
-
#
|
|
66
|
-
# puts "This is blue".colorize( :blue )
|
|
67
|
-
# puts "This is light blue".colorize( :light_blue )
|
|
68
|
-
# puts "This is also blue".colorize( :color => :blue )
|
|
69
|
-
# puts "This is blue with red background".colorize( :color => :light_blue, :background => :red )
|
|
70
|
-
# puts "This is blue with red background".colorize( :light_blue ).colorize( :background => :red )
|
|
71
|
-
# puts "This is blue text on red".blue.on_red
|
|
72
|
-
# puts "This is red on blue".colorize( :red ).on_blue
|
|
73
|
-
# puts "This is red on blue and underline".colorize( :red ).on_blue.underline
|
|
74
|
-
# puts "This is blue text on red".blue.on_red.blink
|
|
75
|
-
#
|
|
76
|
-
def colorize( params )
|
|
77
|
-
|
|
78
|
-
return self unless STDOUT.isatty
|
|
79
|
-
|
|
80
|
-
begin
|
|
81
|
-
require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
|
|
82
|
-
rescue LoadError
|
|
83
|
-
raise 'You must gem install win32console to use color on Windows'
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
color_parameters = {}
|
|
87
|
-
|
|
88
|
-
if (params.instance_of?(Hash))
|
|
89
|
-
color_parameters[:color] = COLORS[params[:color]]
|
|
90
|
-
color_parameters[:background] = COLORS[params[:background]]
|
|
91
|
-
color_parameters[:mode] = MODES[params[:mode]]
|
|
92
|
-
elsif (params.instance_of?(Symbol))
|
|
93
|
-
color_parameters[:color] = COLORS[params]
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
color_parameters[:color] ||= @color || 9
|
|
97
|
-
color_parameters[:background] ||= @background || 9
|
|
98
|
-
color_parameters[:mode] ||= @mode || 0
|
|
99
|
-
|
|
100
|
-
color_parameters[:uncolorized] ||= @uncolorized || self.dup
|
|
101
|
-
|
|
102
|
-
# calculate bright mode
|
|
103
|
-
color_parameters[:color] += 50 if color_parameters[:color] > 10
|
|
104
|
-
|
|
105
|
-
color_parameters[:background] += 50 if color_parameters[:background] > 10
|
|
106
|
-
|
|
107
|
-
return "\033[#{color_parameters[:mode]};#{color_parameters[:color]+30};#{color_parameters[:background]+40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters( color_parameters )
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
#
|
|
112
|
-
# Return uncolorized string
|
|
113
|
-
#
|
|
114
|
-
def uncolorize
|
|
115
|
-
return @uncolorized || self
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
#
|
|
119
|
-
# Return true if sting is colorized
|
|
120
|
-
#
|
|
121
|
-
def colorized?
|
|
122
|
-
return !@uncolorized.nil?
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
#
|
|
126
|
-
# Make some color and on_color methods
|
|
127
|
-
#
|
|
128
|
-
COLORS.each_key do | key |
|
|
129
|
-
eval <<-"end_eval"
|
|
130
|
-
def #{key.to_s}
|
|
131
|
-
return self.colorize( :color => :#{key.to_s} )
|
|
132
|
-
end
|
|
133
|
-
def on_#{key.to_s}
|
|
134
|
-
return self.colorize( :background => :#{key.to_s} )
|
|
135
|
-
end
|
|
136
|
-
end_eval
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
#
|
|
140
|
-
# Methods for modes
|
|
141
|
-
#
|
|
142
|
-
MODES.each_key do | key |
|
|
143
|
-
eval <<-"end_eval"
|
|
144
|
-
def #{key.to_s}
|
|
145
|
-
return self.colorize( :mode => :#{key.to_s} )
|
|
146
|
-
end
|
|
147
|
-
end_eval
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
class << self
|
|
151
|
-
|
|
152
|
-
#
|
|
153
|
-
# Return array of available modes used by colorize method
|
|
154
|
-
#
|
|
155
|
-
def modes
|
|
156
|
-
keys = []
|
|
157
|
-
MODES.each_key do | key |
|
|
158
|
-
keys << key
|
|
159
|
-
end
|
|
160
|
-
keys
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
#
|
|
164
|
-
# Return array of available colors used by colorize method
|
|
165
|
-
#
|
|
166
|
-
def colors
|
|
167
|
-
keys = []
|
|
168
|
-
COLORS.each_key do | key |
|
|
169
|
-
keys << key
|
|
170
|
-
end
|
|
171
|
-
keys
|
|
172
|
-
end
|
|
15
|
+
color_methods
|
|
16
|
+
modes_methods
|
|
173
17
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
#
|
|
177
|
-
def color_matrix( txt = "[X]" )
|
|
178
|
-
size = String.colors.length
|
|
179
|
-
String.colors.each do | color |
|
|
180
|
-
String.colors.each do | back |
|
|
181
|
-
print txt.colorize( :color => color, :background => back )
|
|
182
|
-
end
|
|
183
|
-
puts " < #{color}"
|
|
184
|
-
end
|
|
185
|
-
String.colors.reverse.each_with_index do | back, index |
|
|
186
|
-
puts "#{"|".rjust(txt.length)*(size-index)} < #{back}"
|
|
187
|
-
end
|
|
188
|
-
end
|
|
189
|
-
end
|
|
190
|
-
puts
|
|
18
|
+
add_color_alias(:grey, :light_black)
|
|
19
|
+
add_color_alias(:gray, :light_black)
|
|
191
20
|
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require File.expand_path('colorize/errors', File.dirname(__FILE__))
|
|
4
|
+
require File.expand_path('colorize/class_methods', File.dirname(__FILE__))
|
|
5
|
+
require File.expand_path('colorize/instance_methods', File.dirname(__FILE__))
|
|
6
|
+
#
|
|
7
|
+
# ColorizedString class extension.
|
|
8
|
+
#
|
|
9
|
+
class ColorizedString < String
|
|
10
|
+
extend Colorize::ClassMethods
|
|
11
|
+
include Colorize::InstanceMethods
|
|
12
|
+
|
|
13
|
+
color_methods
|
|
14
|
+
modes_methods
|
|
15
|
+
|
|
16
|
+
add_color_alias(:grey, :light_black)
|
|
17
|
+
add_color_alias(:gray, :light_black)
|
|
18
|
+
|
|
19
|
+
#
|
|
20
|
+
# Shortcut to create ColorizedString with ColorizedString['test'].
|
|
21
|
+
#
|
|
22
|
+
def self.[](string)
|
|
23
|
+
ColorizedString.new(string)
|
|
24
|
+
end
|
|
25
|
+
end
|