class MiniReadline::EditWindow
Support for the edit window. :reek: TooManyInstanceVariables – Yes and it needs them!
Keep the cursor in sync.
Keeping the screen in sync.
Attributes
The width of the window with the base prompt
What is the offset of the window's left margin?
The width of the window with the alternate prompt
The shadow copy of what is actually on the screen?
Public Class Methods
Determine the edit window limits.
# File lib/mini_readline/read_line/edit/edit_window.rb, line 14 def initialize(options) @options = options @base_width = window_width - @options[:base_prompt].length @scroll_width = window_width - @options[:scroll_prompt].length @left_margin, @window_buffer, @show_prompt = 0, "", true end
Public Instance Methods
How wide is the active region of the window now?
# File lib/mini_readline/read_line/edit/edit_window.rb, line 50 def active_width window_scrolled? ? scroll_width : base_width end
Compute what should be on the screen.
# File lib/mini_readline/read_line/edit/edit_window/sync_window.rb, line 37 def build_screen_image(edit_buffer) working_region = edit_buffer[left_margin..right_margin] if (mask = @options[:secret_mask]) mask[0] * working_region.length else working_region end.ljust(active_width) end
Verify/update the window margins. Returns true if they're fine.
# File lib/mini_readline/read_line/edit/edit_window/sync_window.rb, line 22 def check_margins(length, edit_posn) old_margins = [left_margin, right_margin] if length < base_width set_left_margin(0) elsif edit_posn < left_margin set_left_margin([edit_posn - scroll_step, 0].max) elsif edit_posn > right_margin set_right_margin(edit_posn + scroll_step) end old_margins == [left_margin, right_margin] end
What is the current prompt?
# File lib/mini_readline/read_line/edit/edit_window.rb, line 55 def prompt window_scrolled? ? @options[:scroll_prompt] : @options[:base_prompt] end
What is the offset of the window's right margin?
# File lib/mini_readline/read_line/edit/edit_window.rb, line 26 def right_margin left_margin + active_width - 1 end
What is the scroll step?
# File lib/mini_readline/read_line/edit/edit_window.rb, line 60 def scroll_step @options[:scroll_step] end
Keep the cursor in sync!
# File lib/mini_readline/read_line/edit/edit_window/sync_cursor.rb, line 10 def sync_cursor(edit_posn) MiniTerm.set_posn(column: edit_posn - left_margin + prompt.length) end
Keep the edit window in sync!
# File lib/mini_readline/read_line/edit/edit_window/sync_window.rb, line 10 def sync_window(edit_buffer, edit_posn) unless check_margins(edit_buffer.length, edit_posn) window_buffer.clear @show_prompt = true end image = build_screen_image(edit_buffer) update_screen(image) @window_buffer = image end
Bring the screen into agreement with the image.
# File lib/mini_readline/read_line/edit/edit_window/sync_window.rb, line 48 def update_screen(image) if @show_prompt MiniTerm.print("\r#{prompt.text}\r") @show_prompt = false end (0...active_width).each do |index| if (image_char = image[index]) != window_buffer[index] MiniTerm.set_posn(column: prompt.length + index) MiniTerm.print(image_char) end end end
Is the window currently in the scrolled state?
# File lib/mini_readline/read_line/edit/edit_window.rb, line 31 def window_scrolled? left_margin > 0 end
What is the full window width?
# File lib/mini_readline/read_line/edit/edit_window.rb, line 45 def window_width @options[:window_width] end
Private Instance Methods
Set the left margin
# File lib/mini_readline/read_line/edit/edit_window.rb, line 67 def set_left_margin(value) @left_margin = value end
Set the right margin If the right_margin
is being set, then we must be scrolling. That is why the scroll_width
is used instead of active_width
here.
# File lib/mini_readline/read_line/edit/edit_window.rb, line 74 def set_right_margin(value) @left_margin = value - scroll_width + 1 end