Skip to content

Commit 50166f7

Browse files
committed
support SGR color unavailable environment
JRuby with legacy console window on windows can not use ANSI control sequence. So SGR controls are displayed as garbage. Introduce character-only dialog display can provide reduced services. The current Command Prompt window and Terminal do not have this issue.
1 parent 8f1a188 commit 50166f7

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/reline/io/windows.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Reline::Windows < Reline::IO
44

55
attr_writer :output
66
attr_reader :jruby_p
7+
alias jruby? jruby_p
78
alias flush_before_control? jruby_p
89

910
def initialize
@@ -38,6 +39,10 @@ def initialize
3839
if @jruby_p && @legacy_console
3940
setconsolemode(getconsolemode() | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
4041
@legacy_console = getconsolemode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0
42+
if @legacy_console
43+
self.class.const_set(:RESET_COLOR, "")
44+
ENV['RELINE_ALT_SCROLLBAR'] = '1'
45+
end
4146
end
4247
end
4348

lib/reline/line_editor.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ def add_dialog_proc(name, p, context = nil)
700700
end
701701

702702
private def update_each_dialog(dialog, cursor_column, cursor_row, key = nil)
703+
no_color = Reline::IOGate.win? && Reline::IOGate.win_legacy_console? && Reline::IOGate.jruby?
704+
pointer_width = no_color ? 1 : 0
703705
dialog.set_cursor_pos(cursor_column, cursor_row)
704706
dialog_render_info = dialog.call(key)
705707
if dialog_render_info.nil? or dialog_render_info.contents.nil? or dialog_render_info.contents.empty?
@@ -743,6 +745,7 @@ def add_dialog_proc(name, p, context = nil)
743745
end
744746
dialog.column = dialog_render_info.pos.x
745747
dialog.width += @block_elem_width if scrollbar_pos
748+
dialog.width += pointer_width
746749
diff = (dialog.column + dialog.width) - screen_width
747750
if diff > 0
748751
dialog.column -= diff
@@ -759,12 +762,12 @@ def add_dialog_proc(name, p, context = nil)
759762
dialog.width = screen_width
760763
end
761764
face = Reline::Face[dialog_render_info.face || :default]
762-
scrollbar_sgr = face[:scrollbar]
763-
default_sgr = face[:default]
764-
enhanced_sgr = face[:enhanced]
765+
scrollbar_sgr = no_color ? "" : face[:scrollbar]
766+
default_sgr = no_color ? " " : face[:default]
767+
enhanced_sgr = no_color ? "*" : face[:enhanced]
765768
dialog.contents = contents.map.with_index do |item, i|
766769
line_sgr = i == pointer ? enhanced_sgr : default_sgr
767-
str_width = dialog.width - (scrollbar_pos.nil? ? 0 : @block_elem_width)
770+
str_width = dialog.width - (scrollbar_pos.nil? ? 0 : @block_elem_width) - pointer_width
768771
str, = Reline::Unicode.take_mbchar_range(item, 0, str_width, padding: true)
769772
colored_content = "#{line_sgr}#{str}"
770773
if scrollbar_pos

0 commit comments

Comments
 (0)