Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure no escape sequence before printing prompt #716

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading