Skip to content

Commit 3e60610

Browse files
committed
fix JRuby display issue
JRuby with newer JVM dows not seems to flush stdout at an expected timing. By explicitly flushing the output, the screen state is finalized before issuing win32api calls.
1 parent 9b13455 commit 3e60610

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

lib/reline/io/windows.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
class Reline::Windows < Reline::IO
44

55
attr_writer :output
6+
attr_reader :jruby_p
7+
alias flush_before_control? jruby_p
68

79
def initialize
810
@input_buf = []
@@ -32,6 +34,7 @@ def initialize
3234
@WaitForSingleObject = Win32API.new('kernel32', 'WaitForSingleObject', ['L', 'L'], 'L')
3335

3436
@legacy_console = getconsolemode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0
37+
@jruby_p = RUBY_ENGINE == 'jruby'
3538
end
3639

3740
def encoding
@@ -182,11 +185,6 @@ def call(*args)
182185
call_with_console_handle(@SetConsoleMode, mode)
183186
end
184187

185-
#if @legacy_console
186-
# setconsolemode(getconsolemode() | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
187-
# @legacy_console = (getconsolemode() & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0)
188-
#end
189-
190188
def msys_tty?(io = @hConsoleInputHandle)
191189
# check if fd is a pipe
192190
if @GetFileType.call(io) != FILE_TYPE_PIPE
@@ -366,22 +364,26 @@ def get_console_screen_buffer_info
366364
ALTERNATIVE_CSBI = [80, 24, 0, 0, 7, 0, 0, 79, 23].freeze
367365

368366
def get_screen_size
367+
@output.flush if flush_before_control?
369368
width, _, _, _, _, _, top, _, bottom = get_console_screen_buffer_info || ALTERNATIVE_CSBI
370369
[bottom - top + 1, width]
371370
end
372371

373372
def cursor_pos
373+
@output.flush if flush_before_control?
374374
_, _, x, y, _, _, top, = get_console_screen_buffer_info || ALTERNATIVE_CSBI
375375
Reline::CursorPos.new(x, y - top)
376376
end
377377

378378
def move_cursor_column(val)
379+
@output.flush if flush_before_control?
379380
_, _, _, y, = get_console_screen_buffer_info
380381
call_with_console_handle(@SetConsoleCursorPosition, y * 65536 + val) if y
381382
end
382383

383384
def move_cursor_up(val)
384385
if val > 0
386+
@output.flush if flush_before_control?
385387
_, _, x, y, _, _, top, = get_console_screen_buffer_info
386388
return unless y
387389
y = (y - top) - val
@@ -394,6 +396,7 @@ def move_cursor_up(val)
394396

395397
def move_cursor_down(val)
396398
if val > 0
399+
@output.flush if flush_before_control?
397400
_, _, x, y, _, _, top, _, bottom = get_console_screen_buffer_info
398401
return unless y
399402
screen_height = bottom - top
@@ -406,6 +409,7 @@ def move_cursor_down(val)
406409
end
407410

408411
def erase_after_cursor
412+
@output.flush if flush_before_control?
409413
width, _, x, y, attributes, = get_console_screen_buffer_info
410414
return unless x
411415
written = 0.chr * 4
@@ -423,6 +427,7 @@ def scroll_down(x)
423427

424428
def clear_screen
425429
if @legacy_console
430+
@output.flush if flush_before_control?
426431
width, _, _, _, attributes, _, top, _, bottom = get_console_screen_buffer_info
427432
return unless width
428433
fill_length = width * (bottom - top + 1)

0 commit comments

Comments
 (0)