colorize 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96a8cb83d923d510e6c2eb252edf612ccf052e6e4f0a1c3e2a7acfd90323dc40
4
- data.tar.gz: 9ed2a4cb4e82a83c726cd63173cda755c6dbff39329e28e33c0ffdedc304eaa5
3
+ metadata.gz: b2258f1a1f28ceb6396b7e29aa308326e02bba9a9f36cd229e70c0e417aacd37
4
+ data.tar.gz: 65b1cbb6089464e698f2dc8591b7d80af1c4aab479cef11e2fcee3c46428dd20
5
5
  SHA512:
6
- metadata.gz: 61c0f448e3cf1651cc707b5d0691e329743d8c1473458100ce466bc2f98f13c2a1bd511d91836606c7dcc4bd3321b068947fd965a3774c65bb38d1002ceed15e
7
- data.tar.gz: bbc9cc5151b2b6e21f24300a5a08c45068e6cfa1018b2d6865665b9ce509db25823341b758c61002d2d1e8692fbda3d3fd4365ea1a72326b873a227495ba4741
6
+ metadata.gz: 879d312bfb7dad3576bbf6e9791c2a6e168bfb8a29db81980238ce2aaaa5456ac1e535709a6c4f4f811aa5d2a22dce5e89cd0e5366f365102a0667e5c7263d4b
7
+ data.tar.gz: efc22eb11cc908ae015473ac51fc19680b8adcaf6904d6db7c74064e7e3b2a41cdeadca0a45eeb7950780f94db19e472f561cb3b60bd125fc9460ee4807d353a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.0 / 2023-06-22
4
+ - make readline support optional
5
+
3
6
  ## 1.0.5 / 2023-06-22
4
7
  - (backwards compatibility) purple -> magenta
5
8
 
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 << "\001\033[#{match[0]};#{match[1]};#{match[2]}m\002#{match[3]}\001\033[0m\002"
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(/\001?\033\[([0-9;]+)m\002?(.+?)\001?\033\[0m\002?|([^\001\033]+)/m).map do |match|
143
+ scan(scan_for_colors_regex).map do |match|
122
144
  split_colors(match)
123
145
  end
124
146
  end
@@ -4,5 +4,5 @@
4
4
  # Gem version
5
5
  #
6
6
  module Colorize
7
- VERSION = '1.0.5'
7
+ VERSION = '1.1.0'
8
8
  end
@@ -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 "\001\033[0;34;49m\002This is blue\001\033[0m\002", 'This is blue'.colorize(:blue)
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 "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:bold)
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 "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:color => :bold)
16
+ assert_equal "\033[0;39;49mThis is incorrect color\033[0m", 'This is incorrect color'.colorize(:color => :bold)
17
17
 
18
- assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:mode => :green)
18
+ assert_equal "\033[0;39;49mThis is incorrect color\033[0m", 'This is incorrect color'.colorize(:mode => :green)
19
19
 
20
- assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:background => :bold)
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 "\001\033[0;34;49m\002This is also blue?\001\033[0m\002", 'This is also blue?'.colorize(:color => :blue)
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 "\001\033[0;94;49m\002This is light blue\001\033[0m\002", 'This is light blue'.colorize(:light_blue)
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 "\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)
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 "\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)
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 "\001\033[0;34;41m\002This is blue text on red\001\033[0m\002", 'This is blue text on red'.blue.on_red
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 "\001\033[0;31;44m\002This is red on blue\001\033[0m\002", 'This is red on blue'.colorize(:red).on_blue
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 "\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
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 "\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
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 "\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
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 "\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
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 "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", 'This is blue after enabling'.colorize(:blue)
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 "\001\033[0;34;49m\002This is blue before disabling\001\033[0m\002", 'This is blue before disabling'.colorize(:blue)
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 "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", 'This is blue after enabling'.colorize(:blue)
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, "\001\033[31m\002This is red\001\033[0m\002".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 "\001\033[0;34;49m\002This is blue\001\033[0m\002", ColorizedString['This is blue'].colorize(:blue)
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 "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:bold)
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 "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:color => :bold)
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 "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:mode => :green)
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 "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:background => :bold)
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 "\001\033[0;34;49m\002This is also blue\001\033[0m\002", ColorizedString['This is also blue'].colorize(:color => :blue)
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 "\001\033[0;94;49m\002This is light blue\001\033[0m\002", ColorizedString['This is light blue'].colorize(:light_blue)
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 "\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)
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 "\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)
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 "\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
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 "\001\033[0;31;44m\002This is red on blue\001\033[0m\002", ColorizedString['This is red on blue'].colorize(:red).on_blue
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 "\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
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 "\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
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 "\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
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 "\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
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 "\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
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 "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", ColorizedString['This is blue after enabling'].colorize(:blue)
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 "\001\033[0;34;49m\002This is blue before disabling\001\033[0m\002", ColorizedString['This is blue before disabling'].colorize(:blue)
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 "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", ColorizedString['This is blue after enabling'].colorize(:blue)
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["\001\033[31m\002This is red\001\033[0m\002"].red
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.5
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-22 00:00:00.000000000 Z
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.