Skip to content

Commit bd49537

Browse files
authored
Enable to change the background color of dialogs. (#413)
1 parent eb17de0 commit bd49537

File tree

4 files changed

+83
-25
lines changed

4 files changed

+83
-25
lines changed

lib/reline.rb

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ def match?(other)
3333
alias_method :==, :match?
3434
end
3535
CursorPos = Struct.new(:x, :y)
36-
DialogRenderInfo = Struct.new(:pos, :contents, :bg_color, :width, :height, :scrollbar, keyword_init: true)
36+
DialogRenderInfo = Struct.new(
37+
:pos,
38+
:contents,
39+
:bg_color,
40+
:pointer_bg_color,
41+
:fg_color,
42+
:pointer_fg_color,
43+
:width,
44+
:height,
45+
:scrollbar,
46+
keyword_init: true
47+
)
3748

3849
class Core
3950
ATTR_READER_NAMES = %i(
@@ -58,6 +69,19 @@ class Core
5869
attr_accessor :last_incremental_search
5970
attr_reader :output
6071

72+
extend Forwardable
73+
def_delegators :config,
74+
:autocompletion,
75+
:autocompletion=,
76+
:dialog_default_bg_color,
77+
:dialog_default_bg_color=,
78+
:dialog_default_fg_color,
79+
:dialog_default_fg_color=,
80+
:dialog_pointer_bg_color,
81+
:dialog_pointer_bg_color=,
82+
:dialog_pointer_fg_color,
83+
:dialog_pointer_fg_color=
84+
6185
def initialize
6286
self.output = STDOUT
6387
@dialog_proc_list = {}
@@ -123,14 +147,6 @@ def completion_proc=(p)
123147
@completion_proc = p
124148
end
125149

126-
def autocompletion
127-
@config.autocompletion
128-
end
129-
130-
def autocompletion=(val)
131-
@config.autocompletion = val
132-
end
133-
134150
def output_modifier_proc=(p)
135151
raise ArgumentError unless p.respond_to?(:call) or p.nil?
136152
@output_modifier_proc = p
@@ -243,7 +259,16 @@ def get_screen_size
243259
context.push(cursor_pos_to_render, result, pointer, dialog)
244260
end
245261
dialog.pointer = pointer
246-
DialogRenderInfo.new(pos: cursor_pos_to_render, contents: result, scrollbar: true, height: 15)
262+
DialogRenderInfo.new(
263+
pos: cursor_pos_to_render,
264+
contents: result,
265+
scrollbar: true,
266+
height: 15,
267+
bg_color: config.dialog_default_bg_color,
268+
pointer_bg_color: config.dialog_pointer_bg_color,
269+
fg_color: config.dialog_default_fg_color,
270+
pointer_fg_color: config.dialog_pointer_fg_color
271+
)
247272
}
248273
Reline::DEFAULT_DIALOG_CONTEXT = Array.new
249274

@@ -528,6 +553,10 @@ def self.insert_text(*args, &block)
528553
def_single_delegators :core, :add_dialog_proc
529554
def_single_delegators :core, :dialog_proc
530555
def_single_delegators :core, :autocompletion, :autocompletion=
556+
def_single_delegators :core, :dialog_default_bg_color, :dialog_default_bg_color=
557+
def_single_delegators :core, :dialog_pointer_bg_color, :dialog_pointer_bg_color=
558+
def_single_delegators :core, :dialog_default_fg_color, :dialog_default_fg_color=
559+
def_single_delegators :core, :dialog_pointer_fg_color, :dialog_pointer_fg_color=
531560

532561
def_single_delegators :core, :readmultiline
533562
def_instance_delegators self, :readmultiline
@@ -550,6 +579,10 @@ def self.core
550579
core.filename_quote_characters = ""
551580
core.special_prefixes = ""
552581
core.add_dialog_proc(:autocomplete, Reline::DEFAULT_DIALOG_PROC_AUTOCOMPLETE, Reline::DEFAULT_DIALOG_CONTEXT)
582+
core.dialog_default_bg_color = 46 # Cyan
583+
core.dialog_default_fg_color = 37 # White
584+
core.dialog_pointer_bg_color = 45 # Magenta
585+
core.dialog_pointer_fg_color = 37 # White
553586
}
554587
end
555588

lib/reline/config.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ class InvalidInputrc < RuntimeError
4545
attr_accessor v
4646
end
4747

48+
attr_accessor(
49+
:autocompletion,
50+
:dialog_default_bg_color,
51+
:dialog_default_fg_color,
52+
:dialog_pointer_bg_color,
53+
:dialog_pointer_fg_color,
54+
)
55+
4856
def initialize
4957
@additional_key_bindings = {} # from inputrc
5058
@additional_key_bindings[:emacs] = {}
@@ -69,6 +77,10 @@ def initialize
6977
@test_mode = false
7078
@autocompletion = false
7179
@convert_meta = true if seven_bit_encoding?(Reline::IOGate.encoding)
80+
@dialog_default_bg_color = nil
81+
@dialog_pointer_bg_color = nil
82+
@dialog_default_fg_color = nil
83+
@dialog_pointer_fg_color = nil
7284
end
7385

7486
def reset
@@ -94,14 +106,6 @@ def editing_mode_is?(*val)
94106
(val.respond_to?(:any?) ? val : [val]).any?(@editing_mode_label)
95107
end
96108

97-
def autocompletion=(val)
98-
@autocompletion = val
99-
end
100-
101-
def autocompletion
102-
@autocompletion
103-
end
104-
105109
def keymap
106110
@key_actors[@keymap_label]
107111
end
@@ -334,6 +338,14 @@ def bind_variable(name, value)
334338
@vi_ins_mode_string = retrieve_string(value)
335339
when 'emacs-mode-string'
336340
@emacs_mode_string = retrieve_string(value)
341+
when 'dialog-default-bg-color'
342+
@dialog_default_bg_color = value.to_i
343+
when 'dialog-pointer-bg-color'
344+
@dialog_pointer_bg_color = value.to_i
345+
when 'dialog-default-fg-color'
346+
@dialog_default_fg_color = value.to_i
347+
when 'dialog-pointer-fg-color'
348+
@dialog_pointer_fg_color = value.to_i
337349
when *VARIABLE_NAMES then
338350
variable_name = :"@#{name.tr(?-, ?_)}"
339351
instance_variable_set(variable_name, value.nil? || value == '1' || value == 'on')

lib/reline/line_editor.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -743,17 +743,15 @@ def add_dialog_proc(name, p, context = nil)
743743
Reline::IOGate.move_cursor_column(dialog.column)
744744
dialog.contents.each_with_index do |item, i|
745745
if i == pointer
746-
bg_color = '45'
746+
fg_color = dialog_render_info.pointer_fg_color
747+
bg_color = dialog_render_info.pointer_bg_color
747748
else
748-
if dialog_render_info.bg_color
749-
bg_color = dialog_render_info.bg_color
750-
else
751-
bg_color = '46'
752-
end
749+
fg_color = dialog_render_info.fg_color
750+
bg_color = dialog_render_info.bg_color
753751
end
754752
str_width = dialog.width - (dialog.scrollbar_pos.nil? ? 0 : @block_elem_width)
755753
str = padding_space_with_escape_sequences(Reline::Unicode.take_range(item, 0, str_width), str_width)
756-
@output.write "\e[#{bg_color}m#{str}"
754+
@output.write "\e[#{bg_color}m\e[#{fg_color}m#{str}"
757755
if dialog.scrollbar_pos and (dialog.scrollbar_pos != old_dialog.scrollbar_pos or dialog.column != old_dialog.column)
758756
@output.write "\e[37m"
759757
if dialog.scrollbar_pos <= (i * 2) and (i * 2 + 1) < (dialog.scrollbar_pos + bar_height)

test/reline/test_config.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,19 @@ def test_relative_xdg_config_home
408408
ENV['XDG_CONFIG_HOME'] = xdg_config_home_backup
409409
ENV['HOME'] = home_backup
410410
end
411+
412+
def test_dialog_configurations
413+
@config.read_lines(<<~LINES.lines)
414+
set dialog-default-bg-color 1
415+
set dialog-pointer-bg-color 2
416+
set dialog-default-fg-color 3
417+
set dialog-pointer-fg-color 4
418+
LINES
419+
420+
assert_equal 1, @config.dialog_default_bg_color
421+
assert_equal 2, @config.dialog_pointer_bg_color
422+
assert_equal 3, @config.dialog_default_fg_color
423+
assert_equal 4, @config.dialog_pointer_fg_color
424+
end
411425
end
426+

0 commit comments

Comments
 (0)