colorize 0.8.1 → 1.0.3
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 +5 -5
- data/CHANGELOG.md +85 -0
- data/README.md +51 -4
- data/Rakefile +2 -0
- data/colorize.gemspec +12 -11
- data/lib/colorize/class_methods.rb +121 -34
- data/lib/colorize/errors.rb +9 -0
- data/lib/colorize/instance_methods.rb +24 -12
- data/lib/colorize/version.rb +8 -0
- data/lib/colorize.rb +8 -0
- data/lib/colorized_string.rb +7 -1
- data/test/test_colorize.rb +114 -64
- data/test/test_colorized_string.rb +216 -0
- metadata +15 -55
- data/CHANGELOG +0 -65
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 71f8aba9c90cbf391acaabc470d94e628d66be6df312605d382cb52b09a939af
|
4
|
+
data.tar.gz: 8efeb99bf7ae768b0d6bc528048238cf0c62f4280e7237ef78163eb1fb026282
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2543e6215f9a15e7f8507e4c9c061aec22f3a2d3f4b4d84949c4805b912728fb9a018a6b3f65fa2aefbdbb878bc1ddc99c6a5e29f83cd8229a5df31edea68c1e
|
7
|
+
data.tar.gz: 98c4349e508062160e5a80221eb686dc6fe32ef8f2b90e438cd741c4bd5ddb8dd9c288fb8e0d556aec7a2d25e0ffc47eeee4731439430fc907e0e1d099f40430
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 1.0.3 / 2023-06-21
|
4
|
+
- fix ? ommit
|
5
|
+
|
6
|
+
## 1.0.2 / 2023-06-21
|
7
|
+
- check if color exists when extending class
|
8
|
+
|
9
|
+
## 1.0.1 / 2023-06-19
|
10
|
+
- fix maching of colorized strings without readline characters
|
11
|
+
|
12
|
+
## 1.0.0 / 2023-06-19
|
13
|
+
- add more modes
|
14
|
+
- add aliases
|
15
|
+
- grey and gray default aliases
|
16
|
+
- update dependencies
|
17
|
+
- add non-visible text delimiters
|
18
|
+
- add prevent_color switch
|
19
|
+
- cleanup and refactor
|
20
|
+
|
21
|
+
## 0.8.1 / 2016-06-29
|
22
|
+
- fix gemspec bug
|
23
|
+
|
24
|
+
## 0.8.0 / 2016-06-27
|
25
|
+
- add ColorizedString class
|
26
|
+
- update README file
|
27
|
+
- add rubocop.yml and follow style
|
28
|
+
- add italic mode
|
29
|
+
- remove interpreter warrnings
|
30
|
+
|
31
|
+
## 0.7.7 / 2015-04-19
|
32
|
+
- update gems
|
33
|
+
|
34
|
+
## 0.7.6 / 2015-04-18
|
35
|
+
- fix bugs
|
36
|
+
|
37
|
+
## 0.7.5 / 2014-12-11
|
38
|
+
- big code refactoring
|
39
|
+
- disable_colorization feature added
|
40
|
+
|
41
|
+
## 0.7.4 / 2014-12-10
|
42
|
+
- code cleanups
|
43
|
+
|
44
|
+
## 0.7.3 / 2014-05-19
|
45
|
+
- fix new line maching
|
46
|
+
|
47
|
+
## 0.7.2 / 2014-04-08
|
48
|
+
- tests cleanups
|
49
|
+
- gem release date fixed
|
50
|
+
|
51
|
+
## 0.7.1 / 2014-04-02
|
52
|
+
- handling wrong color values
|
53
|
+
|
54
|
+
## 0.7.0 / 2014-03-12
|
55
|
+
- refactored to use regexp pattern matching
|
56
|
+
- works with frozen strings
|
57
|
+
- works with concatenated string
|
58
|
+
|
59
|
+
## 0.6.0 / 2013-09-25
|
60
|
+
- code cleanups
|
61
|
+
- bold mode fixed
|
62
|
+
- STDOUT.isatty condition removed
|
63
|
+
- doc updated
|
64
|
+
- jeweler tasks removed
|
65
|
+
- Rakefile updated
|
66
|
+
|
67
|
+
## 0.5.8 / 2009-12-02
|
68
|
+
- code cleanups
|
69
|
+
- removed 'ruby -w' warnings
|
70
|
+
|
71
|
+
## 0.5.7 / 2009-11-24
|
72
|
+
- moved to github/gemcutter
|
73
|
+
- 1.9 compatible
|
74
|
+
|
75
|
+
## 0.5.6 / 2007-08-27
|
76
|
+
- rdocs Allison template
|
77
|
+
- cleanups
|
78
|
+
|
79
|
+
## 0.5.5 / 2007-08-11
|
80
|
+
- added effects methods
|
81
|
+
- README files
|
82
|
+
- new rake file
|
83
|
+
|
84
|
+
## 0.5.0 / 2007-04-22
|
85
|
+
- initial rewritten version
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
colorize [](http://badge.fury.io/rb/colorize) [](http://badge.fury.io/rb/colorize) [](https://github.com/fazibear/colorize/actions/workflows/release.yml) [](https://codeclimate.com/github/fazibear/colorize) [](https://codeclimate.com/github/fazibear/colorize)
|
2
2
|
========
|
3
3
|
|
4
4
|
Ruby gem for colorizing text using ANSI escape sequences.
|
5
|
-
Extends `String` class or add a `ColorizedString` with methods to set text color, background color and text effects.
|
5
|
+
Extends `String` class or add a `ColorizedString` with methods to set the text color, background color and text effects.
|
6
6
|
|
7
7
|
modes
|
8
8
|
-----
|
@@ -16,8 +16,10 @@ features
|
|
16
16
|
* change string color
|
17
17
|
* change string background
|
18
18
|
* change string effect
|
19
|
+
* chain methods
|
19
20
|
* display color samples
|
20
21
|
* disable colorization
|
22
|
+
* prevent colors
|
21
23
|
|
22
24
|
usage
|
23
25
|
-----
|
@@ -29,20 +31,40 @@ String.colors # return array of all possible colors names
|
|
29
31
|
String.modes # return array of all possible modes
|
30
32
|
String.color_samples # displays color samples in all combinations
|
31
33
|
String.disable_colorization # check if colorization is disabled
|
34
|
+
|
35
|
+
# disable colorization (false by default)
|
36
|
+
|
32
37
|
String.disable_colorization = false # enable colorization
|
33
38
|
String.disable_colorization false # enable colorization
|
34
39
|
String.disable_colorization = true # disable colorization
|
35
40
|
String.disable_colorization true # disable colorization
|
36
41
|
|
42
|
+
# prevent colors (false by default)
|
43
|
+
|
44
|
+
String.prevent_colors = false # override current string colors
|
45
|
+
String.prevent_colors false # override current string colors
|
46
|
+
String.prevent_colors = true # don't colorize colorized strings
|
47
|
+
String.prevent_colors true # don't colorize colorized strings
|
48
|
+
|
49
|
+
# adding aliases (:gray and :grey added by default)
|
50
|
+
|
51
|
+
String.add_color_alias(:niebieski, :blue)
|
52
|
+
String.add_color_alias(:zielony => :green)
|
53
|
+
String.add_color_alias(czarny: :black)
|
54
|
+
String.add_color_alias(czerwony: :red, granatowy: :blue)
|
55
|
+
|
56
|
+
# examaples
|
57
|
+
|
37
58
|
puts "This is blue".colorize(:blue)
|
38
59
|
puts "This is light blue".colorize(:light_blue)
|
39
60
|
puts "This is also blue".colorize(:color => :blue)
|
61
|
+
puts "This is bold green".colorize(:color => :green, :mode => :bold)
|
40
62
|
puts "This is light blue with red background".colorize(:color => :light_blue, :background => :red)
|
41
63
|
puts "This is light blue with red background".colorize(:light_blue ).colorize( :background => :red)
|
42
64
|
puts "This is blue text on red".blue.on_red
|
43
65
|
puts "This is red on blue".colorize(:red).on_blue
|
44
66
|
puts "This is red on blue and underline".colorize(:red).on_blue.underline
|
45
|
-
puts "This is blue text on red".blue.on_red.blink
|
67
|
+
puts "This is blinking blue text on red".blue.on_red.blink
|
46
68
|
puts "This is uncolorized".blue.on_red.uncolorize
|
47
69
|
```
|
48
70
|
|
@@ -53,20 +75,40 @@ ColorizedString.colors # return array of all possible colo
|
|
53
75
|
ColorizedString.modes # return array of all possible modes
|
54
76
|
ColorizedString.color_samples # displays color samples in all combinations
|
55
77
|
ColorizedString.disable_colorization # check if colorization is disabled
|
78
|
+
|
79
|
+
# disable colorization (false by default)
|
80
|
+
|
56
81
|
ColorizedString.disable_colorization = false # enable colorization
|
57
82
|
ColorizedString.disable_colorization false # enable colorization
|
58
83
|
ColorizedString.disable_colorization = true # disable colorization
|
59
84
|
ColorizedString.disable_colorization true # disable colorization
|
60
85
|
|
86
|
+
# prevent colors (false by default)
|
87
|
+
|
88
|
+
ColorizedString.prevent_colors = false # override current string colors
|
89
|
+
ColorizedString.prevent_colors false # override current string colors
|
90
|
+
ColorizedString.prevent_colors = true # don't colorize colorized strings
|
91
|
+
ColorizedString.prevent_colors true # don't colorize colorized strings
|
92
|
+
|
93
|
+
# adding aliases (:gray and :grey added by default)
|
94
|
+
|
95
|
+
ColorizedString.add_color_alias(:niebieski, :blue)
|
96
|
+
ColorizedString.add_color_alias(:zielony => :green)
|
97
|
+
ColorizedString.add_color_alias(czarny: :black)
|
98
|
+
ColorizedString.add_color_alias(czerwony: :red, granatowy: :blue)
|
99
|
+
|
100
|
+
# examples
|
101
|
+
|
61
102
|
puts ColorizedString["This is blue"].colorize(:blue)
|
62
103
|
puts ColorizedString["This is light blue"].colorize(:light_blue)
|
63
104
|
puts ColorizedString["This is also blue"].colorize(:color => :blue)
|
105
|
+
puts ColorizedString["This is bold green"].colorize(:color => :green, :mode => :bold)
|
64
106
|
puts ColorizedString["This is light blue with red background"].colorize(:color => :light_blue, :background => :red)
|
65
107
|
puts ColorizedString["This is light blue with red background"].colorize(:light_blue ).colorize( :background => :red)
|
66
108
|
puts ColorizedString["This is blue text on red"].blue.on_red
|
67
109
|
puts ColorizedString["This is red on blue"].colorize(:red).on_blue
|
68
110
|
puts ColorizedString["This is red on blue and underline"].colorize(:red).on_blue.underline
|
69
|
-
puts ColorizedString["This is blue text on red"].blue.on_red.blink
|
111
|
+
puts ColorizedString["This is blinking blue text on red"].blue.on_red.blink
|
70
112
|
puts ColorizedString["This is uncolorized"].blue.on_red.uncolorize
|
71
113
|
|
72
114
|
puts ColorizedString.new("This is blue").blue
|
@@ -85,6 +127,11 @@ install
|
|
85
127
|
|
86
128
|
*Note:* You may need to use sudo to install gems
|
87
129
|
|
130
|
+
thank you
|
131
|
+
---------
|
132
|
+
|
133
|
+
[](https://www.patreon.com/bePatron?u=6912974)
|
134
|
+
|
88
135
|
license
|
89
136
|
-------
|
90
137
|
|
data/Rakefile
CHANGED
data/colorize.gemspec
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.expand_path('lib/colorize/version', File.dirname(__FILE__))
|
4
|
+
|
1
5
|
Gem::Specification.new do |s|
|
2
6
|
s.name = 'colorize'
|
3
|
-
s.version =
|
7
|
+
s.version = Colorize::VERSION
|
8
|
+
s.required_ruby_version = '>= 2.6'
|
4
9
|
|
5
10
|
s.authors = ['Michał Kalbarczyk']
|
6
11
|
s.email = 'fazibear@gmail.com'
|
7
12
|
|
8
|
-
s.date = Time.now.strftime('%Y-%m-%d')
|
9
|
-
|
10
13
|
s.homepage = 'http://github.com/fazibear/colorize'
|
11
14
|
s.description = 'Extends String class or add a ColorizedString with methods to set text color, background color and text effects.'
|
12
15
|
s.summary = 'Ruby gem for colorizing text using ANSI escape sequences.'
|
@@ -14,23 +17,21 @@ Gem::Specification.new do |s|
|
|
14
17
|
|
15
18
|
s.require_paths = ['lib']
|
16
19
|
|
17
|
-
s.add_development_dependency 'rake', '~> 10.0'
|
18
|
-
s.add_development_dependency 'minitest', '~> 5.0'
|
19
|
-
s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
|
20
|
-
|
21
20
|
s.files = [
|
22
21
|
'LICENSE',
|
23
|
-
'CHANGELOG',
|
22
|
+
'CHANGELOG.md',
|
24
23
|
'README.md',
|
25
24
|
'Rakefile',
|
26
25
|
'colorize.gemspec',
|
27
26
|
'lib/colorize.rb',
|
28
27
|
'lib/colorized_string.rb',
|
28
|
+
'lib/colorize/errors.rb',
|
29
29
|
'lib/colorize/class_methods.rb',
|
30
30
|
'lib/colorize/instance_methods.rb',
|
31
|
+
'lib/colorize/version.rb',
|
31
32
|
'test/test_colorize.rb',
|
33
|
+
'test/test_colorized_string.rb',
|
32
34
|
]
|
33
|
-
|
34
|
-
|
35
|
-
]
|
35
|
+
|
36
|
+
s.metadata['rubygems_mfa_required'] = 'true'
|
36
37
|
end
|
@@ -1,17 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Colorize
|
2
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
|
+
|
3
42
|
#
|
4
|
-
# Property
|
43
|
+
# Property for disable colorization
|
5
44
|
#
|
6
45
|
def disable_colorization(value = nil)
|
7
46
|
if value.nil?
|
8
|
-
if defined?(
|
9
|
-
|
47
|
+
if defined?(@@disable_colorization)
|
48
|
+
@@disable_colorization || false
|
10
49
|
else
|
11
50
|
false
|
12
51
|
end
|
13
52
|
else
|
14
|
-
|
53
|
+
@@disable_colorization = (value || false)
|
15
54
|
end
|
16
55
|
end
|
17
56
|
|
@@ -19,7 +58,29 @@ module Colorize
|
|
19
58
|
# Setter for disable colorization
|
20
59
|
#
|
21
60
|
def disable_colorization=(value)
|
22
|
-
|
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)
|
23
84
|
end
|
24
85
|
|
25
86
|
#
|
@@ -47,10 +108,16 @@ module Colorize
|
|
47
108
|
end
|
48
109
|
|
49
110
|
#
|
50
|
-
#
|
111
|
+
# Add color alias
|
51
112
|
#
|
52
|
-
def
|
53
|
-
|
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
|
54
121
|
end
|
55
122
|
|
56
123
|
# private
|
@@ -59,32 +126,18 @@ module Colorize
|
|
59
126
|
# Color codes hash
|
60
127
|
#
|
61
128
|
def color_codes
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
:blue => 4, :light_blue => 64,
|
68
|
-
:magenta => 5, :light_magenta => 65,
|
69
|
-
:cyan => 6, :light_cyan => 66,
|
70
|
-
:white => 7, :light_white => 67,
|
71
|
-
:default => 9
|
72
|
-
}
|
129
|
+
@@color_codes
|
130
|
+
end
|
131
|
+
|
132
|
+
def add_color_code(code, color)
|
133
|
+
@@color_codes[code] = color
|
73
134
|
end
|
74
135
|
|
75
136
|
#
|
76
137
|
# Mode codes hash
|
77
138
|
#
|
78
139
|
def mode_codes
|
79
|
-
|
80
|
-
:default => 0, # Turn off all attributes
|
81
|
-
:bold => 1, # Set bold mode
|
82
|
-
:italic => 3, # Set italic mode
|
83
|
-
:underline => 4, # Set underline mode
|
84
|
-
:blink => 5, # Set blink mode
|
85
|
-
:swap => 7, # Exchange foreground and background colors
|
86
|
-
:hide => 8 # Hide text (foreground color would be the same as background)
|
87
|
-
}
|
140
|
+
@@mode_codes
|
88
141
|
end
|
89
142
|
|
90
143
|
#
|
@@ -94,13 +147,20 @@ module Colorize
|
|
94
147
|
colors.each do |key|
|
95
148
|
next if key == :default
|
96
149
|
|
97
|
-
|
98
|
-
|
99
|
-
|
150
|
+
add_color_method(key)
|
151
|
+
end
|
152
|
+
end
|
100
153
|
|
101
|
-
|
102
|
-
|
103
|
-
|
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)
|
104
164
|
end
|
105
165
|
end
|
106
166
|
|
@@ -116,5 +176,32 @@ module Colorize
|
|
116
176
|
end
|
117
177
|
end
|
118
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_exist?(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_exist?(color) || fail(::Colorize::ColorDontExist, "Colorize: color :#{color} don't exist!")
|
201
|
+
end
|
202
|
+
|
203
|
+
def color_exist?(color)
|
204
|
+
!color_codes[color].nil?
|
205
|
+
end
|
119
206
|
end
|
120
207
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Colorize
|
2
4
|
module InstanceMethods
|
3
5
|
#
|
@@ -18,11 +20,11 @@ module Colorize
|
|
18
20
|
#
|
19
21
|
def colorize(params)
|
20
22
|
return self if self.class.disable_colorization
|
21
|
-
|
23
|
+
|
22
24
|
scan_for_colors.inject(self.class.new) do |str, match|
|
23
25
|
colors_from_params(match, params)
|
24
26
|
defaults_colors(match)
|
25
|
-
str << "\033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\033[0m"
|
27
|
+
str << "\001\033[#{match[0]};#{match[1]};#{match[2]}m\002#{match[3]}\001\033[0m\002"
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
@@ -69,30 +71,40 @@ module Colorize
|
|
69
71
|
# Set colors from params hash
|
70
72
|
#
|
71
73
|
def colors_from_hash(match, hash)
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
75
83
|
end
|
76
84
|
|
77
85
|
#
|
78
86
|
# Set color from params symbol
|
79
87
|
#
|
80
88
|
def color_from_symbol(match, symbol)
|
81
|
-
|
89
|
+
if self.class.prevent_colors && color(symbol)
|
90
|
+
match[1] ||= color(symbol)
|
91
|
+
else
|
92
|
+
match[1] = color(symbol)
|
93
|
+
end
|
82
94
|
end
|
83
95
|
|
84
96
|
#
|
85
|
-
# Color for foreground
|
97
|
+
# Color for foreground
|
86
98
|
#
|
87
99
|
def color(color)
|
88
|
-
self.class.color_codes[color]
|
100
|
+
self.class.color_codes[color]
|
89
101
|
end
|
90
102
|
|
91
103
|
#
|
92
|
-
# Color for background (offset
|
104
|
+
# Color for background (offset 10)
|
93
105
|
#
|
94
106
|
def background_color(color)
|
95
|
-
self.class.color_codes[color] +
|
107
|
+
self.class.color_codes[color] + 10 if self.class.color_codes[color]
|
96
108
|
end
|
97
109
|
|
98
110
|
#
|
@@ -106,7 +118,7 @@ module Colorize
|
|
106
118
|
# Scan for colorized string
|
107
119
|
#
|
108
120
|
def scan_for_colors
|
109
|
-
scan(/\033\[([0-9;]+)m(.+?)\033\[0m
|
121
|
+
scan(/\001?\033\[([0-9;]+)m\002?(.+?)\001?\033\[0m\002?|([^\001\033]+)/m).map do |match|
|
110
122
|
split_colors(match)
|
111
123
|
end
|
112
124
|
end
|
@@ -124,7 +136,7 @@ module Colorize
|
|
124
136
|
# Require windows libs
|
125
137
|
#
|
126
138
|
def require_windows_libs
|
127
|
-
require 'Win32/Console/ANSI' if RUBY_VERSION < '2.0.0' && RUBY_PLATFORM
|
139
|
+
require 'Win32/Console/ANSI' if RUBY_VERSION < '2.0.0' && RUBY_PLATFORM.include?('win32')
|
128
140
|
rescue LoadError
|
129
141
|
raise 'You must gem install win32console to use colorize on Windows'
|
130
142
|
end
|
data/lib/colorize.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
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__))
|
1
5
|
require File.expand_path('colorize/class_methods', File.dirname(__FILE__))
|
2
6
|
require File.expand_path('colorize/instance_methods', File.dirname(__FILE__))
|
7
|
+
|
3
8
|
#
|
4
9
|
# String class extension.
|
5
10
|
#
|
@@ -9,4 +14,7 @@ class String
|
|
9
14
|
|
10
15
|
color_methods
|
11
16
|
modes_methods
|
17
|
+
|
18
|
+
add_color_alias(:grey, :light_black) unless color_exist?(:grey)
|
19
|
+
add_color_alias(:gray, :light_black) unless color_exist?(:gray)
|
12
20
|
end
|
data/lib/colorized_string.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.expand_path('colorize/errors', File.dirname(__FILE__))
|
1
4
|
require File.expand_path('colorize/class_methods', File.dirname(__FILE__))
|
2
5
|
require File.expand_path('colorize/instance_methods', File.dirname(__FILE__))
|
3
6
|
#
|
@@ -10,8 +13,11 @@ class ColorizedString < String
|
|
10
13
|
color_methods
|
11
14
|
modes_methods
|
12
15
|
|
16
|
+
add_color_alias(:grey, :light_black) unless color_exist?(:grey)
|
17
|
+
add_color_alias(:gray, :light_black) unless color_exist?(:gray)
|
18
|
+
|
13
19
|
#
|
14
|
-
#
|
20
|
+
# Shortcut to create ColorizedString with ColorizedString['test'].
|
15
21
|
#
|
16
22
|
def self.[](string)
|
17
23
|
ColorizedString.new(string)
|
data/test/test_colorize.rb
CHANGED
@@ -1,153 +1,128 @@
|
|
1
|
-
|
2
|
-
CodeClimate::TestReporter.start
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
3
|
require 'minitest/autorun'
|
5
|
-
require File.dirname(__FILE__)
|
4
|
+
require "#{File.dirname(__FILE__)}/../lib/colorize"
|
6
5
|
|
7
6
|
class TestColorize < Minitest::Test
|
8
7
|
def test_blue_symbol
|
9
|
-
assert_equal 'This is blue'.colorize(:blue)
|
10
|
-
"\e[0;34;49mThis is blue\e[0m"
|
8
|
+
assert_equal "\001\033[0;34;49m\002This is blue\001\033[0m\002", 'This is blue'.colorize(:blue)
|
11
9
|
end
|
12
10
|
|
13
11
|
def test_incorrect_symbol
|
14
|
-
assert_equal 'This is incorrect color'.colorize(:bold)
|
15
|
-
"\e[0;39;49mThis is incorrect color\e[0m"
|
12
|
+
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:bold)
|
16
13
|
end
|
17
14
|
|
18
15
|
def test_incorrect_hash
|
19
|
-
assert_equal 'This is incorrect color'.colorize(:color => :bold)
|
20
|
-
"\e[0;39;49mThis is incorrect color\e[0m"
|
16
|
+
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:color => :bold)
|
21
17
|
|
22
|
-
assert_equal 'This is incorrect color'.colorize(:mode => :green)
|
23
|
-
"\e[0;39;49mThis is incorrect color\e[0m"
|
18
|
+
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:mode => :green)
|
24
19
|
|
25
|
-
assert_equal 'This is incorrect color'.colorize(:background => :bold)
|
26
|
-
"\e[0;39;49mThis is incorrect color\e[0m"
|
20
|
+
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:background => :bold)
|
27
21
|
end
|
28
22
|
|
29
23
|
def test_blue_hash
|
30
|
-
assert_equal 'This is also blue'.colorize(:color => :blue)
|
31
|
-
"\e[0;34;49mThis is also blue\e[0m"
|
24
|
+
assert_equal "\001\033[0;34;49m\002This is also blue?\001\033[0m\002", 'This is also blue?'.colorize(:color => :blue)
|
32
25
|
end
|
33
26
|
|
34
27
|
def test_light_blue_symbol
|
35
|
-
assert_equal 'This is light blue'.colorize(:light_blue)
|
36
|
-
"\e[0;94;49mThis is light blue\e[0m"
|
28
|
+
assert_equal "\001\033[0;94;49m\002This is light blue\001\033[0m\002", 'This is light blue'.colorize(:light_blue)
|
37
29
|
end
|
38
30
|
|
39
31
|
def test_light_blue_with_red_background_hash
|
40
|
-
assert_equal 'This is light blue with red background'.colorize(:color => :light_blue, :background => :red)
|
41
|
-
"\e[0;94;41mThis is light blue with red background\e[0m"
|
32
|
+
assert_equal "\001\033[0;94;41m\002This is light blue with red background\001\033[0m\002", 'This is light blue with red background'.colorize(:color => :light_blue, :background => :red)
|
42
33
|
end
|
43
34
|
|
44
35
|
def test_light_blue_with_red_background_symbol_and_hash
|
45
|
-
assert_equal 'This is light blue with red background'.colorize(:light_blue).colorize(:background => :red)
|
46
|
-
"\e[0;94;41mThis is light blue with red background\e[0m"
|
36
|
+
assert_equal "\001\033[0;94;41m\002This is light blue with red background\001\033[0m\002", 'This is light blue with red background'.colorize(:light_blue).colorize(:background => :red)
|
47
37
|
end
|
48
38
|
|
49
39
|
def test_blue_with_red_background_method
|
50
|
-
assert_equal 'This is blue text on red'.blue.on_red
|
51
|
-
"\e[0;34;41mThis is blue text on red\e[0m"
|
40
|
+
assert_equal "\001\033[0;34;41m\002This is blue text on red\001\033[0m\002", 'This is blue text on red'.blue.on_red
|
52
41
|
end
|
53
42
|
|
54
43
|
def test_red_with_blue_background_symbol_and_method
|
55
|
-
assert_equal 'This is red on blue'.colorize(:red).on_blue
|
56
|
-
"\e[0;31;44mThis is red on blue\e[0m"
|
44
|
+
assert_equal "\001\033[0;31;44m\002This is red on blue\001\033[0m\002", 'This is red on blue'.colorize(:red).on_blue
|
57
45
|
end
|
58
46
|
|
59
|
-
def
|
60
|
-
assert_equal 'This is red on blue and underline'.colorize(:red).on_blue.underline
|
61
|
-
"\e[4;31;44mThis is red on blue and underline\e[0m"
|
47
|
+
def test_red_with_blue_background_and_underline_symbol_and_methods
|
48
|
+
assert_equal "\001\033[4;31;44m\002This is red on blue and underline\001\033[0m\002", 'This is red on blue and underline'.colorize(:red).on_blue.underline
|
62
49
|
end
|
63
50
|
|
64
51
|
def test_blue_with_red_background_and_blink_methods
|
65
|
-
assert_equal 'This is blue text on red'.blue.on_red.blink
|
66
|
-
"\e[5;34;41mThis is blue text on red\e[0m"
|
52
|
+
assert_equal "\001\033[5;34;41m\002This is blue text on red\001\033[0m\002", 'This is blue text on red'.blue.on_red.blink
|
67
53
|
end
|
68
54
|
|
69
55
|
def test_uncolorize
|
70
|
-
assert_equal 'This is uncolorized'.blue.on_red.uncolorize
|
71
|
-
'This is uncolorized'
|
56
|
+
assert_equal 'This is uncolorized', 'This is uncolorized'.blue.on_red.uncolorize
|
72
57
|
end
|
73
58
|
|
74
59
|
def test_colorized?
|
75
|
-
|
76
|
-
|
77
|
-
|
60
|
+
assert_predicate 'Red'.red, :colorized?
|
61
|
+
refute_predicate 'Blue', :colorized?
|
62
|
+
refute_predicate 'Green'.blue.green.uncolorize, :colorized?
|
78
63
|
end
|
79
64
|
|
80
65
|
def test_concatenated__colorize?
|
81
|
-
|
82
|
-
|
66
|
+
assert_predicate "none #{'red'.red} none #{'blue'.blue} none", :colorized?
|
67
|
+
refute_predicate "none #{'red'.red} none #{'blue'.blue} none".uncolorize, :colorized?
|
83
68
|
end
|
84
69
|
|
85
70
|
def test_concatenated_strings_on_green
|
86
|
-
assert_equal "none #{'red'.red} none #{'blue'.blue} none".on_green
|
87
|
-
"\e[0;39;42mnone \e[0m\e[0;31;42mred\e[0m\e[0;39;42m none \e[0m\e[0;34;42mblue\e[0m\e[0;39;42m none\e[0m"
|
71
|
+
assert_equal "\001\033[0;39;42m\002none \001\033[0m\002\001\033[0;31;42m\002red\001\033[0m\002\001\033[0;39;42m\002 none \001\033[0m\002\001\033[0;34;42m\002blue\001\033[0m\002\001\033[0;39;42m\002 none\001\033[0m\002", "none #{'red'.red} none #{'blue'.blue} none".on_green
|
88
72
|
end
|
89
73
|
|
90
74
|
def test_concatenated_strings_uncolorize
|
91
|
-
assert_equal "none #{'red'.red} none #{'blue'.blue} none".uncolorize
|
92
|
-
'none red none blue none'
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_frozen_strings
|
96
|
-
assert_equal 'This is blue text on red'.freeze.blue.on_red.blink,
|
97
|
-
"\e[5;34;41mThis is blue text on red\e[0m"
|
75
|
+
assert_equal 'none red none blue none', "none #{'red'.red} none #{'blue'.blue} none".uncolorize
|
98
76
|
end
|
99
77
|
|
100
78
|
def test_new_line
|
101
|
-
assert_equal "This is blue\ntext on red".
|
102
|
-
"\e[5;34;41mThis is blue\ntext on red\e[0m"
|
79
|
+
assert_equal "\001\033[5;34;41m\002This is blue\ntext on red\001\033[0m\002", "This is blue\ntext on red".blue.on_red.blink
|
103
80
|
end
|
104
81
|
|
105
82
|
def test_disable_colorization_with_=
|
106
83
|
String.disable_colorization = true
|
107
|
-
|
84
|
+
|
85
|
+
assert String.disable_colorization
|
86
|
+
|
108
87
|
String.disable_colorization = false
|
109
88
|
end
|
110
89
|
|
111
90
|
def test_disable_colorization_with_method
|
112
91
|
String.disable_colorization true
|
113
|
-
|
92
|
+
|
93
|
+
assert String.disable_colorization
|
94
|
+
|
114
95
|
String.disable_colorization false
|
115
96
|
end
|
116
97
|
|
117
98
|
def test_string_disable_colorization_with_=
|
118
99
|
String.disable_colorization = true
|
119
100
|
|
120
|
-
|
101
|
+
assert String.disable_colorization
|
121
102
|
|
122
|
-
assert_equal 'This is blue after disabling'.blue
|
123
|
-
'This is blue after disabling'
|
103
|
+
assert_equal 'This is blue after disabling', 'This is blue after disabling'.blue
|
124
104
|
|
125
105
|
String.disable_colorization = false
|
126
106
|
|
127
|
-
assert_equal 'This is blue after enabling'.colorize(:blue)
|
128
|
-
"\e[0;34;49mThis is blue after enabling\e[0m"
|
107
|
+
assert_equal "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", 'This is blue after enabling'.colorize(:blue)
|
129
108
|
end
|
130
109
|
|
131
110
|
def test_string_disable_colorization_with_method
|
132
|
-
assert_equal 'This is blue before disabling'.colorize(:blue)
|
133
|
-
"\e[0;34;49mThis is blue before disabling\e[0m"
|
111
|
+
assert_equal "\001\033[0;34;49m\002This is blue before disabling\001\033[0m\002", 'This is blue before disabling'.colorize(:blue)
|
134
112
|
|
135
113
|
String.disable_colorization true
|
136
114
|
|
137
|
-
|
115
|
+
assert String.disable_colorization
|
138
116
|
|
139
|
-
assert_equal 'This is blue after disabling'.blue
|
140
|
-
'This is blue after disabling'
|
117
|
+
assert_equal 'This is blue after disabling', 'This is blue after disabling'.blue
|
141
118
|
|
142
119
|
String.disable_colorization false
|
143
120
|
|
144
|
-
assert_equal 'This is blue after enabling'.colorize(:blue)
|
145
|
-
"\e[0;34;49mThis is blue after enabling\e[0m"
|
121
|
+
assert_equal "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", 'This is blue after enabling'.colorize(:blue)
|
146
122
|
end
|
147
123
|
|
148
124
|
def test_already_colored_string_with_one_value
|
149
|
-
assert_equal "\
|
150
|
-
'This is red'.red
|
125
|
+
assert_equal 'This is red'.red, "\001\033[31m\002This is red\001\033[0m\002".red
|
151
126
|
end
|
152
127
|
|
153
128
|
def test_color_matrix_method
|
@@ -161,4 +136,79 @@ class TestColorize < Minitest::Test
|
|
161
136
|
String.color_samples
|
162
137
|
end
|
163
138
|
end
|
139
|
+
|
140
|
+
def test_grey_alias
|
141
|
+
assert_equal 'This is grey'.grey, 'This is grey'.light_black
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_gray_alias
|
145
|
+
assert_equal 'This is gray'.gray, 'This is gray'.light_black
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_add_color_alias
|
149
|
+
String.add_color_alias(:extra_blue, :light_blue)
|
150
|
+
|
151
|
+
assert_equal 'blue'.light_blue, 'blue'.extra_blue
|
152
|
+
assert_equal 'blue'.on_light_blue, 'blue'.on_extra_blue
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_add_color_alias_errors
|
156
|
+
String.add_color_alias(:extra_red, :light_red)
|
157
|
+
|
158
|
+
assert_raises ::Colorize::ColorAlreadyExist, 'Colorize: color :extra_red already exist!' do
|
159
|
+
String.add_color_alias(:extra_red, :light_blue)
|
160
|
+
end
|
161
|
+
|
162
|
+
assert_raises ::Colorize::ColorDontExist, 'Colorize: color :light_color don\'t exist!' do
|
163
|
+
String.add_color_alias(:extra_white, :light_color)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_add_color_alias_with_single_hash
|
168
|
+
String.add_color_alias(extra_green: :light_green)
|
169
|
+
|
170
|
+
assert_equal 'example string'.light_green, 'example string'.extra_green
|
171
|
+
assert_equal 'example string'.on_light_green, 'example string'.on_extra_green
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_add_color_alias_with_single_hash_with_arrow
|
175
|
+
String.add_color_alias(:extra_color => :gray)
|
176
|
+
|
177
|
+
assert_equal 'example string'.gray, 'example string'.extra_color
|
178
|
+
assert_equal 'example string'.on_gray, 'example string'.on_extra_color
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_add_color_alias_with_multi_hash
|
182
|
+
String.add_color_alias(extra_color_a: :gray, extra_color_b: :blue)
|
183
|
+
|
184
|
+
assert_equal 'example string'.gray, 'example string'.extra_color_a
|
185
|
+
assert_equal 'example string'.blue, 'example string'.extra_color_b
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_add_color_alias_with_multi_hash_with_arrow
|
189
|
+
String.add_color_alias(:extra_color_c => :gray, :extra_color_d => :blue)
|
190
|
+
|
191
|
+
assert_equal 'example string'.gray, 'example string'.extra_color_c
|
192
|
+
assert_equal 'example string'.on_blue, 'example string'.on_extra_color_d
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_add_color_alias_with_multi_hash_mixed
|
196
|
+
String.add_color_alias(extra_color_e: :gray, :extra_color_f => :blue)
|
197
|
+
|
198
|
+
assert_equal 'example string'.gray, 'example string'.extra_color_e
|
199
|
+
assert_equal 'example string'.on_blue, 'example string'.on_extra_color_f
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_prevent_colors
|
203
|
+
String.prevent_colors true
|
204
|
+
|
205
|
+
assert String.prevent_colors
|
206
|
+
assert_equal "#{'blue'.blue}#{'red'.red}#{'green'.green}", "#{'blue'.blue}red#{'green'.green}".red
|
207
|
+
|
208
|
+
String.prevent_colors false
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_colorized_string_without_readline
|
212
|
+
assert_equal "\e[0;31;49mgreen\e[0m".green, 'green'.green
|
213
|
+
end
|
164
214
|
end
|
@@ -0,0 +1,216 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require "#{File.dirname(__FILE__)}/../lib/colorized_string"
|
5
|
+
|
6
|
+
class TestColorizedString < Minitest::Test
|
7
|
+
def test_colorize_not_required
|
8
|
+
refute_respond_to '', :colorize, 'Colorize should not be required. Only colorized_string'
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_blue_symbol
|
12
|
+
assert_equal "\001\033[0;34;49m\002This is blue\001\033[0m\002", ColorizedString['This is blue'].colorize(:blue)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_incorrect_symbol
|
16
|
+
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:bold)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_incorrect_hash
|
20
|
+
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:color => :bold)
|
21
|
+
|
22
|
+
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:mode => :green)
|
23
|
+
|
24
|
+
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:background => :bold)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_blue_hash
|
28
|
+
assert_equal "\001\033[0;34;49m\002This is also blue\001\033[0m\002", ColorizedString['This is also blue'].colorize(:color => :blue)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_light_blue_symbol
|
32
|
+
assert_equal "\001\033[0;94;49m\002This is light blue\001\033[0m\002", ColorizedString['This is light blue'].colorize(:light_blue)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_light_blue_with_red_background_hash
|
36
|
+
assert_equal "\001\033[0;94;41m\002This is light blue with red background\001\033[0m\002", ColorizedString['This is light blue with red background'].colorize(:color => :light_blue, :background => :red)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_light_blue_with_red_background_symbol_and_hash
|
40
|
+
assert_equal "\001\033[0;94;41m\002This is light blue with red background\001\033[0m\002", ColorizedString['This is light blue with red background'].colorize(:light_blue).colorize(:background => :red)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_blue_with_red_background_method
|
44
|
+
assert_equal "\001\033[0;34;41m\002This is blue text on red\001\033[0m\002", ColorizedString['This is blue text on red'].blue.on_red
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_red_with_blue_background_symbol_and_method
|
48
|
+
assert_equal "\001\033[0;31;44m\002This is red on blue\001\033[0m\002", ColorizedString['This is red on blue'].colorize(:red).on_blue
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_red_with_blue_background_and_underline_sumbol_and_methods
|
52
|
+
assert_equal "\001\033[4;31;44m\002This is red on blue and underline\001\033[0m\002", ColorizedString['This is red on blue and underline'].colorize(:red).on_blue.underline
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_blue_with_red_background_and_blink_methods
|
56
|
+
assert_equal "\001\033[5;34;41m\002This is blue text on red\001\033[0m\002", ColorizedString['This is blue text on red'].blue.on_red.blink
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_uncolorize
|
60
|
+
assert_equal 'This is uncolorized', ColorizedString['This is uncolorized'].blue.on_red.uncolorize
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_colorized?
|
64
|
+
assert_predicate ColorizedString['Red'].red, :colorized?
|
65
|
+
refute_predicate ColorizedString['Blue'], :colorized?
|
66
|
+
refute_predicate ColorizedString['Green'].blue.green.uncolorize, :colorized?
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_concatenated__colorize?
|
70
|
+
assert_predicate ColorizedString["none #{ColorizedString['red'].red} none #{ColorizedString['blue'].blue} none"], :colorized?
|
71
|
+
refute_predicate ColorizedString["none #{ColorizedString['red'].red} none #{ColorizedString['blue'].blue} none"].uncolorize, :colorized?
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_concatenated_strings_on_green
|
75
|
+
assert_equal "\001\033[0;39;42m\002none \001\033[0m\002\001\033[0;31;42m\002red\001\033[0m\002\001\033[0;39;42m\002 none \001\033[0m\002\001\033[0;34;42m\002blue\001\033[0m\002\001\033[0;39;42m\002 none\001\033[0m\002", ColorizedString["none #{ColorizedString['red'].red} none #{ColorizedString['blue'].blue} none"].on_green
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_concatenated_strings_uncolorize
|
79
|
+
assert_equal 'none red none blue none', ColorizedString["none #{ColorizedString['red'].red} none #{ColorizedString['blue'].blue} none"].uncolorize
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_frozen_strings
|
83
|
+
assert_equal "\001\033[5;34;41m\002This is blue text on red\001\033[0m\002", ColorizedString['This is blue text on red'].freeze.blue.on_red.blink
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_new_line
|
87
|
+
assert_equal "\001\033[5;34;41m\002This is blue\ntext on red\001\033[0m\002", ColorizedString["This is blue\ntext on red"].freeze.blue.on_red.blink
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_disable_colorization_with_=
|
91
|
+
ColorizedString.disable_colorization = true
|
92
|
+
|
93
|
+
assert ColorizedString.disable_colorization
|
94
|
+
|
95
|
+
ColorizedString.disable_colorization = false
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_disable_colorization_with_method
|
99
|
+
ColorizedString.disable_colorization true
|
100
|
+
|
101
|
+
assert ColorizedString.disable_colorization
|
102
|
+
|
103
|
+
ColorizedString.disable_colorization false
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_string_disable_colorization_with_=
|
107
|
+
ColorizedString.disable_colorization = true
|
108
|
+
|
109
|
+
assert ColorizedString.disable_colorization
|
110
|
+
|
111
|
+
assert_equal 'This is blue after disabling', ColorizedString['This is blue after disabling'].blue
|
112
|
+
|
113
|
+
ColorizedString.disable_colorization = false
|
114
|
+
|
115
|
+
assert_equal "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", ColorizedString['This is blue after enabling'].colorize(:blue)
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_string_disable_colorization_with_method
|
119
|
+
assert_equal "\001\033[0;34;49m\002This is blue before disabling\001\033[0m\002", ColorizedString['This is blue before disabling'].colorize(:blue)
|
120
|
+
|
121
|
+
ColorizedString.disable_colorization true
|
122
|
+
|
123
|
+
assert ColorizedString.disable_colorization
|
124
|
+
|
125
|
+
assert_equal 'This is blue after disabling', ColorizedString['This is blue after disabling'].blue
|
126
|
+
|
127
|
+
ColorizedString.disable_colorization false
|
128
|
+
|
129
|
+
assert_equal "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", ColorizedString['This is blue after enabling'].colorize(:blue)
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_already_colored_string_with_one_value
|
133
|
+
assert_equal ColorizedString['This is red'].red, ColorizedString["\001\033[31m\002This is red\001\033[0m\002"].red
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_color_matrix_method
|
137
|
+
assert_raises(NoMethodError) { ColorizedString.color_matrix }
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_color_samples_method
|
141
|
+
assert_output do
|
142
|
+
ColorizedString.color_samples
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_grey_alias
|
147
|
+
assert_equal ColorizedString['This is grey'].grey, ColorizedString['This is grey'].light_black
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_gray_alias
|
151
|
+
assert_equal ColorizedString['This is gray'].gray, ColorizedString['This is gray'].light_black
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_add_color_alias
|
155
|
+
ColorizedString.add_color_alias(:extra_blue, :light_blue)
|
156
|
+
|
157
|
+
assert_equal ColorizedString['blue'].light_blue, ColorizedString['blue'].extra_blue
|
158
|
+
assert_equal ColorizedString['blue'].on_light_blue, ColorizedString['blue'].on_extra_blue
|
159
|
+
|
160
|
+
assert_raises ::Colorize::ColorAlreadyExist, 'Colorize: color :extra_blue already exist!' do
|
161
|
+
ColorizedString.add_color_alias(:extra_blue, :light_color)
|
162
|
+
end
|
163
|
+
|
164
|
+
assert_raises ::Colorize::ColorDontExist, 'Colorize: color :light_color don\'t exist!' do
|
165
|
+
ColorizedString.add_color_alias(:extra_white, :light_color)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_add_color_alias_with_single_hash
|
170
|
+
ColorizedString.add_color_alias(extra_green: :light_green)
|
171
|
+
|
172
|
+
assert_equal ColorizedString['example string'].light_green, ColorizedString['example string'].extra_green
|
173
|
+
assert_equal ColorizedString['example string'].on_light_green, ColorizedString['example string'].on_extra_green
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_add_color_alias_with_single_hash_with_arrow
|
177
|
+
ColorizedString.add_color_alias(:extra_color => :gray)
|
178
|
+
|
179
|
+
assert_equal ColorizedString['example string'].gray, ColorizedString['example string'].extra_color
|
180
|
+
assert_equal ColorizedString['example string'].on_gray, ColorizedString['example string'].on_extra_color
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_add_color_alias_with_multi_hash
|
184
|
+
ColorizedString.add_color_alias(extra_color_a: :gray, extra_color_b: :blue)
|
185
|
+
|
186
|
+
assert_equal ColorizedString['example string'].gray, ColorizedString['example string'].extra_color_a
|
187
|
+
assert_equal ColorizedString['example string'].blue, ColorizedString['example string'].extra_color_b
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_add_color_alias_with_multi_hash_with_arrow
|
191
|
+
ColorizedString.add_color_alias(:extra_color_c => :gray, :extra_color_d => :blue)
|
192
|
+
|
193
|
+
assert_equal ColorizedString['example string'].gray, ColorizedString['example string'].extra_color_c
|
194
|
+
assert_equal ColorizedString['example string'].on_blue, ColorizedString['example string'].on_extra_color_d
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_add_color_alias_with_multi_hash_mixed
|
198
|
+
ColorizedString.add_color_alias(extra_color_e: :gray, :extra_color_f => :blue)
|
199
|
+
|
200
|
+
assert_equal ColorizedString['example string'].gray, ColorizedString['example string'].extra_color_e
|
201
|
+
assert_equal ColorizedString['example string'].on_blue, ColorizedString['example string'].on_extra_color_f
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_prevent_colors
|
205
|
+
ColorizedString.prevent_colors true
|
206
|
+
|
207
|
+
assert ColorizedString.prevent_colors
|
208
|
+
assert_equal "#{ColorizedString['blue'].blue}#{ColorizedString['red'].red}#{ColorizedString['green'].green}", ColorizedString["#{ColorizedString['blue'].blue}red#{ColorizedString['green'].green}"].red
|
209
|
+
|
210
|
+
ColorizedString.prevent_colors false
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_colorized_string_without_readline
|
214
|
+
assert_equal ColorizedString["\e[0;31;49mgreen\e[0m"].green, ColorizedString['green'].green
|
215
|
+
end
|
216
|
+
end
|
metadata
CHANGED
@@ -1,57 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: colorize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michał Kalbarczyk
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: rake
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '10.0'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '10.0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: minitest
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '5.0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '5.0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: codeclimate-test-reporter
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0.4'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0.4'
|
11
|
+
date: 2023-06-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
55
13
|
description: Extends String class or add a ColorizedString with methods to set text
|
56
14
|
color, background color and text effects.
|
57
15
|
email: fazibear@gmail.com
|
@@ -59,21 +17,25 @@ executables: []
|
|
59
17
|
extensions: []
|
60
18
|
extra_rdoc_files: []
|
61
19
|
files:
|
62
|
-
- CHANGELOG
|
20
|
+
- CHANGELOG.md
|
63
21
|
- LICENSE
|
64
22
|
- README.md
|
65
23
|
- Rakefile
|
66
24
|
- colorize.gemspec
|
67
25
|
- lib/colorize.rb
|
68
26
|
- lib/colorize/class_methods.rb
|
27
|
+
- lib/colorize/errors.rb
|
69
28
|
- lib/colorize/instance_methods.rb
|
29
|
+
- lib/colorize/version.rb
|
70
30
|
- lib/colorized_string.rb
|
71
31
|
- test/test_colorize.rb
|
32
|
+
- test/test_colorized_string.rb
|
72
33
|
homepage: http://github.com/fazibear/colorize
|
73
34
|
licenses:
|
74
35
|
- GPL-2.0
|
75
|
-
metadata:
|
76
|
-
|
36
|
+
metadata:
|
37
|
+
rubygems_mfa_required: 'true'
|
38
|
+
post_install_message:
|
77
39
|
rdoc_options: []
|
78
40
|
require_paths:
|
79
41
|
- lib
|
@@ -81,17 +43,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
81
43
|
requirements:
|
82
44
|
- - ">="
|
83
45
|
- !ruby/object:Gem::Version
|
84
|
-
version: '
|
46
|
+
version: '2.6'
|
85
47
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
48
|
requirements:
|
87
49
|
- - ">="
|
88
50
|
- !ruby/object:Gem::Version
|
89
51
|
version: '0'
|
90
52
|
requirements: []
|
91
|
-
|
92
|
-
|
93
|
-
signing_key:
|
53
|
+
rubygems_version: 3.4.14
|
54
|
+
signing_key:
|
94
55
|
specification_version: 4
|
95
56
|
summary: Ruby gem for colorizing text using ANSI escape sequences.
|
96
|
-
test_files:
|
97
|
-
- test/test_colorize.rb
|
57
|
+
test_files: []
|
data/CHANGELOG
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
== 0.8.1 / 2016-06-29
|
2
|
-
* fix gemspec bug
|
3
|
-
|
4
|
-
== 0.8.0 / 2016-06-27
|
5
|
-
* add ColorizedString class
|
6
|
-
* update README file
|
7
|
-
* add rubocop.yml and follow style
|
8
|
-
* add italic mode
|
9
|
-
* remove interpreter warrnings
|
10
|
-
|
11
|
-
== 0.7.7 / 2015-04-19
|
12
|
-
* update gems
|
13
|
-
|
14
|
-
== 0.7.6 / 2015-04-18
|
15
|
-
* fix bugs
|
16
|
-
|
17
|
-
== 0.7.5 / 2014-12-11
|
18
|
-
* big code refactoring
|
19
|
-
* disable_colorization feature added
|
20
|
-
|
21
|
-
== 0.7.4 / 2014-12-10
|
22
|
-
* code cleanups
|
23
|
-
|
24
|
-
== 0.7.3 / 2014-05-19
|
25
|
-
* fix new line maching
|
26
|
-
|
27
|
-
== 0.7.2 / 2014-04-08
|
28
|
-
* tests cleanups
|
29
|
-
* gem release date fixed
|
30
|
-
|
31
|
-
== 0.7.1 / 2014-04-02
|
32
|
-
* handling wrong color values
|
33
|
-
|
34
|
-
== 0.7.0 / 2014-03-12
|
35
|
-
* refactored to use regexp pattern matching
|
36
|
-
* works with frozen strings
|
37
|
-
* works with concatenated string
|
38
|
-
|
39
|
-
== 0.6.0 / 2013-09-25
|
40
|
-
* code cleanups
|
41
|
-
* bold mode fixed
|
42
|
-
* STDOUT.isatty condition removed
|
43
|
-
* doc updated
|
44
|
-
* jeweler tasks removed
|
45
|
-
* Rakefile updated
|
46
|
-
|
47
|
-
== 0.5.8 / 2009-12-02
|
48
|
-
* code cleanups
|
49
|
-
* removed 'ruby -w' warnings
|
50
|
-
|
51
|
-
== 0.5.7 / 2009-11-24
|
52
|
-
* moved to github/gemcutter
|
53
|
-
* 1.9 compatible
|
54
|
-
|
55
|
-
== 0.5.6 / 2007-08-27
|
56
|
-
* rdocs Allison template
|
57
|
-
* cleanups
|
58
|
-
|
59
|
-
== 0.5.5 / 2007-08-11
|
60
|
-
* added effects methods
|
61
|
-
* README files
|
62
|
-
* new rake file
|
63
|
-
|
64
|
-
== 0.5.0 / 2007-04-22
|
65
|
-
* initial rewritten version
|