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

Fix bracketed paste and scrolling bug #801

Merged
merged 2 commits into from
Jan 5, 2025

Conversation

tompng
Copy link
Member

@tompng tompng commented Jan 2, 2025

Reproduce

Pasting 10 lines of code into terminal with screen height = 5

Actual screen (Reline 0.6.0)
irb(main):001> 1.tap do
irb(main):002>   2
irb(main):003>   3 
irb(main):004>   4
irb(main):005>   5█

Expected screen (Reline <= 0.5.12)
irb(main):006>   6
irb(main):007>   7
irb(main):008>   8 
irb(main):009>   9
irb(main):010> end█
  1. If you press Ctrl+C, irb crashes with the error below. Pressing other key will update the screen to the expected result.
/Users/tomoya.ishida/.rbenv/versions/3.4.1/lib/ruby/3.4.0/reline/io/ansi.rb:289:in 'String#*': negative argument (ArgumentError)

    write "\n" * x
                 ^
	from /Users/tomoya.ishida/.rbenv/versions/3.4.1/lib/ruby/3.4.0/reline/io/ansi.rb:289:in 'Reline::ANSI#scroll_down'
	from /Users/tomoya.ishida/.rbenv/versions/3.4.1/lib/ruby/3.4.0/reline/line_editor.rb:192:in 'Reline::LineEditor#handle_interrupted'
	from /Users/tomoya.ishida/.rbenv/versions/3.4.1/lib/ruby/3.4.0/reline/line_editor.rb:169:in 'Reline::LineEditor#handle_signal'

Reason

# Set pasting=true because the content of bracketed_paste is not read from stdin
line_editor.set_pasting_state(io_gate.in_pasting?)

inputs.each do |key| # inputs.size is always 1 if you don't have custom .inputrc file
  ...
  key = Reline::Key.new(io_gate.read_bracketed_paste, :insert_multiline_text) # everything is all read from stdin
  ...
  # Calls update with pasting state = true. In this case, scroll_into_view is skipped (Bug1)
  line_editor.update(key)
end

# set pasting state to false
line_editor.set_pasting_state(io_gate.in_pasting?)
# renders the screen with cursor out of the screen. rendered_screen.cursor_y will be larger than screen height (Bug2)
line_editor.rerender

Fixes

Bug1: scroll_into_view wrongly skipped, is fixed by the change in lib/reline.rb
Bug2: Fixed in lib/line_editor.rb, in case of other unknown bug that cause calling render with cursor position out of the screen.

Copy link
Member

@ima1zumi ima1zumi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix!

@ima1zumi ima1zumi merged commit c5d5c44 into ruby:master Jan 5, 2025
45 checks passed
matzbot pushed a commit to ruby/ruby that referenced this pull request Jan 5, 2025
(ruby/reline#801)

* Fix set_pasting_state bug with bracketed paste

* Fix rendered cursor_y caching in case of rendering the screen without scroll_into_view

ruby/reline@c5d5c444df
@tompng tompng deleted the bracketed_paste_in_pasting_fix branch January 5, 2025 06:23
@ima1zumi ima1zumi added the bug Something isn't working label Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging this pull request may close these issues.

2 participants