ruby-changes:58281
From: aycabta <ko1@a...>
Date: Thu, 17 Oct 2019 01:58:56 +0900 (JST)
Subject: [ruby-changes:58281] e26c6d4ab8 (master): Collect multiline prompt logic into a method
https://git.ruby-lang.org/ruby.git/commit/?id=e26c6d4ab8 From e26c6d4ab840c100c4f3ad20d0399598ef427c13 Mon Sep 17 00:00:00 2001 From: aycabta <aycabta@g...> Date: Thu, 17 Oct 2019 01:51:29 +0900 Subject: Collect multiline prompt logic into a method diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index a62ed0e..5ba7039 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -60,6 +60,18 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L60 reset_variables end + private def check_multiline_prompt(buffer, prompt, special_prompt) + if @prompt_proc + prompt_list = @prompt_proc.(buffer) + prompt_list[@line_index] = special_prompt if special_prompt + prompt = prompt_list[@line_index] + prompt_width = calculate_width(prompt, true) + [prompt, prompt_list] + else + [prompt, nil] + end + end + def reset(prompt = '', encoding = Encoding.default_external) @rest_height = (Reline::IOGate.get_screen_size.first - 1) - Reline::IOGate.cursor_pos.y @screen_size = Reline::IOGate.get_screen_size @@ -91,13 +103,7 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L103 end back = 0 new_buffer = whole_lines - prompt_list = nil - if @prompt_proc - prompt_list = @prompt_proc.(new_buffer) - prompt_list[@line_index] = special_prompt if special_prompt - prompt = prompt_list[@line_index] - prompt_width = calculate_width(prompt, true) - end + prompt, prompt_list = check_multiline_prompt(new_buffer, prompt, special_prompt) new_buffer.each_with_index do |line, index| prompt_width = calculate_width(prompt_list[index], true) if @prompt_proc width = prompt_width + calculate_width(line) @@ -329,13 +335,7 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L335 Reline::IOGate.clear_screen @cleared = false back = 0 - prompt_list = nil - if @prompt_proc - prompt_list = @prompt_proc.(whole_lines) - prompt_list[@line_index] = special_prompt if special_prompt - prompt = prompt_list[@line_index] - prompt_width = calculate_width(prompt, true) - end + prompt, prompt_list = check_multiline_prompt(whole_lines, prompt, special_prompt) modify_lines(whole_lines).each_with_index do |line, index| if @prompt_proc pr = prompt_list[index] @@ -361,13 +361,7 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L361 else new_lines = whole_lines end - prompt_list = nil - if @prompt_proc - prompt_list = @prompt_proc.(new_lines) - prompt_list[@line_index] = special_prompt if special_prompt - prompt = prompt_list[@line_index] - prompt_width = calculate_width(prompt, true) - end + prompt, prompt_list = check_multiline_prompt(new_lines, prompt, special_prompt) all_height = new_lines.inject(0) { |result, line| result + calculate_height_by_width(prompt_width + calculate_width(line)) # TODO prompt_list } @@ -431,13 +425,7 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L425 Reline::IOGate.move_cursor_column(0) back = 0 new_buffer = whole_lines - prompt_list = nil - if @prompt_proc - prompt_list = @prompt_proc.(new_buffer) - prompt_list[@line_index] = special_prompt if special_prompt - prompt = prompt_list[@line_index] - prompt_width = calculate_width(prompt, true) - end + prompt, prompt_list = check_multiline_prompt(new_buffer, prompt, special_prompt) new_buffer.each_with_index do |line, index| prompt_width = calculate_width(prompt_list[index], true) if @prompt_proc width = prompt_width + calculate_width(line) @@ -489,13 +477,7 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L477 end line = modify_lines(whole_lines)[@line_index] if @is_multiline - prompt_list = nil - if @prompt_proc - prompt_list = @prompt_proc.(whole_lines) - prompt_list[@line_index] = special_prompt if special_prompt - prompt = prompt_list[@line_index] - prompt_width = calculate_width(prompt, true) - end + prompt, prompt_list = check_multiline_prompt(whole_lines, prompt, special_prompt) if finished? # Always rerender on finish because output_modifier_proc may return a different output. render_partial(prompt, prompt_width, line) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/