Skip to content

Commit

Permalink
Stop rerendering whole screen when adding newline at end of buffer
Browse files Browse the repository at this point in the history
The rendering time in IRB has been reduced as follows:

  start = Time.now

  def each_top_level_statement
    initialize_input
    catch(:TERM_INPUT) do
      loop do
        begin
          prompt
          unless l = lex
            throw :TERM_INPUT if @line == ''
          else
            @line_no += l.count("\n")
            next if l == "\n"
            @line.concat l
            if @code_block_open or @ltype or @continue or @indent > 0
              next
            end
          end
          if @line != "\n"
            @line.force_encoding(@io.encoding)
            yield @line, @exp_line_no
          end
          break if @io.eof?
          @line = ''
          @exp_line_no = @line_no

          @indent = 0
        rescue TerminateLineInput
          initialize_input
          prompt
        end
      end
    end
  end

  puts "Duration: #{Time.now - start} seconds"

0.33sec -> 0.22sec
  • Loading branch information
aycabta committed Nov 19, 2020
1 parent b78a7d2 commit 496c6a1
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/reline/line_editor.rb
Expand Up @@ -187,6 +187,7 @@ def reset_variables(prompt = '', encoding:)
@first_prompt = true
@searching_prompt = nil
@first_char = true
@add_newline_to_end_of_buffer = false
@eof = false
@continuous_insertion_buffer = String.new(encoding: @encoding)
reset_line
Expand Down Expand Up @@ -352,7 +353,22 @@ def rerender
end
new_highest_in_this = calculate_height_by_width(prompt_width + calculate_width(@line.nil? ? '' : @line))
# FIXME: end of logical line sometimes breaks
if @previous_line_index or new_highest_in_this != @highest_in_this
if @add_newline_to_end_of_buffer
scroll_down(1)
new_lines = whole_lines(index: @previous_line_index, line: @line)
prompt, prompt_width, prompt_list = check_multiline_prompt(new_lines, prompt)
@buffer_of_lines[@previous_line_index] = @line
@line = @buffer_of_lines[@line_index]
render_partial(prompt, prompt_width, @line, false)
@cursor = @cursor_max = calculate_width(@line)
@byte_pointer = @line.bytesize
@highest_in_all += @highest_in_this
@highest_in_this = calculate_height_by_width(prompt_width + @cursor_max)
@first_line_started_from += @started_from + 1
@started_from = calculate_height_by_width(prompt_width + @cursor) - 1
@previous_line_index = nil
@add_newline_to_end_of_buffer = false
elsif @previous_line_index or new_highest_in_this != @highest_in_this
if @previous_line_index
new_lines = whole_lines(index: @previous_line_index, line: @line)
else
Expand Down Expand Up @@ -1116,6 +1132,9 @@ def finish

private def key_newline(key)
if @is_multiline
if (@buffer_of_lines.size - 1) == @line_index and @line.bytesize == @byte_pointer
@add_newline_to_end_of_buffer = true
end
next_line = @line.byteslice(@byte_pointer, @line.bytesize - @byte_pointer)
cursor_line = @line.byteslice(0, @byte_pointer)
insert_new_line(cursor_line, next_line)
Expand Down

0 comments on commit 496c6a1

Please sign in to comment.