class MiniReadline::Readline

The Readline class that does the actual work of getting lines from the user. Note that each instance of this class maintains its own copy of the optional command history. :reek: TooManyInstanceVariables – Yes and it needs them!

Attributes

instance_options[R]

The options specifically associated with this instance.

Public Class Methods

new(instance_options={}) click to toggle source

Setup the instance of the mini line editor.

# File lib/mini_readline/read_line.rb, line 21
def initialize(instance_options={})
  @instance_options = instance_options
  log = (@instance_options[:log] || BASE_OPTIONS[:log] || []).clone
  @history    = History.new(log)
  @no_history = NoHistory.new
end

Public Instance Methods

history() click to toggle source

Get the history buffer of this read line instance.

# File lib/mini_readline/read_line.rb, line 29
def history
  @history.history
end
initialize_parms(options) click to toggle source

Initialize the read line process. This basically process the arguments of the readline method.

# File lib/mini_readline/read_line.rb, line 45
def initialize_parms(options)
  set_options(options)

  history = @options[:history] ? @history : @no_history
  @edit = Edit.new(history, @options)

  @history.initialize_parms(@options)
end
readline(options = {}) click to toggle source

Read a line from the console with edit and history.

# File lib/mini_readline/read_line.rb, line 34
def readline(options = {})
  suppress_warnings
  initialize_parms(options)
  MiniTerm.raw { @edit.edit_process }
ensure
  restore_warnings
  puts
end
restore_warnings() click to toggle source

Restore warnings to their typical ugliness.

# File lib/mini_readline/read_line.rb, line 96
def restore_warnings
  $stderr.close
  $stderr = @old_stderr
end
set_options(options) click to toggle source

Set up the options

# File lib/mini_readline/read_line.rb, line 55
def set_options(options)
  @options = MiniReadline::BASE_OPTIONS
               .merge(instance_options)
               .merge(options)

  @options[:window_width] = MiniTerm.width - 1
  set_prompt(@options[:prompt])
  verify_mask(@options[:secret_mask])
end
set_prompt(prompt) click to toggle source

Set up the prompt.

# File lib/mini_readline/read_line.rb, line 66
def set_prompt(prompt)
  @options[:base_prompt]   = Prompt.new(prompt)
  @options[:scroll_prompt] = Prompt.new(@options[:alt_prompt] || prompt)

  verify_prompt(@options[:base_prompt])
  verify_prompt(@options[:scroll_prompt])
end
suppress_warnings() click to toggle source

No warnings please!

# File lib/mini_readline/read_line.rb, line 90
def suppress_warnings
  @old_stderr = $stderr
  $stderr = File.open(File::NULL, 'w')
end
verify_mask(secret) click to toggle source

Verify the secret mask

# File lib/mini_readline/read_line.rb, line 83
def verify_mask(secret)
  if secret && secret.length != 1
    fail MiniReadlineSME, "Secret mask must be nil or a single character string."
  end
end
verify_prompt(prompt) click to toggle source

Verify that the prompt will fit!

# File lib/mini_readline/read_line.rb, line 75
def verify_prompt(prompt)
  unless (@options[:window_width] - prompt.length) >
         (@options[:scroll_step] * 2)
    fail MiniReadlinePLE, "Too long: #{prompt.inspect}"
  end
end