Skip to content

Commit

Permalink
Change to Hash-accessor style
Browse files Browse the repository at this point in the history
- Reline::Face[:completion_dialog].enhanced ->
  Reline::Face[:completion_dialog][:enhanced]
- Reline::Face.configs shows all defined values
  • Loading branch information
hasumikin committed Oct 23, 2023
1 parent 9286c59 commit f001348
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
17 changes: 14 additions & 3 deletions lib/reline/face.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,26 @@ class Config
RESET_SGR = "\e[0m".freeze

def initialize(name, &block)
@definition = {}
block.call(self)
ESSENTIAL_DEFINE_NAMES.each do |name|
define(name, style: :default) unless self.respond_to?(name)
@definition[name] ||= { style: :default, escape_sequence: RESET_SGR }
end
end

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
sgr = format_to_sgr(values)
define_singleton_method(name) { sgr }
values[:escape_sequence] = format_to_sgr(values).freeze
@definition[name] = values
end

def [](name)
@definition.dig(name, :escape_sequence) or raise ArgumentError, "unknown face: #{name}"
end

private
Expand Down Expand Up @@ -125,5 +132,9 @@ def self.config(name, &block)
@configs[name] = Config.new(name, &block)
end

def self.configs
@configs.transform_values(&:definition)
end

end

8 changes: 4 additions & 4 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ def add_dialog_proc(name, p, context = nil)
end
contents = contents[dialog.scroll_top, height]
end
if dialog_render_info.scrollbar and dialog_render_info.contents.size > height
if dialog_render_info[:scrollbar] and dialog_render_info.contents.size > height
bar_max_height = height * 2
moving_distance = (dialog_render_info.contents.size - height) * 2
position_ratio = dialog.scroll_top.zero? ? 0.0 : ((dialog.scroll_top * 2).to_f / moving_distance)
Expand Down Expand Up @@ -832,10 +832,10 @@ def add_dialog_proc(name, p, context = nil)
dialog.width = @screen_size.last
end
face = Reline::Face[dialog_render_info.face || :default]
scrollbar_sgr = face.scrollbar
default_sgr = face.default
scrollbar_sgr = face[:scrollbar]
default_sgr = face[:default]
dialog.contents = contents.map.with_index do |item, i|
line_sgr = i == pointer ? face.enhanced : default_sgr
line_sgr = i == pointer ? face[:enhanced] : default_sgr
str_width = dialog.width - (scrollbar_pos.nil? ? 0 : @block_elem_width)
str = padding_space_with_escape_sequences(Reline::Unicode.take_range(item, 0, str_width), str_width)
colored_content = "#{line_sgr}#{str}"
Expand Down
53 changes: 47 additions & 6 deletions test/reline/test_face.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@ def setup
end

def test_my_insufficient_config_line
assert_equal RESET_SGR, @face.default
assert_equal RESET_SGR, @face.enhanced
assert_equal RESET_SGR, @face.scrollbar
assert_equal RESET_SGR, @face[:default]
assert_equal RESET_SGR, @face[:enhanced]
assert_equal RESET_SGR, @face[:scrollbar]
end

def test_my_insufficient_configs
my_configs = Reline::Face.configs[:my_insufficient_config]
assert_equal(
{
default: { style: :default, escape_sequence: RESET_SGR },
enhanced: { style: :default, escape_sequence: RESET_SGR },
scrollbar: { style: :default, escape_sequence: RESET_SGR }
},
my_configs
)
end
end

Expand All @@ -31,12 +43,34 @@ def setup
@face = Reline::Face[:my_config]
end

def test_my_configs
my_configs = Reline::Face.configs[:my_config]
assert_equal(
{
default: {
escape_sequence: "#{RESET_SGR}\e[34m", foreground: :blue
},
enhanced: {
background: :black,
foreground: "#FF1020",
style: [:bold, :underlined],
escape_sequence: "\e[0m\e[38;2;255;16;32;40;1;4m"
},
scrollbar: {
style: :default,
escape_sequence: "\e[0m"
}
},
my_configs
)
end

def test_my_config_line
assert_equal "#{RESET_SGR}\e[34m", @face.default
assert_equal "#{RESET_SGR}\e[34m", @face[:default]
end

def test_my_config_enhanced
assert_equal "#{RESET_SGR}\e[38;2;255;16;32;40;1;4m", @face.enhanced
assert_equal "#{RESET_SGR}\e[38;2;255;16;32;40;1;4m", @face[:enhanced]
end

def test_not_respond_to_another_label
Expand All @@ -51,7 +85,14 @@ def test_my_config_default
# do nothing
end
face = Reline::Face[:my_config]
assert_equal RESET_SGR, face.default
assert_equal RESET_SGR, face[:default]
end

def test_style_does_not_exist
face = Reline::Face[:default]
assert_raise ArgumentError do
face[:style_does_not_exist]
end
end

def test_invalid_keyword
Expand Down

0 comments on commit f001348

Please sign in to comment.