Skip to content

Commit

Permalink
Ensure no escape sequence before printing prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Jun 4, 2024
1 parent 2d68284 commit 6e006b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/reline/io/ansi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def set_screen_size(rows, columns)
end

def cursor_pos
if @input.tty? && @output.tty?
if both_tty?
res = +''
m = nil
@input.raw do |stdin|
Expand Down Expand Up @@ -276,6 +276,10 @@ def cursor_pos
Reline::CursorPos.new(column, row)
end

def both_tty?
@input.tty? && @output.tty?
end

def move_cursor_column(x)
@output.write "\e[#{x + 1}G"
end
Expand Down Expand Up @@ -343,14 +347,14 @@ def set_winch_handler(&handler)

def prep
# Enable bracketed paste
@output.write "\e[?2004h" if Reline.core.config.enable_bracketed_paste
@output.write "\e[?2004h" if Reline.core.config.enable_bracketed_paste && both_tty?
retrieve_keybuffer
nil
end

def deprep(otio)
# Disable bracketed paste
@output.write "\e[?2004l" if Reline.core.config.enable_bracketed_paste
@output.write "\e[?2004l" if Reline.core.config.enable_bracketed_paste && both_tty?
Signal.trap('WINCH', @old_winch_handler) if @old_winch_handler
end
end
12 changes: 12 additions & 0 deletions test/reline/test_reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,18 @@ def test_dumb_terminal
assert_match(/#<Reline::Dumb/, out.chomp)
end

def test_print_prompt_before_everything_else
pend if win?
lib = File.expand_path("../../lib", __dir__)
code = "p Reline::IOGate.class; p Reline.readline 'prompt> '"
out = IO.popen([Reline.test_rubybin, "-I#{lib}", "-rreline", "-e", code], "r+") do |io|
io.write "abc\n"
io.close_write
io.read
end
assert_match(/\AReline::ANSI\nprompt> /, out)
end

def test_require_reline_should_not_trigger_winsize
pend if win?
lib = File.expand_path("../../lib", __dir__)
Expand Down

0 comments on commit 6e006b0

Please sign in to comment.