Class: Rereline::LineEditor
- Inherits:
-
Object
- Object
- Rereline::LineEditor
- Defined in:
- lib/rereline/line_editor.rb
Instance Attribute Summary collapse
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#input_pos ⇒ Object
Returns the value of attribute input_pos.
-
#input_text ⇒ Object
Returns the value of attribute input_text.
-
#prompt ⇒ Object
Returns the value of attribute prompt.
Instance Method Summary collapse
- #delete_prev_input_pos ⇒ Object
-
#initialize(&block) ⇒ LineEditor
constructor
A new instance of LineEditor.
- #input_char(c) ⇒ Object
- #input_key(key) ⇒ Object
- #line ⇒ Object
- #move_input_pos(offset) ⇒ Object
- #move_left ⇒ Object
- #move_right ⇒ Object
- #prev_input_pos_line ⇒ Object
Constructor Details
#initialize(&block) ⇒ LineEditor
Returns a new instance of LineEditor.
8 9 10 11 12 13 14 15 |
# File 'lib/rereline/line_editor.rb', line 8 def initialize(&block) @input_text = "" @input_pos = 0 @encoding = Encoding.default_external block.call(self) if block @input_text = @input_text.dup @input_key_buffer = [] end |
Instance Attribute Details
#encoding ⇒ Object
Returns the value of attribute encoding.
5 6 7 |
# File 'lib/rereline/line_editor.rb', line 5 def encoding @encoding end |
#input_pos ⇒ Object
Returns the value of attribute input_pos.
6 7 8 |
# File 'lib/rereline/line_editor.rb', line 6 def input_pos @input_pos end |
#input_text ⇒ Object
Returns the value of attribute input_text.
5 6 7 |
# File 'lib/rereline/line_editor.rb', line 5 def input_text @input_text end |
#prompt ⇒ Object
Returns the value of attribute prompt.
5 6 7 |
# File 'lib/rereline/line_editor.rb', line 5 def prompt @prompt end |
Instance Method Details
#delete_prev_input_pos ⇒ Object
32 33 34 35 36 |
# File 'lib/rereline/line_editor.rb', line 32 def delete_prev_input_pos return if input_pos <= 0 input_text.slice!(input_pos - 1, 1) move_left end |
#input_char(c) ⇒ Object
27 28 29 30 |
# File 'lib/rereline/line_editor.rb', line 27 def input_char(c) input_text.insert(input_pos, c) move_right end |
#input_key(key) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/rereline/line_editor.rb', line 17 def input_key(key) return if key.nil? @input_key_buffer << key if (result = @input_key_buffer.map(&:chr).join.force_encoding(encoding)).valid_encoding? input_char(result) @input_key_buffer.clear end end |
#line ⇒ Object
58 59 60 |
# File 'lib/rereline/line_editor.rb', line 58 def line "#{prompt}#{input_text}" end |
#move_input_pos(offset) ⇒ Object
38 39 40 |
# File 'lib/rereline/line_editor.rb', line 38 def move_input_pos(offset) self.input_pos = input_pos + offset end |
#move_left ⇒ Object
42 43 44 |
# File 'lib/rereline/line_editor.rb', line 42 def move_left move_input_pos(-1) end |
#move_right ⇒ Object
46 47 48 |
# File 'lib/rereline/line_editor.rb', line 46 def move_right move_input_pos(+1) end |
#prev_input_pos_line ⇒ Object
54 55 56 |
# File 'lib/rereline/line_editor.rb', line 54 def prev_input_pos_line "#{prompt}#{input_text.slice(0, input_pos)}" end |