ruby-changes:72335
From: pocari <ko1@a...>
Date: Mon, 27 Jun 2022 22:29:03 +0900 (JST)
Subject: [ruby-changes:72335] 8c6c3e30f3 (master): [ruby/reline] Enable to change the background color of dialogs. (https://github.com/ruby/reline/pull/413)
https://git.ruby-lang.org/ruby.git/commit/?id=8c6c3e30f3 From 8c6c3e30f3a86ba0b697a0d99efe8ff4585c4a42 Mon Sep 17 00:00:00 2001 From: pocari <caffelattenonsugar@g...> Date: Mon, 27 Jun 2022 22:28:34 +0900 Subject: [ruby/reline] Enable to change the background color of dialogs. (https://github.com/ruby/reline/pull/413) https://github.com/ruby/reline/commit/bd49537964 --- lib/reline.rb | 53 +++++++++++++++++++++++++++++++++++++--------- lib/reline/config.rb | 28 +++++++++++++++++------- lib/reline/line_editor.rb | 12 +++++------ test/reline/test_config.rb | 15 +++++++++++++ 4 files changed, 83 insertions(+), 25 deletions(-) diff --git a/lib/reline.rb b/lib/reline.rb index a57b570544..21e2dbf095 100644 --- a/lib/reline.rb +++ b/lib/reline.rb @@ -33,7 +33,18 @@ module Reline https://github.com/ruby/ruby/blob/trunk/lib/reline.rb#L33 alias_method :==, :match? end CursorPos = Struct.new(:x, :y) - DialogRenderInfo = Struct.new(:pos, :contents, :bg_color, :width, :height, :scrollbar, keyword_init: true) + DialogRenderInfo = Struct.new( + :pos, + :contents, + :bg_color, + :pointer_bg_color, + :fg_color, + :pointer_fg_color, + :width, + :height, + :scrollbar, + keyword_init: true + ) class Core ATTR_READER_NAMES = %i( @@ -58,6 +69,19 @@ module Reline https://github.com/ruby/ruby/blob/trunk/lib/reline.rb#L69 attr_accessor :last_incremental_search attr_reader :output + extend Forwardable + def_delegators :config, + :autocompletion, + :autocompletion=, + :dialog_default_bg_color, + :dialog_default_bg_color=, + :dialog_default_fg_color, + :dialog_default_fg_color=, + :dialog_pointer_bg_color, + :dialog_pointer_bg_color=, + :dialog_pointer_fg_color, + :dialog_pointer_fg_color= + def initialize self.output = STDOUT @dialog_proc_list = {} @@ -123,14 +147,6 @@ module Reline https://github.com/ruby/ruby/blob/trunk/lib/reline.rb#L147 @completion_proc = p end - def autocompletion - @config.autocompletion - end - - def autocompletion=(val) - @config.autocompletion = val - end - def output_modifier_proc=(p) raise ArgumentError unless p.respond_to?(:call) or p.nil? @output_modifier_proc = p @@ -243,7 +259,16 @@ module Reline https://github.com/ruby/ruby/blob/trunk/lib/reline.rb#L259 context.push(cursor_pos_to_render, result, pointer, dialog) end dialog.pointer = pointer - DialogRenderInfo.new(pos: cursor_pos_to_render, contents: result, scrollbar: true, height: 15) + DialogRenderInfo.new( + pos: cursor_pos_to_render, + contents: result, + scrollbar: true, + height: 15, + bg_color: config.dialog_default_bg_color, + pointer_bg_color: config.dialog_pointer_bg_color, + fg_color: config.dialog_default_fg_color, + pointer_fg_color: config.dialog_pointer_fg_color + ) } Reline::DEFAULT_DIALOG_CONTEXT = Array.new @@ -528,6 +553,10 @@ module Reline https://github.com/ruby/ruby/blob/trunk/lib/reline.rb#L553 def_single_delegators :core, :add_dialog_proc def_single_delegators :core, :dialog_proc def_single_delegators :core, :autocompletion, :autocompletion= + def_single_delegators :core, :dialog_default_bg_color, :dialog_default_bg_color= + def_single_delegators :core, :dialog_pointer_bg_color, :dialog_pointer_bg_color= + def_single_delegators :core, :dialog_default_fg_color, :dialog_default_fg_color= + def_single_delegators :core, :dialog_pointer_fg_color, :dialog_pointer_fg_color= def_single_delegators :core, :readmultiline def_instance_delegators self, :readmultiline @@ -550,6 +579,10 @@ module Reline https://github.com/ruby/ruby/blob/trunk/lib/reline.rb#L579 core.filename_quote_characters = "" core.special_prefixes = "" core.add_dialog_proc(:autocomplete, Reline::DEFAULT_DIALOG_PROC_AUTOCOMPLETE, Reline::DEFAULT_DIALOG_CONTEXT) + core.dialog_default_bg_color = 46 # Cyan + core.dialog_default_fg_color = 37 # White + core.dialog_pointer_bg_color = 45 # Magenta + core.dialog_pointer_fg_color = 37 # White } end diff --git a/lib/reline/config.rb b/lib/reline/config.rb index 1bb12a2506..b1cb7645ea 100644 --- a/lib/reline/config.rb +++ b/lib/reline/config.rb @@ -45,6 +45,14 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L45 attr_accessor v end + attr_accessor( + :autocompletion, + :dialog_default_bg_color, + :dialog_default_fg_color, + :dialog_pointer_bg_color, + :dialog_pointer_fg_color, + ) + def initialize @additional_key_bindings = {} # from inputrc @additional_key_bindings[:emacs] = {} @@ -69,6 +77,10 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L77 @test_mode = false @autocompletion = false @convert_meta = true if seven_bit_encoding?(Reline::IOGate.encoding) + @dialog_default_bg_color = nil + @dialog_pointer_bg_color = nil + @dialog_default_fg_color = nil + @dialog_pointer_fg_color = nil end def reset @@ -94,14 +106,6 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L106 (val.respond_to?(:any?) ? val : [val]).any?(@editing_mode_label) end - def autocompletion=(val) - @autocompletion = val - end - - def autocompletion - @autocompletion - end - def keymap @key_actors[@keymap_label] end @@ -334,6 +338,14 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L338 @vi_ins_mode_string = retrieve_string(value) when 'emacs-mode-string' @emacs_mode_string = retrieve_string(value) + when 'dialog-default-bg-color' + @dialog_default_bg_color = value.to_i + when 'dialog-pointer-bg-color' + @dialog_pointer_bg_color = value.to_i + when 'dialog-default-fg-color' + @dialog_default_fg_color = value.to_i + when 'dialog-pointer-fg-color' + @dialog_pointer_fg_color = value.to_i when *VARIABLE_NAMES then variable_name = :"@#{name.tr(?-, ?_)}" instance_variable_set(variable_name, value.nil? || value == '1' || value == 'on') diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 135432a5d1..8d0719ef7c 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -743,17 +743,15 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L743 Reline::IOGate.move_cursor_column(dialog.column) dialog.contents.each_with_index do |item, i| if i == pointer - bg_color = '45' + fg_color = dialog_render_info.pointer_fg_color + bg_color = dialog_render_info.pointer_bg_color else - if dialog_render_info.bg_color - bg_color = dialog_render_info.bg_color - else - bg_color = '46' - end + fg_color = dialog_render_info.fg_color + bg_color = dialog_render_info.bg_color end str_width = dialog.width - (dialog.scrollbar_pos.nil? ? 0 : @block_elem_width) str = padding_space_with_escape_sequences(Reline::Unicode.take_range(item, 0, str_width), str_width) - @output.write "\e[#{bg_color}m#{str}" + @output.write "\e[#{bg_color}m\e[#{fg_color}m#{str}" if dialog.scrollbar_pos and (dialog.scrollbar_pos != old_dialog.scrollbar_pos or dialog.column != old_dialog.column) @output.write "\e[37m" if dialog.scrollbar_pos <= (i * 2) and (i * 2 + 1) < (dialog.scrollbar_pos + bar_height) diff --git a/test/reline/test_config.rb b/test/reline/test_config.rb index 99d190d246..234eee11f7 100644 --- a/test/reline/test_config.rb +++ b/test/reline/test_config.rb @@ -408,4 +408,19 @@ class Reline::Config::Test < Reline::TestCase https://github.com/ruby/ruby/blob/trunk/test/reline/test_config.rb#L408 ENV['XDG_CONFIG_HOME'] = xdg_config_home_backup ENV['HOME'] = home_backup end + + def test_dialog_configurations + @config.read_lines(<<~LINES.lines) + set dialog-default-bg-color 1 + set dialog-pointer-bg-color 2 + set dialog-default-fg-color 3 + set dialog-pointer-fg-color 4 + LINES + + assert_equal 1, @config.dialog_default_bg_color + assert_equal 2, @config.dialog_pointer_bg_color + assert_equal 3, @config.dialog_default_fg_color + assert_equal 4, @config.dialog_pointer_fg_color + end end + -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/