colorize 0.8.1 → 1.1.0
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 +91 -0
- data/README.md +51 -4
- data/Rakefile +6 -0
- data/colorize.gemspec +12 -11
- data/lib/colorize/class_methods.rb +143 -34
- data/lib/colorize/errors.rb +9 -0
- data/lib/colorize/instance_methods.rb +46 -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: b2258f1a1f28ceb6396b7e29aa308326e02bba9a9f36cd229e70c0e417aacd37
|
4
|
+
data.tar.gz: 65b1cbb6089464e698f2dc8591b7d80af1c4aab479cef11e2fcee3c46428dd20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 879d312bfb7dad3576bbf6e9791c2a6e168bfb8a29db81980238ce2aaaa5456ac1e535709a6c4f4f811aa5d2a22dce5e89cd0e5366f365102a0667e5c7263d4b
|
7
|
+
data.tar.gz: efc22eb11cc908ae015473ac51fc19680b8adcaf6904d6db7c74064e7e3b2a41cdeadca0a45eeb7950780f94db19e472f561cb3b60bd125fc9460ee4807d353a
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 1.1.0 / 2023-06-22
|
4
|
+
- make readline support optional
|
5
|
+
|
6
|
+
## 1.0.5 / 2023-06-22
|
7
|
+
- (backwards compatibility) purple -> magenta
|
8
|
+
|
9
|
+
## 1.0.3 / 2023-06-21
|
10
|
+
- fix ? ommit
|
11
|
+
|
12
|
+
## 1.0.2 / 2023-06-21
|
13
|
+
- check if color exists when extending class
|
14
|
+
|
15
|
+
## 1.0.1 / 2023-06-19
|
16
|
+
- fix maching of colorized strings without readline characters
|
17
|
+
|
18
|
+
## 1.0.0 / 2023-06-19
|
19
|
+
- add more modes
|
20
|
+
- add aliases
|
21
|
+
- grey and gray default aliases
|
22
|
+
- update dependencies
|
23
|
+
- add non-visible text delimiters
|
24
|
+
- add prevent_color switch
|
25
|
+
- cleanup and refactor
|
26
|
+
|
27
|
+
## 0.8.1 / 2016-06-29
|
28
|
+
- fix gemspec bug
|
29
|
+
|
30
|
+
## 0.8.0 / 2016-06-27
|
31
|
+
- add ColorizedString class
|
32
|
+
- update README file
|
33
|
+
- add rubocop.yml and follow style
|
34
|
+
- add italic mode
|
35
|
+
- remove interpreter warrnings
|
36
|
+
|
37
|
+
## 0.7.7 / 2015-04-19
|
38
|
+
- update gems
|
39
|
+
|
40
|
+
## 0.7.6 / 2015-04-18
|
41
|
+
- fix bugs
|
42
|
+
|
43
|
+
## 0.7.5 / 2014-12-11
|
44
|
+
- big code refactoring
|
45
|
+
- disable_colorization feature added
|
46
|
+
|
47
|
+
## 0.7.4 / 2014-12-10
|
48
|
+
- code cleanups
|
49
|
+
|
50
|
+
## 0.7.3 / 2014-05-19
|
51
|
+
- fix new line maching
|
52
|
+
|
53
|
+
## 0.7.2 / 2014-04-08
|
54
|
+
- tests cleanups
|
55
|
+
- gem release date fixed
|
56
|
+
|
57
|
+
## 0.7.1 / 2014-04-02
|
58
|
+
- handling wrong color values
|
59
|
+
|
60
|
+
## 0.7.0 / 2014-03-12
|
61
|
+
- refactored to use regexp pattern matching
|
62
|
+
- works with frozen strings
|
63
|
+
- works with concatenated string
|
64
|
+
|
65
|
+
## 0.6.0 / 2013-09-25
|
66
|
+
- code cleanups
|
67
|
+
- bold mode fixed
|
68
|
+
- STDOUT.isatty condition removed
|
69
|
+
- doc updated
|
70
|
+
- jeweler tasks removed
|
71
|
+
- Rakefile updated
|
72
|
+
|
73
|
+
## 0.5.8 / 2009-12-02
|
74
|
+
- code cleanups
|
75
|
+
- removed 'ruby -w' warnings
|
76
|
+
|
77
|
+
## 0.5.7 / 2009-11-24
|
78
|
+
- moved to github/gemcutter
|
79
|
+
- 1.9 compatible
|
80
|
+
|
81
|
+
## 0.5.6 / 2007-08-27
|
82
|
+
- rdocs Allison template
|
83
|
+
- cleanups
|
84
|
+
|
85
|
+
## 0.5.5 / 2007-08-11
|
86
|
+
- added effects methods
|
87
|
+
- README files
|
88
|
+
- new rake file
|
89
|
+
|
90
|
+
## 0.5.0 / 2007-04-22
|
91
|
+
- 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
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rake/testtask'
|
2
4
|
|
3
5
|
Rake::TestTask.new do |t|
|
@@ -10,4 +12,8 @@ task :default do
|
|
10
12
|
Rake::Task[:test].execute
|
11
13
|
ENV['TEST'] = 'test/test_colorized_string.rb'
|
12
14
|
Rake::Task[:test].execute
|
15
|
+
ENV['TEST'] = 'test/test_colorize_with_readline.rb'
|
16
|
+
Rake::Task[:test].execute
|
17
|
+
ENV['TEST'] = 'test/test_colorized_string_with_readline.rb'
|
18
|
+
Rake::Task[:test].execute
|
13
19
|
end
|
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,78 @@
|
|
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
|
+
magenta: 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_magenta: 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 readline support
|
44
|
+
#
|
45
|
+
def enable_readline_support(value = nil)
|
46
|
+
if value.nil?
|
47
|
+
if defined?(@@readline_support)
|
48
|
+
@@readline_support || false
|
49
|
+
else
|
50
|
+
false
|
51
|
+
end
|
52
|
+
else
|
53
|
+
@@readline_support = (value || false)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# Setter for enable readline support
|
59
|
+
#
|
60
|
+
def enable_readline_support=(value)
|
61
|
+
@@readline_support = (value || false)
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Property for disable colorization
|
5
66
|
#
|
6
67
|
def disable_colorization(value = nil)
|
7
68
|
if value.nil?
|
8
|
-
if defined?(
|
9
|
-
|
69
|
+
if defined?(@@disable_colorization)
|
70
|
+
@@disable_colorization || false
|
10
71
|
else
|
11
72
|
false
|
12
73
|
end
|
13
74
|
else
|
14
|
-
|
75
|
+
@@disable_colorization = (value || false)
|
15
76
|
end
|
16
77
|
end
|
17
78
|
|
@@ -19,7 +80,29 @@ module Colorize
|
|
19
80
|
# Setter for disable colorization
|
20
81
|
#
|
21
82
|
def disable_colorization=(value)
|
22
|
-
|
83
|
+
@@disable_colorization = (value || false)
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# Property for prevent recolorization
|
88
|
+
#
|
89
|
+
def prevent_colors(value = nil)
|
90
|
+
if value.nil?
|
91
|
+
if defined?(@@prevent_colors)
|
92
|
+
@@prevent_colors || false
|
93
|
+
else
|
94
|
+
false
|
95
|
+
end
|
96
|
+
else
|
97
|
+
@@prevent_colors = (value || false)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
#
|
102
|
+
# Setter for prevent recolorization
|
103
|
+
#
|
104
|
+
def prevent_colors=(value)
|
105
|
+
@@prevent_colors = (value || false)
|
23
106
|
end
|
24
107
|
|
25
108
|
#
|
@@ -47,10 +130,16 @@ module Colorize
|
|
47
130
|
end
|
48
131
|
|
49
132
|
#
|
50
|
-
#
|
133
|
+
# Add color alias
|
51
134
|
#
|
52
|
-
def
|
53
|
-
|
135
|
+
def add_color_alias(*params)
|
136
|
+
parse_color_alias_params(params).each do |_alias_, _color_|
|
137
|
+
check_if_color_available!(_alias_)
|
138
|
+
check_if_color_exist!(_color_)
|
139
|
+
|
140
|
+
add_color_code(_alias_, color_codes[_color_])
|
141
|
+
add_color_method(_alias_)
|
142
|
+
end
|
54
143
|
end
|
55
144
|
|
56
145
|
# private
|
@@ -59,32 +148,18 @@ module Colorize
|
|
59
148
|
# Color codes hash
|
60
149
|
#
|
61
150
|
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
|
-
}
|
151
|
+
@@color_codes
|
152
|
+
end
|
153
|
+
|
154
|
+
def add_color_code(code, color)
|
155
|
+
@@color_codes[code] = color
|
73
156
|
end
|
74
157
|
|
75
158
|
#
|
76
159
|
# Mode codes hash
|
77
160
|
#
|
78
161
|
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
|
-
}
|
162
|
+
@@mode_codes
|
88
163
|
end
|
89
164
|
|
90
165
|
#
|
@@ -94,13 +169,20 @@ module Colorize
|
|
94
169
|
colors.each do |key|
|
95
170
|
next if key == :default
|
96
171
|
|
97
|
-
|
98
|
-
|
99
|
-
|
172
|
+
add_color_method(key)
|
173
|
+
end
|
174
|
+
end
|
100
175
|
|
101
|
-
|
102
|
-
|
103
|
-
|
176
|
+
#
|
177
|
+
# Generate color and on_color method
|
178
|
+
#
|
179
|
+
def add_color_method(key)
|
180
|
+
define_method key do
|
181
|
+
colorize(:color => key)
|
182
|
+
end
|
183
|
+
|
184
|
+
define_method "on_#{key}" do
|
185
|
+
colorize(:background => key)
|
104
186
|
end
|
105
187
|
end
|
106
188
|
|
@@ -116,5 +198,32 @@ module Colorize
|
|
116
198
|
end
|
117
199
|
end
|
118
200
|
end
|
201
|
+
|
202
|
+
def parse_color_alias_params(params)
|
203
|
+
return [params] if params.is_a?(Array) && params.length == 2
|
204
|
+
|
205
|
+
params.flat_map do |param|
|
206
|
+
next param if param.is_a?(Array) && param.length == 2
|
207
|
+
next param.to_a if param.is_a?(Hash)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
#
|
212
|
+
# Check if color exists
|
213
|
+
#
|
214
|
+
def check_if_color_available!(color)
|
215
|
+
color_exist?(color) && fail(::Colorize::ColorAlreadyExist, "Colorize: color named :#{color} already exist!")
|
216
|
+
end
|
217
|
+
|
218
|
+
#
|
219
|
+
# Check if color is missing
|
220
|
+
#
|
221
|
+
def check_if_color_exist!(color)
|
222
|
+
color_exist?(color) || fail(::Colorize::ColorDontExist, "Colorize: color :#{color} don't exist!")
|
223
|
+
end
|
224
|
+
|
225
|
+
def color_exist?(color)
|
226
|
+
!color_codes[color].nil?
|
227
|
+
end
|
119
228
|
end
|
120
229
|
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 <<
|
27
|
+
str << colorized_string(match[0], match[1], match[2], match[3])
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
@@ -46,6 +48,17 @@ module Colorize
|
|
46
48
|
|
47
49
|
private
|
48
50
|
|
51
|
+
#
|
52
|
+
# Generate string with escape colors
|
53
|
+
#
|
54
|
+
def colorized_string(mode, color, background_color, previous)
|
55
|
+
if self.class.enable_readline_support
|
56
|
+
"\001\033[#{mode};#{color};#{background_color}m\002#{previous}\001\033[0m\002"
|
57
|
+
else
|
58
|
+
"\033[#{mode};#{color};#{background_color}m#{previous}\033[0m"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
49
62
|
#
|
50
63
|
# Set default colors
|
51
64
|
#
|
@@ -69,30 +82,40 @@ module Colorize
|
|
69
82
|
# Set colors from params hash
|
70
83
|
#
|
71
84
|
def colors_from_hash(match, hash)
|
72
|
-
|
73
|
-
|
74
|
-
|
85
|
+
if self.class.prevent_colors
|
86
|
+
match[0] ||= mode(hash[:mode]) if mode(hash[:mode])
|
87
|
+
match[1] ||= color(hash[:color]) if color(hash[:color])
|
88
|
+
match[2] ||= background_color(hash[:background]) if background_color(hash[:background])
|
89
|
+
else
|
90
|
+
match[0] = mode(hash[:mode]) if mode(hash[:mode])
|
91
|
+
match[1] = color(hash[:color]) if color(hash[:color])
|
92
|
+
match[2] = background_color(hash[:background]) if background_color(hash[:background])
|
93
|
+
end
|
75
94
|
end
|
76
95
|
|
77
96
|
#
|
78
97
|
# Set color from params symbol
|
79
98
|
#
|
80
99
|
def color_from_symbol(match, symbol)
|
81
|
-
|
100
|
+
if self.class.prevent_colors && color(symbol)
|
101
|
+
match[1] ||= color(symbol)
|
102
|
+
else
|
103
|
+
match[1] = color(symbol)
|
104
|
+
end
|
82
105
|
end
|
83
106
|
|
84
107
|
#
|
85
|
-
# Color for foreground
|
108
|
+
# Color for foreground
|
86
109
|
#
|
87
110
|
def color(color)
|
88
|
-
self.class.color_codes[color]
|
111
|
+
self.class.color_codes[color]
|
89
112
|
end
|
90
113
|
|
91
114
|
#
|
92
|
-
# Color for background (offset
|
115
|
+
# Color for background (offset 10)
|
93
116
|
#
|
94
117
|
def background_color(color)
|
95
|
-
self.class.color_codes[color] +
|
118
|
+
self.class.color_codes[color] + 10 if self.class.color_codes[color]
|
96
119
|
end
|
97
120
|
|
98
121
|
#
|
@@ -102,11 +125,22 @@ module Colorize
|
|
102
125
|
self.class.mode_codes[mode]
|
103
126
|
end
|
104
127
|
|
128
|
+
#
|
129
|
+
# Generate regex for color scanner
|
130
|
+
#
|
131
|
+
def scan_for_colors_regex
|
132
|
+
if self.class.enable_readline_support
|
133
|
+
/\001?\033\[([0-9;]+)m\002?(.+?)\001?\033\[0m\002?|([^\001\033]+)/m
|
134
|
+
else
|
135
|
+
/\033\[([0-9;]+)m(.+?)\033\[0m|([^\033]+)/m
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
105
139
|
#
|
106
140
|
# Scan for colorized string
|
107
141
|
#
|
108
142
|
def scan_for_colors
|
109
|
-
scan(
|
143
|
+
scan(scan_for_colors_regex).map do |match|
|
110
144
|
split_colors(match)
|
111
145
|
end
|
112
146
|
end
|
@@ -124,7 +158,7 @@ module Colorize
|
|
124
158
|
# Require windows libs
|
125
159
|
#
|
126
160
|
def require_windows_libs
|
127
|
-
require 'Win32/Console/ANSI' if RUBY_VERSION < '2.0.0' && RUBY_PLATFORM
|
161
|
+
require 'Win32/Console/ANSI' if RUBY_VERSION < '2.0.0' && RUBY_PLATFORM.include?('win32')
|
128
162
|
rescue LoadError
|
129
163
|
raise 'You must gem install win32console to use colorize on Windows'
|
130
164
|
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
|