From 2f842da04339556bec7dfb81fecc85cf376eb9e4 Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Wed, 19 Jun 2024 16:19:59 +0200 Subject: [PATCH] Add some more codes for text --- lib/term/ansicolor.rb | 29 ++++++---- lib/term/ansicolor/attribute.rb | 5 +- lib/term/ansicolor/attribute/color256.rb | 74 ++++++++++++------------ lib/term/ansicolor/attribute/text.rb | 31 +++++----- tests/attribute_test.rb | 18 +++--- tests/hsl_triple_test.rb | 2 +- 6 files changed, 86 insertions(+), 73 deletions(-) diff --git a/lib/term/ansicolor.rb b/lib/term/ansicolor.rb index 89c97ab..3268355 100644 --- a/lib/term/ansicolor.rb +++ b/lib/term/ansicolor.rb @@ -66,8 +66,8 @@ def self.true_coloring=(val) def self.create_color_method(color_name, color_value) module_eval <<-EOT - def #{color_name}(string = nil, &block) - color(:#{color_name}, string, &block) + def #{color_name}(string = nil, **options, &block) + apply_attribute(:#{color_name}, string, **options, &block) end EOT self @@ -79,7 +79,7 @@ def #{color_name}(string = nil, &block) # Regular expression that is used to scan for ANSI-Attributes while # uncoloring strings. - COLORED_REGEXP = /\e\[(?:(?:[349]|10)[0-7]|[0-9]|[34]8;(5;\d{1,3}|2;\d{1,3}(;\d{1,3}){2}))?m/ + COLORED_REGEXP = /\e\[(?:(?:[349]|10)[0-7]|[0-9]|[34]8;(5;\d{1,3}|2;\d{1,3}(;\d{1,3}){2})|4:\d|53)?m/ # Returns an uncolored version of the string, that is all ANSI-Attributes # are stripped from the string. @@ -97,11 +97,9 @@ def uncolor(string = nil) # :yields: alias uncolored uncolor - # Return +string+ or the result string of the given +block+ colored with - # color +name+. If string isn't a string only the escape sequence to switch - # on the color +name+ is returned. - def color(name, string = nil, &block) - attribute = Attribute[name] or raise ArgumentError, "unknown attribute #{name.inspect}" + def apply_attribute(name, string = nil, &block) + attribute = Attribute[name] or + raise ArgumentError, "unknown attribute #{name.inspect}" result = '' result << "\e[#{attribute.code}m" if Term::ANSIColor.coloring? if block_given? @@ -117,11 +115,22 @@ def color(name, string = nil, &block) result.extend(Term::ANSIColor) end + # Return +string+ or the result string of the given +block+ colored with + # color +name+. If string isn't a string only the escape sequence to switch + # on the color +name+ is returned. + def color(name, string = nil, &block) + apply_attribute(name, string, &block) + end + + # Return +string+ or the result string of the given +block+ with a + # background colored with color +name+. If string isn't a string only the + # escape sequence to switch on the color +name+ is returned. def on_color(name, string = nil, &block) - attribute = Attribute[name] or raise ArgumentError, "unknown attribute #{name.inspect}" + attribute = Attribute[name] or + raise ArgumentError, "unknown attribute #{name.inspect}" attribute = attribute.dup attribute.background = true - color(attribute, string, &block) + apply_attribute(attribute, string, &block) end class << self diff --git a/lib/term/ansicolor/attribute.rb b/lib/term/ansicolor/attribute.rb index 45abd7d..0294f21 100644 --- a/lib/term/ansicolor/attribute.rb +++ b/lib/term/ansicolor/attribute.rb @@ -95,8 +95,9 @@ def self.on_true_color(color, options = {}) def initialize(name, code, options = {}) @name = name.to_sym - @background = @name.start_with?('on_') + @background = !!options[:background] @code = code.to_s + @true_color = false if rgb = options[:true_color] @true_color = true @rgb = rgb @@ -122,7 +123,7 @@ def code end def apply(string = nil, &block) - ::Term::ANSIColor.color(self, string, &block) + ::Term::ANSIColor.apply_attribute(self, string, &block) end def background? diff --git a/lib/term/ansicolor/attribute/color256.rb b/lib/term/ansicolor/attribute/color256.rb index eb7c4fb..1b66554 100644 --- a/lib/term/ansicolor/attribute/color256.rb +++ b/lib/term/ansicolor/attribute/color256.rb @@ -2,65 +2,67 @@ module Term module ANSIColor class Attribute class Color256 - Attribute.set :color0, 0, :html => '#000000' - Attribute.set :color1, 1, :html => '#800000' - Attribute.set :color2, 2, :html => '#808000' - Attribute.set :color3, 3, :html => '#808000' - Attribute.set :color4, 4, :html => '#000080' - Attribute.set :color5, 5, :html => '#800080' - Attribute.set :color6, 6, :html => '#008080' - Attribute.set :color7, 7, :html => '#c0c0c0' + Attribute.set :color0, 0, html: '#000000' + Attribute.set :color1, 1, html: '#800000' + Attribute.set :color2, 2, html: '#808000' + Attribute.set :color3, 3, html: '#808000' + Attribute.set :color4, 4, html: '#000080' + Attribute.set :color5, 5, html: '#800080' + Attribute.set :color6, 6, html: '#008080' + Attribute.set :color7, 7, html: '#c0c0c0' - Attribute.set :color8, 8, :html => '#808080' - Attribute.set :color9, 9, :html => '#ff0000' - Attribute.set :color10, 10, :html => '#00ff00' - Attribute.set :color11, 11, :html => '#ffff00' - Attribute.set :color12, 12, :html => '#0000ff' - Attribute.set :color13, 13, :html => '#ff00ff' - Attribute.set :color14, 14, :html => '#00ffff' - Attribute.set :color15, 15, :html => '#ffffff' + Attribute.set :color8, 8, html: '#808080' + Attribute.set :color9, 9, html: '#ff0000' + Attribute.set :color10, 10, html: '#00ff00' + Attribute.set :color11, 11, html: '#ffff00' + Attribute.set :color12, 12, html: '#0000ff' + Attribute.set :color13, 13, html: '#ff00ff' + Attribute.set :color14, 14, html: '#00ffff' + Attribute.set :color15, 15, html: '#ffffff' steps = [ 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff ] for i in 16..231 red, green, blue = (i - 16).to_s(6).rjust(3, '0').each_char.map { |c| steps[c.to_i] } - Attribute.set "color#{i}", i, :red => red, :green => green, :blue => blue + Attribute.set "color#{i}", i, red: red, green: green, blue: blue end grey = 8 for i in 232..255 - Attribute.set "color#{i}", i, :red => grey, :green => grey, :blue => grey + Attribute.set "color#{i}", i, red: grey, green: grey, blue: grey grey += 10 end - Attribute.set :on_color0, 0, :html => '#000000' - Attribute.set :on_color1, 1, :html => '#800000' - Attribute.set :on_color2, 2, :html => '#808000' - Attribute.set :on_color3, 3, :html => '#808000' - Attribute.set :on_color4, 4, :html => '#000080' - Attribute.set :on_color5, 5, :html => '#800080' - Attribute.set :on_color6, 6, :html => '#008080' - Attribute.set :on_color7, 7, :html => '#c0c0c0' + Attribute.set :on_color0, 0, html: '#000000', background: true + Attribute.set :on_color1, 1, html: '#800000', background: true + Attribute.set :on_color2, 2, html: '#808000', background: true + Attribute.set :on_color3, 3, html: '#808000', background: true + Attribute.set :on_color4, 4, html: '#000080', background: true + Attribute.set :on_color5, 5, html: '#800080', background: true + Attribute.set :on_color6, 6, html: '#008080', background: true + Attribute.set :on_color7, 7, html: '#c0c0c0' - Attribute.set :on_color8, 8, :html => '#808080' - Attribute.set :on_color9, 9, :html => '#ff0000' - Attribute.set :on_color10, 10, :html => '#00ff00' - Attribute.set :on_color11, 11, :html => '#ffff00' - Attribute.set :on_color12, 12, :html => '#0000ff' - Attribute.set :on_color13, 13, :html => '#ff00ff' - Attribute.set :on_color14, 14, :html => '#00ffff' - Attribute.set :on_color15, 15, :html => '#ffffff' + Attribute.set :on_color8, 8, html: '#808080', background: true + Attribute.set :on_color9, 9, html: '#ff0000', background: true + Attribute.set :on_color10, 10, html: '#00ff00', background: true + Attribute.set :on_color11, 11, html: '#ffff00', background: true + Attribute.set :on_color12, 12, html: '#0000ff', background: true + Attribute.set :on_color13, 13, html: '#ff00ff', background: true + Attribute.set :on_color14, 14, html: '#00ffff', background: true + Attribute.set :on_color15, 15, html: '#ffffff', background: true steps = [ 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff ] for i in 16..231 red, green, blue = (i - 16).to_s(6).rjust(3, '0').each_char.map { |c| steps[c.to_i] } - Attribute.set "on_color#{i}", i, :red => red, :green => green, :blue => blue + Attribute.set "on_color#{i}", i, + red: red, green: green, blue: blue, background: true end grey = 8 for i in 232..255 - Attribute.set "on_color#{i}", i, :red => grey, :green => grey, :blue => grey + Attribute.set "on_color#{i}", i, + red: grey, green: grey, blue: grey, background: true grey += 10 end end diff --git a/lib/term/ansicolor/attribute/text.rb b/lib/term/ansicolor/attribute/text.rb index e41d92f..6f2078b 100644 --- a/lib/term/ansicolor/attribute/text.rb +++ b/lib/term/ansicolor/attribute/text.rb @@ -2,21 +2,22 @@ module Term module ANSIColor class Attribute class Text - Attribute.set :clear, 0 # String#clear already used in String - Attribute.set :reset, 0 # synonym for :clear - Attribute.set :bold, 1 - Attribute.set :dark, 2 - Attribute.set :faint, 2 - Attribute.set :italic, 3 # not widely implemented - Attribute.set :underline, 4 - Attribute.set :underscore, 4 # synonym for :underline - Attribute.set :blink, 5 - Attribute.set :rapid_blink, 6 # not widely implemented - Attribute.set :reverse, 7 # String#reverse already used in String - Attribute.set :negative, 7 # synonym for :reverse - Attribute.set :concealed, 8 - Attribute.set :conceal, 8 # synonym for :concealed - Attribute.set :strikethrough, 9 # not widely implemented + Attribute.set :clear, 0 # String#clear already used in String + Attribute.set :reset, 0 # synonym for :clear + Attribute.set :bold, 1 + Attribute.set :dark, 2 + Attribute.set :faint, 2 + Attribute.set :italic, 3 # not widely implemented + Attribute.set :underline, 4 + Attribute.set :underscore, 4 # synonym for :underline + Attribute.set :blink, 5 + Attribute.set :rapid_blink, 6 # not widely implemented + Attribute.set :reverse, 7 # String#reverse already used in String + Attribute.set :negative, 7 # synonym for :reverse + Attribute.set :concealed, 8 + Attribute.set :conceal, 8 # synonym for :concealed + Attribute.set :strikethrough, 9 # not widely implemented + Attribute.set :overline, 53 # not widely implemented end end end diff --git a/tests/attribute_test.rb b/tests/attribute_test.rb index bd2a082..2b76a51 100644 --- a/tests/attribute_test.rb +++ b/tests/attribute_test.rb @@ -32,7 +32,7 @@ def test_distance_to assert_in_delta 250.954, Attribute.get(:color0).distance_to(color), 1e-3 color = Attribute.nearest_rgb_color('#0f0') assert_in_delta 255, Attribute.get(:color0).distance_to(color, - :metric => RGBColorMetrics::Euclidean), 1e-3 + metric: RGBColorMetrics::Euclidean), 1e-3 assert_equal 1 / 0.0, Attribute.get(:color0).distance_to(nil) end @@ -40,14 +40,14 @@ def test_nearest_rgb_color assert_equal Attribute.get(:color0).rgb, Attribute.nearest_rgb_color('#000').rgb assert_equal Attribute.get(:color15).rgb, Attribute.nearest_rgb_color('#ffffff').rgb assert_equal :color248, Attribute.nearest_rgb_color('#aaa').name - assert_equal :color109, Attribute.nearest_rgb_color('#aaa', :gray => false).name + assert_equal :color109, Attribute.nearest_rgb_color('#aaa', gray: false).name end def test_nearest_rgb_on_color assert_equal Attribute.get(:on_color0).rgb, Attribute.nearest_rgb_on_color('#000').rgb assert_equal Attribute.get(:on_color15).rgb, Attribute.nearest_rgb_on_color('#ffffff').rgb assert_equal :on_color248, Attribute.nearest_rgb_on_color('#aaa').name - assert_equal :on_color109, Attribute.nearest_rgb_on_color('#aaa', :gray => false).name + assert_equal :on_color109, Attribute.nearest_rgb_on_color('#aaa', gray: false).name end def test_apply @@ -59,13 +59,13 @@ def test_apply def test_gradient g0 = Attribute[:blink].gradient_to Attribute['#30ffaa'] assert_equal [], g0 - g1 = Attribute['#30ffaa'].gradient_to(Attribute['#ff507f'], :steps => 9) + g1 = Attribute['#30ffaa'].gradient_to(Attribute['#ff507f'], steps: 9) assert_equal [ :color49, :color49, :color43, :color79, :color108, :color247, :color138, :color168, :color204 ], g1.map(&:name) g2 = Attribute['#30ffaa'].gradient_to( Attribute['#ff507f'], - :steps => 9, - :metric => RGBColorMetrics::Euclidean + steps: 9, + metric: RGBColorMetrics::Euclidean ) assert_equal [ :color49, :color43, :color79, :color73, :color108, :color247, :color138, :color168, :color204 ], g2.map(&:name) @@ -93,7 +93,7 @@ def test_true_color_gradient assert_equal [], g0 g1 = Attribute['#30ffaa'].gradient_to( Attribute['#ff507f'], - :steps => 9, + steps: 9, true_coloring: true ) assert_equal %w[ @@ -101,9 +101,9 @@ def test_true_color_gradient ], g1.map { _1.rgb.html } g2 = Attribute['#30ffaa'].gradient_to( Attribute['#ff507f'], - :steps => 9, + steps: 9, true_coloring: true, - :metric => RGBColorMetrics::Euclidean + metric: RGBColorMetrics::Euclidean ) assert_equal %w[ #00ffaf #1febaa #3ed7a5 #5dc3a0 #7caf9b #9b9b96 #ba8791 #d9738c #ff5f87 diff --git a/tests/hsl_triple_test.rb b/tests/hsl_triple_test.rb index 4b2b47c..9bf26e1 100644 --- a/tests/hsl_triple_test.rb +++ b/tests/hsl_triple_test.rb @@ -13,7 +13,7 @@ def setup def test_hsl_cast assert_equal '#85e085', HSLTriple[ @pastel_green_hsl ].html - assert_equal '#85e085', HSLTriple[ :hue => 120, :saturation => 59.4, :lightness => 70 ].html + assert_equal '#85e085', HSLTriple[ hue: 120, saturation: 59.4, lightness: 70 ].html assert_equal '#11ddff', HSLTriple[ '#1df' ].html assert_equal '#8000ff', HSLTriple[ 'rgb(128,0,255)' ].html assert_equal '#85e085', HSLTriple[ 'hsl(120.0,59.4%,70.0%)' ].html