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

edit_buffer[R]

The main edit buffer

edit_posn[R]

The current edit position

Public Class Methods

new(history, options) click to toggle source

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

auto_complete(_keyboard_args) click to toggle source

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
auto_manager() click to toggle source

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
auto_trim() click to toggle source

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
cancel(_keyboard_args) click to toggle source

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
delete_all_left(_keyboard_args) click to toggle source

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
delete_all_right(_keyboard_args) click to toggle source

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
delete_left(_keyboard_args) click to toggle source

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
delete_right(_keyboard_args) click to toggle source

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
edit_loop() click to toggle source

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
edit_process() click to toggle source

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
end_of_input(_keyboard_args) click to toggle source

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
enter(_keyboard_args) click to toggle source

The insert_text command.

# File lib/mini_readline/read_line/edit/enter.rb, line 10
def enter(_keyboard_args)
  @working = false
end
go_end(_keyboard_args) click to toggle source

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
go_home(_keyboard_args) click to toggle source

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
go_left(_keyboard_args) click to toggle source

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
go_right(_keyboard_args) click to toggle source

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
insert_text(keyboard_args) click to toggle source

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
length() click to toggle source

How long is the current string?

# File lib/mini_readline/read_line/edit.rb, line 56
def length
  edit_buffer.length
end
next_history(_keyboard_args) click to toggle source

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
previous_history(_keyboard_args) click to toggle source

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_keystroke(key_cmd) click to toggle source

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
unmapped(_keyboard_args) click to toggle source

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
word_left(_keyboard_args) click to toggle source

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
word_right(_keyboard_args) click to toggle source

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