Class: MiniReadline::Readline

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_readline/read_line.rb

Overview

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!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_options = {}) ⇒ Readline

Setup the instance of the mini line editor.



21
22
23
24
25
26
# 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

Instance Attribute Details

#instance_optionsObject (readonly)

The options specifically associated with this instance.



18
19
20
# File 'lib/mini_readline/read_line.rb', line 18

def instance_options
  @instance_options
end

Instance Method Details

#historyObject

Get the history buffer of this read line instance.



29
30
31
# File 'lib/mini_readline/read_line.rb', line 29

def history
  @history.history
end

#initialize_parms(options) ⇒ Object

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



45
46
47
48
49
50
51
52
# 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 = {}) ⇒ Object

Read a line from the console with edit and history.



34
35
36
37
38
39
40
41
# 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_warningsObject

Restore warnings to their typical ugliness.



96
97
98
99
# File 'lib/mini_readline/read_line.rb', line 96

def restore_warnings
  $stderr.close
  $stderr = @old_stderr
end

#set_options(options) ⇒ Object

Set up the options



55
56
57
58
59
60
61
62
63
# 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) ⇒ Object

Set up the prompt.



66
67
68
69
70
71
72
# 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_warningsObject

No warnings please!



90
91
92
93
# 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) ⇒ Object

Verify the secret mask



83
84
85
86
87
# 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) ⇒ Object

Verify that the prompt will fit!



75
76
77
78
79
80
# 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