class MiniReadline::Edit
The line editor. :reek: TooManyInstanceVariables – Yes and it needs them!
Process :auto_complete
Process :cancel - delete all text.
Process :delete_all_left
Process :delete_right
Process :delete_left
Process :delete_right
Process :end_of_input
Process :enter
Process :go_end
Process :go_home
Process :go_left
Process :go_right
Process :insert_text
Process :next_history
Process :previous_history
Process :unmapped
Process :word_left
Process :word_right
Attributes
The main edit buffer
The current edit position
Public Class Methods
Set up the edit instance.
# File lib/mini_readline/read_line/edit.rb, line 39 def initialize(history, options) @options = options @history = history @edit_buffer = @options[:initial] @edit_posn = @edit_buffer.length @working = true @edit_window = EditWindow.new(@options) end
Public Instance Methods
The auto-complete command.
# File lib/mini_readline/read_line/edit/auto_complete.rb, line 19 def auto_complete(_keyboard_args) if @options[:auto_complete] && (new_buffer = auto_manager.next(auto_trim)) @edit_buffer = new_buffer @edit_posn = length else MiniTerm.beep end end
Get the auto-complete manager
# File lib/mini_readline/read_line/edit/auto_complete.rb, line 34 def auto_manager @_auto_manager ||= AutoManager.new{@options[:auto_source].new(@options)} end
Get the base part of the edit buffer.
# File lib/mini_readline/read_line/edit/auto_complete.rb, line 29 def auto_trim @edit_buffer[0...(@edit_posn)] end
All right! Scrap all of this and start over!
# File lib/mini_readline/read_line/edit/cancel.rb, line 10 def cancel(_keyboard_args) @edit_buffer = "" @edit_posn = 0 end
The delete to the left command
# File lib/mini_readline/read_line/edit/delete_all_left.rb, line 10 def delete_all_left(_keyboard_args) if @edit_posn > 0 @edit_buffer = @edit_buffer[@edit_posn..-1] @edit_posn = 0 else MiniTerm.beep end end
The delete to the right
# File lib/mini_readline/read_line/edit/delete_all_right.rb, line 10 def delete_all_right(_keyboard_args) if @edit_posn < self.length @edit_buffer = @edit_buffer[0...@edit_posn] else MiniTerm.beep end end
The delete to the left command
# File lib/mini_readline/read_line/edit/delete_left.rb, line 10 def delete_left(_keyboard_args) if @edit_posn > 0 @edit_buffer = @edit_buffer[0...(@edit_posn-1)] + @edit_buffer[@edit_posn..-1] @edit_posn -= 1 else MiniTerm.beep end end
The delete to the right
# File lib/mini_readline/read_line/edit/delete_right.rb, line 10 def delete_right(_keyboard_args) if @edit_posn < self.length @edit_buffer = @edit_buffer[0...(@edit_posn)] + @edit_buffer[@edit_posn+1..-1] else MiniTerm.beep end end
The line editor processing loop.
# File lib/mini_readline/read_line/edit.rb, line 68 def edit_loop while @working @edit_window.sync_window(edit_buffer, edit_posn) @edit_window.sync_cursor(edit_posn) process_keystroke(MiniTerm.get_mapped_char) end edit_buffer end
Interact with the user
# File lib/mini_readline/read_line/edit.rb, line 61 def edit_process result = edit_loop @history.append_history(result) result + (@options[:chomp] ? "" : "\n") end
We are DONE!
# File lib/mini_readline/read_line/edit/end_of_input.rb, line 10 def end_of_input(_keyboard_args) if @options[:eoi_detect] raise MiniReadlineEOI, "End of input detected." else MiniTerm.beep end end
The insert_text
command.
# File lib/mini_readline/read_line/edit/enter.rb, line 10 def enter(_keyboard_args) @working = false end
A lot to the right please!
# File lib/mini_readline/read_line/edit/go_end.rb, line 10 def go_end(_keyboard_args) @edit_posn = length end
A lot to the left please!
# File lib/mini_readline/read_line/edit/go_home.rb, line 10 def go_home(_keyboard_args) @edit_posn = 0 end
A little to the left please!
# File lib/mini_readline/read_line/edit/go_left.rb, line 10 def go_left(_keyboard_args) if @edit_posn > 0 @edit_posn -= 1 else MiniTerm.beep end end
A little to the right please!
# File lib/mini_readline/read_line/edit/go_right.rb, line 10 def go_right(_keyboard_args) if @edit_posn < edit_buffer.length @edit_posn += 1 else MiniTerm.beep end end
The insert_text
command
# File lib/mini_readline/read_line/edit/insert_text.rb, line 10 def insert_text(keyboard_args) @edit_buffer = @edit_buffer[0...@edit_posn] + keyboard_args[1] + @edit_buffer[@edit_posn..-1] @edit_posn += 1 end
How long is the current string?
# File lib/mini_readline/read_line/edit.rb, line 56 def length edit_buffer.length end
The insert_text
command. We are DONE!
# File lib/mini_readline/read_line/edit/next_history.rb, line 10 def next_history(_keyboard_args) if (temp = @history.get_next_history) @edit_buffer = temp @edit_posn = temp.length else MiniTerm.beep end end
The insert_text
command. We are DONE!
# File lib/mini_readline/read_line/edit/previous_history.rb, line 10 def previous_history(_keyboard_args) if (temp = @history.get_previous_history) @edit_buffer = temp @edit_posn = temp.length else MiniTerm.beep end end
Process a keystroke.
# File lib/mini_readline/read_line/edit.rb, line 79 def process_keystroke(key_cmd) send(key_cmd[0], key_cmd) end
An unmapped key was pressed. Beep! :reek: UtilityFunction – Does not depend on state.
# File lib/mini_readline/read_line/edit/unmapped.rb, line 11 def unmapped(_keyboard_args) MiniTerm.beep end
A little more to the left please!
# File lib/mini_readline/read_line/edit/word_left.rb, line 10 def word_left(_keyboard_args) if @edit_posn > 0 left = @edit_buffer[0...@edit_posn] @edit_posn = (posn = left.rindex(/\s\S/)) ? posn+1 : 0 else MiniTerm.beep end end
A little more to the right please!
# File lib/mini_readline/read_line/edit/word_right.rb, line 10 def word_right(_keyboard_args) if @edit_posn < length right = @edit_buffer[(@edit_posn+1)..-1] @edit_posn = (posn = right.index(/\s\S/)) ? @edit_posn+posn+2 : length else MiniTerm.beep end end