colorize 1.0.5 → 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 +4 -4
- data/CHANGELOG.md +3 -0
- data/Rakefile +4 -0
- data/lib/colorize/class_methods.rb +22 -0
- data/lib/colorize/instance_methods.rb +24 -2
- data/lib/colorize/version.rb +1 -1
- data/test/test_colorize.rb +19 -19
- data/test/test_colorized_string.rb +20 -20
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
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
CHANGED
data/Rakefile
CHANGED
@@ -12,4 +12,8 @@ task :default do
|
|
12
12
|
Rake::Task[:test].execute
|
13
13
|
ENV['TEST'] = 'test/test_colorized_string.rb'
|
14
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
|
15
19
|
end
|
@@ -39,6 +39,28 @@ module Colorize
|
|
39
39
|
overlined: 53
|
40
40
|
}
|
41
41
|
|
42
|
+
#
|
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
|
+
|
42
64
|
#
|
43
65
|
# Property for disable colorization
|
44
66
|
#
|
@@ -24,7 +24,7 @@ module Colorize
|
|
24
24
|
scan_for_colors.inject(self.class.new) do |str, match|
|
25
25
|
colors_from_params(match, params)
|
26
26
|
defaults_colors(match)
|
27
|
-
str <<
|
27
|
+
str << colorized_string(match[0], match[1], match[2], match[3])
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -48,6 +48,17 @@ module Colorize
|
|
48
48
|
|
49
49
|
private
|
50
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
|
+
|
51
62
|
#
|
52
63
|
# Set default colors
|
53
64
|
#
|
@@ -114,11 +125,22 @@ module Colorize
|
|
114
125
|
self.class.mode_codes[mode]
|
115
126
|
end
|
116
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
|
+
|
117
139
|
#
|
118
140
|
# Scan for colorized string
|
119
141
|
#
|
120
142
|
def scan_for_colors
|
121
|
-
scan(
|
143
|
+
scan(scan_for_colors_regex).map do |match|
|
122
144
|
split_colors(match)
|
123
145
|
end
|
124
146
|
end
|
data/lib/colorize/version.rb
CHANGED
data/test/test_colorize.rb
CHANGED
@@ -5,51 +5,51 @@ require "#{File.dirname(__FILE__)}/../lib/colorize"
|
|
5
5
|
|
6
6
|
class TestColorize < Minitest::Test
|
7
7
|
def test_blue_symbol
|
8
|
-
assert_equal "\
|
8
|
+
assert_equal "\033[0;34;49mThis is blue\033[0m", 'This is blue'.colorize(:blue)
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_incorrect_symbol
|
12
|
-
assert_equal "\
|
12
|
+
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", 'This is incorrect color'.colorize(:bold)
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_incorrect_hash
|
16
|
-
assert_equal "\
|
16
|
+
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", 'This is incorrect color'.colorize(:color => :bold)
|
17
17
|
|
18
|
-
assert_equal "\
|
18
|
+
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", 'This is incorrect color'.colorize(:mode => :green)
|
19
19
|
|
20
|
-
assert_equal "\
|
20
|
+
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", 'This is incorrect color'.colorize(:background => :bold)
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_blue_hash
|
24
|
-
assert_equal "\
|
24
|
+
assert_equal "\033[0;34;49mThis is also blue?\033[0m", 'This is also blue?'.colorize(:color => :blue)
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_light_blue_symbol
|
28
|
-
assert_equal "\
|
28
|
+
assert_equal "\033[0;94;49mThis is light blue\033[0m", 'This is light blue'.colorize(:light_blue)
|
29
29
|
end
|
30
30
|
|
31
31
|
def test_light_blue_with_red_background_hash
|
32
|
-
assert_equal "\
|
32
|
+
assert_equal "\033[0;94;41mThis is light blue with red background\033[0m", 'This is light blue with red background'.colorize(:color => :light_blue, :background => :red)
|
33
33
|
end
|
34
34
|
|
35
35
|
def test_light_blue_with_red_background_symbol_and_hash
|
36
|
-
assert_equal "\
|
36
|
+
assert_equal "\033[0;94;41mThis is light blue with red background\033[0m", 'This is light blue with red background'.colorize(:light_blue).colorize(:background => :red)
|
37
37
|
end
|
38
38
|
|
39
39
|
def test_blue_with_red_background_method
|
40
|
-
assert_equal "\
|
40
|
+
assert_equal "\033[0;34;41mThis is blue text on red\033[0m", 'This is blue text on red'.blue.on_red
|
41
41
|
end
|
42
42
|
|
43
43
|
def test_red_with_blue_background_symbol_and_method
|
44
|
-
assert_equal "\
|
44
|
+
assert_equal "\033[0;31;44mThis is red on blue\033[0m", 'This is red on blue'.colorize(:red).on_blue
|
45
45
|
end
|
46
46
|
|
47
47
|
def test_red_with_blue_background_and_underline_symbol_and_methods
|
48
|
-
assert_equal "\
|
48
|
+
assert_equal "\033[4;31;44mThis is red on blue and underline\033[0m", 'This is red on blue and underline'.colorize(:red).on_blue.underline
|
49
49
|
end
|
50
50
|
|
51
51
|
def test_blue_with_red_background_and_blink_methods
|
52
|
-
assert_equal "\
|
52
|
+
assert_equal "\033[5;34;41mThis is blue text on red\033[0m", 'This is blue text on red'.blue.on_red.blink
|
53
53
|
end
|
54
54
|
|
55
55
|
def test_uncolorize
|
@@ -68,7 +68,7 @@ class TestColorize < Minitest::Test
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def test_concatenated_strings_on_green
|
71
|
-
assert_equal "\
|
71
|
+
assert_equal "\033[0;39;42mnone \033[0m\033[0;31;42mred\033[0m\033[0;39;42m none \033[0m\033[0;34;42mblue\033[0m\033[0;39;42m none\033[0m", "none #{'red'.red} none #{'blue'.blue} none".on_green
|
72
72
|
end
|
73
73
|
|
74
74
|
def test_concatenated_strings_uncolorize
|
@@ -76,7 +76,7 @@ class TestColorize < Minitest::Test
|
|
76
76
|
end
|
77
77
|
|
78
78
|
def test_new_line
|
79
|
-
assert_equal "\
|
79
|
+
assert_equal "\033[5;34;41mThis is blue\ntext on red\033[0m", "This is blue\ntext on red".blue.on_red.blink
|
80
80
|
end
|
81
81
|
|
82
82
|
def test_disable_colorization_with_=
|
@@ -104,11 +104,11 @@ class TestColorize < Minitest::Test
|
|
104
104
|
|
105
105
|
String.disable_colorization = false
|
106
106
|
|
107
|
-
assert_equal "\
|
107
|
+
assert_equal "\033[0;34;49mThis is blue after enabling\033[0m", 'This is blue after enabling'.colorize(:blue)
|
108
108
|
end
|
109
109
|
|
110
110
|
def test_string_disable_colorization_with_method
|
111
|
-
assert_equal "\
|
111
|
+
assert_equal "\033[0;34;49mThis is blue before disabling\033[0m", 'This is blue before disabling'.colorize(:blue)
|
112
112
|
|
113
113
|
String.disable_colorization true
|
114
114
|
|
@@ -118,11 +118,11 @@ class TestColorize < Minitest::Test
|
|
118
118
|
|
119
119
|
String.disable_colorization false
|
120
120
|
|
121
|
-
assert_equal "\
|
121
|
+
assert_equal "\033[0;34;49mThis is blue after enabling\033[0m", 'This is blue after enabling'.colorize(:blue)
|
122
122
|
end
|
123
123
|
|
124
124
|
def test_already_colored_string_with_one_value
|
125
|
-
assert_equal 'This is red'.red, "\
|
125
|
+
assert_equal 'This is red'.red, "\033[31mThis is red\033[0m".red
|
126
126
|
end
|
127
127
|
|
128
128
|
def test_color_matrix_method
|
@@ -9,51 +9,51 @@ class TestColorizedString < Minitest::Test
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_blue_symbol
|
12
|
-
assert_equal "\
|
12
|
+
assert_equal "\033[0;34;49mThis is blue\033[0m", ColorizedString['This is blue'].colorize(:blue)
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_incorrect_symbol
|
16
|
-
assert_equal "\
|
16
|
+
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", ColorizedString['This is incorrect color'].colorize(:bold)
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_incorrect_hash
|
20
|
-
assert_equal "\
|
20
|
+
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", ColorizedString['This is incorrect color'].colorize(:color => :bold)
|
21
21
|
|
22
|
-
assert_equal "\
|
22
|
+
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", ColorizedString['This is incorrect color'].colorize(:mode => :green)
|
23
23
|
|
24
|
-
assert_equal "\
|
24
|
+
assert_equal "\033[0;39;49mThis is incorrect color\033[0m", ColorizedString['This is incorrect color'].colorize(:background => :bold)
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_blue_hash
|
28
|
-
assert_equal "\
|
28
|
+
assert_equal "\033[0;34;49mThis is also blue\033[0m", ColorizedString['This is also blue'].colorize(:color => :blue)
|
29
29
|
end
|
30
30
|
|
31
31
|
def test_light_blue_symbol
|
32
|
-
assert_equal "\
|
32
|
+
assert_equal "\033[0;94;49mThis is light blue\033[0m", ColorizedString['This is light blue'].colorize(:light_blue)
|
33
33
|
end
|
34
34
|
|
35
35
|
def test_light_blue_with_red_background_hash
|
36
|
-
assert_equal "\
|
36
|
+
assert_equal "\033[0;94;41mThis is light blue with red background\033[0m", ColorizedString['This is light blue with red background'].colorize(:color => :light_blue, :background => :red)
|
37
37
|
end
|
38
38
|
|
39
39
|
def test_light_blue_with_red_background_symbol_and_hash
|
40
|
-
assert_equal "\
|
40
|
+
assert_equal "\033[0;94;41mThis is light blue with red background\033[0m", ColorizedString['This is light blue with red background'].colorize(:light_blue).colorize(:background => :red)
|
41
41
|
end
|
42
42
|
|
43
43
|
def test_blue_with_red_background_method
|
44
|
-
assert_equal "\
|
44
|
+
assert_equal "\033[0;34;41mThis is blue text on red\033[0m", ColorizedString['This is blue text on red'].blue.on_red
|
45
45
|
end
|
46
46
|
|
47
47
|
def test_red_with_blue_background_symbol_and_method
|
48
|
-
assert_equal "\
|
48
|
+
assert_equal "\033[0;31;44mThis is red on blue\033[0m", ColorizedString['This is red on blue'].colorize(:red).on_blue
|
49
49
|
end
|
50
50
|
|
51
51
|
def test_red_with_blue_background_and_underline_sumbol_and_methods
|
52
|
-
assert_equal "\
|
52
|
+
assert_equal "\033[4;31;44mThis is red on blue and underline\033[0m", ColorizedString['This is red on blue and underline'].colorize(:red).on_blue.underline
|
53
53
|
end
|
54
54
|
|
55
55
|
def test_blue_with_red_background_and_blink_methods
|
56
|
-
assert_equal "\
|
56
|
+
assert_equal "\033[5;34;41mThis is blue text on red\033[0m", ColorizedString['This is blue text on red'].blue.on_red.blink
|
57
57
|
end
|
58
58
|
|
59
59
|
def test_uncolorize
|
@@ -72,7 +72,7 @@ class TestColorizedString < Minitest::Test
|
|
72
72
|
end
|
73
73
|
|
74
74
|
def test_concatenated_strings_on_green
|
75
|
-
assert_equal "\
|
75
|
+
assert_equal "\033[0;39;42mnone \033[0m\033[0;31;42mred\033[0m\033[0;39;42m none \033[0m\033[0;34;42mblue\033[0m\033[0;39;42m none\033[0m", ColorizedString["none #{ColorizedString['red'].red} none #{ColorizedString['blue'].blue} none"].on_green
|
76
76
|
end
|
77
77
|
|
78
78
|
def test_concatenated_strings_uncolorize
|
@@ -80,11 +80,11 @@ class TestColorizedString < Minitest::Test
|
|
80
80
|
end
|
81
81
|
|
82
82
|
def test_frozen_strings
|
83
|
-
assert_equal "\
|
83
|
+
assert_equal "\033[5;34;41mThis is blue text on red\033[0m", ColorizedString['This is blue text on red'].freeze.blue.on_red.blink
|
84
84
|
end
|
85
85
|
|
86
86
|
def test_new_line
|
87
|
-
assert_equal "\
|
87
|
+
assert_equal "\033[5;34;41mThis is blue\ntext on red\033[0m", ColorizedString["This is blue\ntext on red"].freeze.blue.on_red.blink
|
88
88
|
end
|
89
89
|
|
90
90
|
def test_disable_colorization_with_=
|
@@ -112,11 +112,11 @@ class TestColorizedString < Minitest::Test
|
|
112
112
|
|
113
113
|
ColorizedString.disable_colorization = false
|
114
114
|
|
115
|
-
assert_equal "\
|
115
|
+
assert_equal "\033[0;34;49mThis is blue after enabling\033[0m", ColorizedString['This is blue after enabling'].colorize(:blue)
|
116
116
|
end
|
117
117
|
|
118
118
|
def test_string_disable_colorization_with_method
|
119
|
-
assert_equal "\
|
119
|
+
assert_equal "\033[0;34;49mThis is blue before disabling\033[0m", ColorizedString['This is blue before disabling'].colorize(:blue)
|
120
120
|
|
121
121
|
ColorizedString.disable_colorization true
|
122
122
|
|
@@ -126,11 +126,11 @@ class TestColorizedString < Minitest::Test
|
|
126
126
|
|
127
127
|
ColorizedString.disable_colorization false
|
128
128
|
|
129
|
-
assert_equal "\
|
129
|
+
assert_equal "\033[0;34;49mThis is blue after enabling\033[0m", ColorizedString['This is blue after enabling'].colorize(:blue)
|
130
130
|
end
|
131
131
|
|
132
132
|
def test_already_colored_string_with_one_value
|
133
|
-
assert_equal ColorizedString['This is red'].red, ColorizedString["\
|
133
|
+
assert_equal ColorizedString['This is red'].red, ColorizedString["\033[31mThis is red\033[0m"].red
|
134
134
|
end
|
135
135
|
|
136
136
|
def test_color_matrix_method
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: colorize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michał Kalbarczyk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-06-
|
11
|
+
date: 2023-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Extends String class or add a ColorizedString with methods to set text
|
14
14
|
color, background color and text effects.
|