summaryrefslogtreecommitdiff
path: root/lib/reline
diff options
context:
space:
mode:
authorst0012 <stan001212@gmail.com>2022-06-28 14:39:56 +0100
committergit <svn-admin@ruby-lang.org>2022-07-16 02:30:23 +0900
commit36ca0e58b600f3338ad4880d77c8c7fbc8f51460 (patch)
tree31bc41b1c4b4526f10c72cec587d5e66a6d945da /lib/reline
parent280b805d040fa537d5a459b40d4bfa6d49700905 (diff)
[ruby/reline] Use color name instead of code (integer) in dialog color APIs
As pointed out in the [comment](https://github.com/ruby/reline/pull/413#issuecomment-1168033973), the code is actually a control sequence and not only for colors. To make the dialog color APIs safer to use, we should restrict its usages and extract away the bg/fg concept from the input. So in this commit, I made these changes: 1. The dialog_*_bg/fg_color APIs only takes and returns color names (symbol): - :black - :red - :green - :yellow - :blue - :magenta - :cyan - :white 2. Add additional dialog_*_bg/fg_color_sequence APIs to access the raw code. https://github.com/ruby/reline/commit/b32a977766
Diffstat (limited to 'lib/reline')
-rw-r--r--lib/reline/config.rb89
1 files changed, 73 insertions, 16 deletions
diff --git a/lib/reline/config.rb b/lib/reline/config.rb
index b1cb7645ea..cef7735bdb 100644
--- a/lib/reline/config.rb
+++ b/lib/reline/config.rb
@@ -45,13 +45,11 @@ class Reline::Config
attr_accessor v
end
- attr_accessor(
- :autocompletion,
- :dialog_default_bg_color,
- :dialog_default_fg_color,
- :dialog_pointer_bg_color,
- :dialog_pointer_fg_color,
- )
+ attr_accessor :autocompletion
+ attr_reader :dialog_default_bg_color_sequence,
+ :dialog_default_fg_color_sequence,
+ :dialog_pointer_bg_color_sequence,
+ :dialog_pointer_fg_color_sequence
def initialize
@additional_key_bindings = {} # from inputrc
@@ -77,10 +75,10 @@ class Reline::Config
@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
+ @dialog_default_bg_color_sequence = nil
+ @dialog_pointer_bg_color_sequence = nil
+ @dialog_default_fg_color_sequence = nil
+ @dialog_pointer_fg_color_sequence = nil
end
def reset
@@ -106,6 +104,65 @@ class Reline::Config
(val.respond_to?(:any?) ? val : [val]).any?(@editing_mode_label)
end
+ def dialog_default_bg_color=(color)
+ @dialog_default_bg_color_sequence = dialog_color_to_code(:bg, color)
+ end
+
+ def dialog_default_fg_color=(color)
+ @dialog_default_fg_color_sequence = dialog_color_to_code(:fg, color)
+ end
+
+ def dialog_pointer_bg_color=(color)
+ @dialog_pointer_bg_color_sequence = dialog_color_to_code(:bg, color)
+ end
+
+ def dialog_pointer_fg_color=(color)
+ @dialog_pointer_fg_color_sequence = dialog_color_to_code(:fg, color)
+ end
+
+ def dialog_default_bg_color
+ dialog_code_to_color(:bg, @dialog_default_bg_color_sequence)
+ end
+
+ def dialog_default_fg_color
+ dialog_code_to_color(:fg, @dialog_default_fg_color_sequence)
+ end
+
+ def dialog_pointer_bg_color
+ dialog_code_to_color(:bg, @dialog_pointer_bg_color_sequence)
+ end
+
+ def dialog_pointer_fg_color
+ dialog_code_to_color(:fg, @dialog_pointer_fg_color_sequence)
+ end
+
+ COLORS = [
+ :black,
+ :red,
+ :green,
+ :yellow,
+ :blue,
+ :magenta,
+ :cyan,
+ :white
+ ].freeze
+
+ private def dialog_color_to_code(type, color)
+ base = type == :bg ? 40 : 30
+ c = COLORS.index(color.to_sym)
+
+ if c
+ base + c
+ else
+ raise ArgumentError.new("Unknown color: #{color}.\nAvailable colors: #{COLORS.join(", ")}")
+ end
+ end
+
+ private def dialog_code_to_color(type, code)
+ base = type == :bg ? 40 : 30
+ COLORS[code - base]
+ end
+
def keymap
@key_actors[@keymap_label]
end
@@ -339,13 +396,13 @@ class Reline::Config
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
+ self.dialog_default_bg_color = value
when 'dialog-default-fg-color'
- @dialog_default_fg_color = value.to_i
+ self.dialog_default_fg_color = value
+ when 'dialog-pointer-bg-color'
+ self.dialog_pointer_bg_color = value
when 'dialog-pointer-fg-color'
- @dialog_pointer_fg_color = value.to_i
+ self.dialog_pointer_fg_color = value
when *VARIABLE_NAMES then
variable_name = :"@#{name.tr(?-, ?_)}"
instance_variable_set(variable_name, value.nil? || value == '1' || value == 'on')