Skip to content

Commit

Permalink
The order of define values should be preserved
Browse files Browse the repository at this point in the history
  • Loading branch information
hasumikin committed Nov 3, 2023
1 parent 67a8497 commit c02205c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ end
gem 'bundler'
gem 'rake'
gem 'test-unit'
gem 'test-unit-rr'
13 changes: 5 additions & 8 deletions lib/reline/face.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,8 @@ def initialize(name, &block)

attr_reader :definition

def define(name, foreground: nil, background: nil, style: nil)
values = {}
values[:foreground] = foreground if foreground
values[:background] = background if background
values[:style] = style if style
values[:escape_sequence] = format_to_sgr(values).freeze
def define(name, **values)
values[:escape_sequence] = format_to_sgr(values.to_a).freeze
@definition[name] = values
end

Expand All @@ -94,8 +90,9 @@ def sgr_rgb(key, value)
end + value[1, 6].scan(/../).map(&:hex).join(";")
end

def format_to_sgr(values)
sgr = "\e[" + values.map do |key, value|
def format_to_sgr(ordered_values)
sgr = "\e[" + ordered_values.map do |key_value|
key, value = key_value
case key
when :foreground, :background
case value
Expand Down
1 change: 1 addition & 0 deletions test/reline/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require 'reline'
require 'test/unit'
require 'test/unit/rr'

begin
require 'rbconfig'
Expand Down
11 changes: 11 additions & 0 deletions test/reline/test_face.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ def setup
@config = Reline::Face.const_get(:Config).new(:my_config) { }
end

def test_the_order_of_define_values_should_be_preserved
any_instance_of(Reline::Face.const_get(:Config)) do |config|
mock(config).format_to_sgr(
[[:foreground, :blue], [:style, [:bold, :italicized]], [:background, :red]]
)
end
Reline::Face.config(:my_config) do |face|
face.define :default, foreground: :blue, style: [:bold, :italicized], background: :red
end
end

def test_rgb?
assert_equal true, @config.send(:rgb_expression?, "#FFFFFF")
end
Expand Down

0 comments on commit c02205c

Please sign in to comment.