Class: MiniReadline::Readline
- Inherits:
-
Object
- Object
- MiniReadline::Readline
- 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_options ⇒ Object
readonly
The options specifically associated with this instance.
Instance Method Summary collapse
-
#history ⇒ Object
Get the history buffer of this read line instance.
-
#initialize(instance_options = {}) ⇒ Readline
constructor
Setup the instance of the mini line editor.
-
#initialize_parms(options) ⇒ Object
Initialize the read line process.
-
#readline(options = {}) ⇒ Object
Read a line from the console with edit and history.
-
#restore_warnings ⇒ Object
Restore warnings to their typical ugliness.
-
#set_options(options) ⇒ Object
Set up the options.
-
#set_prompt(prompt) ⇒ Object
Set up the prompt.
-
#suppress_warnings ⇒ Object
No warnings please!.
-
#verify_mask(secret) ⇒ Object
Verify the secret mask.
-
#verify_prompt(prompt) ⇒ Object
Verify that the prompt will fit!.
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(={}) = log = ([:log] || BASE_OPTIONS[:log] || []).clone @history = History.new(log) @no_history = NoHistory.new end |
Instance Attribute Details
#instance_options ⇒ Object (readonly)
The options specifically associated with this instance.
18 19 20 |
# File 'lib/mini_readline/read_line.rb', line 18 def end |
Instance Method Details
#history ⇒ Object
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() () history = [:history] ? @history : @no_history @edit = Edit.new(history, ) @history.initialize_parms() 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( = {}) suppress_warnings initialize_parms() MiniTerm.raw { @edit.edit_process } ensure restore_warnings puts end |
#restore_warnings ⇒ Object
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 () = MiniReadline::BASE_OPTIONS .merge() .merge() [:window_width] = MiniTerm.width - 1 set_prompt([:prompt]) verify_mask([: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) [:base_prompt] = Prompt.new(prompt) [:scroll_prompt] = Prompt.new([:alt_prompt] || prompt) verify_prompt([:base_prompt]) verify_prompt([:scroll_prompt]) end |
#suppress_warnings ⇒ Object
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 ([:window_width] - prompt.length) > ([:scroll_step] * 2) fail MiniReadlinePLE, "Too long: #{prompt.inspect}" end end |