Skip to content

Commit

Permalink
Support readline
Browse files Browse the repository at this point in the history
  • Loading branch information
fazibear committed Mar 23, 2023
1 parent 2f2ee4e commit 9aa7038
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* add aliases
* grey and gray default aliases
* update dependencies
* support readline
* cleanup and refactor

== 0.8.1 / 2016-06-29
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ puts "This is light blue with red background".colorize(:light_blue ).colorize( :
puts "This is blue text on red".blue.on_red
puts "This is red on blue".colorize(:red).on_blue
puts "This is red on blue and underline".colorize(:red).on_blue.underline
puts "This is blue text on red".blue.on_red.blink
puts "This is blinking blue text on red".blue.on_red.blink
puts "This is uncolorized".blue.on_red.uncolorize
```

Expand Down Expand Up @@ -82,7 +82,7 @@ puts ColorizedString["This is light blue with red background"].colorize(:light_b
puts ColorizedString["This is blue text on red"].blue.on_red
puts ColorizedString["This is red on blue"].colorize(:red).on_blue
puts ColorizedString["This is red on blue and underline"].colorize(:red).on_blue.underline
puts ColorizedString["This is blue text on red"].blue.on_red.blink
puts ColorizedString["This is blinking blue text on red"].blue.on_red.blink
puts ColorizedString["This is uncolorized"].blue.on_red.uncolorize

puts ColorizedString.new("This is blue").blue
Expand Down
4 changes: 2 additions & 2 deletions lib/colorize/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def colorize(params)
scan_for_colors.inject(self.class.new) do |str, match|
colors_from_params(match, params)
defaults_colors(match)
str << "\033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\033[0m"
str << "\001\033[#{match[0]};#{match[1]};#{match[2]}m\002#{match[3]}\001\033[0m\002"
end
end

Expand Down Expand Up @@ -108,7 +108,7 @@ def mode(mode)
# Scan for colorized string
#
def scan_for_colors
scan(/\033\[([0-9;]+)m(.+?)\033\[0m|([^\033]+)/m).map do |match|
scan(/\001\033\[([0-9;]+)m\002(.+?)\001\033\[0m\002|([^\001\033]+)/m).map do |match|
split_colors(match)
end
end
Expand Down
38 changes: 19 additions & 19 deletions test/test_colorize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,51 @@

class TestColorize < Minitest::Test
def test_blue_symbol
assert_equal "\e[0;34;49mThis is blue\e[0m", 'This is blue'.colorize(:blue)
assert_equal "\001\033[0;34;49m\002This is blue\001\033[0m\002", 'This is blue'.colorize(:blue)
end

def test_incorrect_symbol
assert_equal "\e[0;39;49mThis is incorrect color\e[0m", 'This is incorrect color'.colorize(:bold)
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:bold)
end

def test_incorrect_hash
assert_equal "\e[0;39;49mThis is incorrect color\e[0m", 'This is incorrect color'.colorize(:color => :bold)
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:color => :bold)

assert_equal "\e[0;39;49mThis is incorrect color\e[0m", 'This is incorrect color'.colorize(:mode => :green)
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:mode => :green)

assert_equal "\e[0;39;49mThis is incorrect color\e[0m", 'This is incorrect color'.colorize(:background => :bold)
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", 'This is incorrect color'.colorize(:background => :bold)
end

def test_blue_hash
assert_equal "\e[0;34;49mThis is also blue\e[0m", 'This is also blue'.colorize(:color => :blue)
assert_equal "\001\033[0;34;49m\002This is also blue\001\033[0m\002", 'This is also blue'.colorize(:color => :blue)
end

def test_light_blue_symbol
assert_equal "\e[0;94;49mThis is light blue\e[0m", 'This is light blue'.colorize(:light_blue)
assert_equal "\001\033[0;94;49m\002This is light blue\001\033[0m\002", 'This is light blue'.colorize(:light_blue)
end

def test_light_blue_with_red_background_hash
assert_equal "\e[0;94;41mThis is light blue with red background\e[0m", 'This is light blue with red background'.colorize(:color => :light_blue, :background => :red)
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)
end

def test_light_blue_with_red_background_symbol_and_hash
assert_equal "\e[0;94;41mThis is light blue with red background\e[0m", 'This is light blue with red background'.colorize(:light_blue).colorize(:background => :red)
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)
end

def test_blue_with_red_background_method
assert_equal "\e[0;34;41mThis is blue text on red\e[0m", 'This is blue text on red'.blue.on_red
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
end

def test_red_with_blue_background_symbol_and_method
assert_equal "\e[0;31;44mThis is red on blue\e[0m", 'This is red on blue'.colorize(:red).on_blue
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
end

def test_red_with_blue_background_and_underline_symbol_and_methods
assert_equal "\e[4;31;44mThis is red on blue and underline\e[0m", 'This is red on blue and underline'.colorize(:red).on_blue.underline
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
end

def test_blue_with_red_background_and_blink_methods
assert_equal "\e[5;34;41mThis is blue text on red\e[0m", 'This is blue text on red'.blue.on_red.blink
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
end

def test_uncolorize
Expand All @@ -68,15 +68,15 @@ def test_concatenated__colorize?
end

def test_concatenated_strings_on_green
assert_equal "\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", "none #{'red'.red} none #{'blue'.blue} none".on_green
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
end

def test_concatenated_strings_uncolorize
assert_equal 'none red none blue none', "none #{'red'.red} none #{'blue'.blue} none".uncolorize
end

def test_new_line
assert_equal "\e[5;34;41mThis is blue\ntext on red\e[0m", "This is blue\ntext on red".blue.on_red.blink
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
end

def test_disable_colorization_with_=
Expand Down Expand Up @@ -104,11 +104,11 @@ def test_string_disable_colorization_with_=

String.disable_colorization = false

assert_equal "\e[0;34;49mThis is blue after enabling\e[0m", 'This is blue after enabling'.colorize(:blue)
assert_equal "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", 'This is blue after enabling'.colorize(:blue)
end

def test_string_disable_colorization_with_method
assert_equal "\e[0;34;49mThis is blue before disabling\e[0m", 'This is blue before disabling'.colorize(:blue)
assert_equal "\001\033[0;34;49m\002This is blue before disabling\001\033[0m\002", 'This is blue before disabling'.colorize(:blue)

String.disable_colorization true

Expand All @@ -118,11 +118,11 @@ def test_string_disable_colorization_with_method

String.disable_colorization false

assert_equal "\e[0;34;49mThis is blue after enabling\e[0m", 'This is blue after enabling'.colorize(:blue)
assert_equal "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", 'This is blue after enabling'.colorize(:blue)
end

def test_already_colored_string_with_one_value
assert_equal 'This is red'.red, "\e[31mThis is red\e[0m".red
assert_equal 'This is red'.red, "\001\033[31m\002This is red\001\033[0m\002".red
end

def test_color_matrix_method
Expand Down
40 changes: 20 additions & 20 deletions test/test_colorized_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,51 @@ def test_colorize_not_required
end

def test_blue_symbol
assert_equal "\e[0;34;49mThis is blue\e[0m", ColorizedString['This is blue'].colorize(:blue)
assert_equal "\001\033[0;34;49m\002This is blue\001\033[0m\002", ColorizedString['This is blue'].colorize(:blue)
end

def test_incorrect_symbol
assert_equal "\e[0;39;49mThis is incorrect color\e[0m", ColorizedString['This is incorrect color'].colorize(:bold)
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:bold)
end

def test_incorrect_hash
assert_equal "\e[0;39;49mThis is incorrect color\e[0m", ColorizedString['This is incorrect color'].colorize(:color => :bold)
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:color => :bold)

assert_equal "\e[0;39;49mThis is incorrect color\e[0m", ColorizedString['This is incorrect color'].colorize(:mode => :green)
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:mode => :green)

assert_equal "\e[0;39;49mThis is incorrect color\e[0m", ColorizedString['This is incorrect color'].colorize(:background => :bold)
assert_equal "\001\033[0;39;49m\002This is incorrect color\001\033[0m\002", ColorizedString['This is incorrect color'].colorize(:background => :bold)
end

def test_blue_hash
assert_equal "\e[0;34;49mThis is also blue\e[0m", ColorizedString['This is also blue'].colorize(:color => :blue)
assert_equal "\001\033[0;34;49m\002This is also blue\001\033[0m\002", ColorizedString['This is also blue'].colorize(:color => :blue)
end

def test_light_blue_symbol
assert_equal "\e[0;94;49mThis is light blue\e[0m", ColorizedString['This is light blue'].colorize(:light_blue)
assert_equal "\001\033[0;94;49m\002This is light blue\001\033[0m\002", ColorizedString['This is light blue'].colorize(:light_blue)
end

def test_light_blue_with_red_background_hash
assert_equal "\e[0;94;41mThis is light blue with red background\e[0m", ColorizedString['This is light blue with red background'].colorize(:color => :light_blue, :background => :red)
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)
end

def test_light_blue_with_red_background_symbol_and_hash
assert_equal "\e[0;94;41mThis is light blue with red background\e[0m", ColorizedString['This is light blue with red background'].colorize(:light_blue).colorize(:background => :red)
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)
end

def test_blue_with_red_background_method
assert_equal "\e[0;34;41mThis is blue text on red\e[0m", ColorizedString['This is blue text on red'].blue.on_red
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
end

def test_red_with_blue_background_symbol_and_method
assert_equal "\e[0;31;44mThis is red on blue\e[0m", ColorizedString['This is red on blue'].colorize(:red).on_blue
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
end

def test_red_with_blue_background_and_underline_sumbol_and_methods
assert_equal "\e[4;31;44mThis is red on blue and underline\e[0m", ColorizedString['This is red on blue and underline'].colorize(:red).on_blue.underline
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
end

def test_blue_with_red_background_and_blink_methods
assert_equal "\e[5;34;41mThis is blue text on red\e[0m", ColorizedString['This is blue text on red'].blue.on_red.blink
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
end

def test_uncolorize
Expand All @@ -72,19 +72,19 @@ def test_concatenated__colorize?
end

def test_concatenated_strings_on_green
assert_equal "\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", ColorizedString["none #{ColorizedString['red'].red} none #{ColorizedString['blue'].blue} none"].on_green
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
end

def test_concatenated_strings_uncolorize
assert_equal 'none red none blue none', ColorizedString["none #{ColorizedString['red'].red} none #{ColorizedString['blue'].blue} none"].uncolorize
end

def test_frozen_strings
assert_equal "\e[5;34;41mThis is blue text on red\e[0m", ColorizedString['This is blue text on red'].freeze.blue.on_red.blink
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
end

def test_new_line
assert_equal "\e[5;34;41mThis is blue\ntext on red\e[0m", ColorizedString["This is blue\ntext on red"].freeze.blue.on_red.blink
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
end

def test_disable_colorization_with_=
Expand Down Expand Up @@ -112,11 +112,11 @@ def test_string_disable_colorization_with_=

ColorizedString.disable_colorization = false

assert_equal "\e[0;34;49mThis is blue after enabling\e[0m", ColorizedString['This is blue after enabling'].colorize(:blue)
assert_equal "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", ColorizedString['This is blue after enabling'].colorize(:blue)
end

def test_string_disable_colorization_with_method
assert_equal "\e[0;34;49mThis is blue before disabling\e[0m", ColorizedString['This is blue before disabling'].colorize(:blue)
assert_equal "\001\033[0;34;49m\002This is blue before disabling\001\033[0m\002", ColorizedString['This is blue before disabling'].colorize(:blue)

ColorizedString.disable_colorization true

Expand All @@ -126,11 +126,11 @@ def test_string_disable_colorization_with_method

ColorizedString.disable_colorization false

assert_equal "\e[0;34;49mThis is blue after enabling\e[0m", ColorizedString['This is blue after enabling'].colorize(:blue)
assert_equal "\001\033[0;34;49m\002This is blue after enabling\001\033[0m\002", ColorizedString['This is blue after enabling'].colorize(:blue)
end

def test_already_colored_string_with_one_value
assert_equal ColorizedString['This is red'].red, ColorizedString["\e[31mThis is red\e[0m"].red
assert_equal ColorizedString['This is red'].red, ColorizedString["\001\033[31m\002This is red\001\033[0m\002"].red
end

def test_color_matrix_method
Expand Down

0 comments on commit 9aa7038

Please sign in to comment.