Class: Rereline::LineEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/rereline/line_editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#encodingObject

Returns the value of attribute encoding.



5
6
7
# File 'lib/rereline/line_editor.rb', line 5

def encoding
  @encoding
end

#input_posObject

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_textObject

Returns the value of attribute input_text.



5
6
7
# File 'lib/rereline/line_editor.rb', line 5

def input_text
  @input_text
end

#promptObject

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_posObject



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

#lineObject



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_leftObject



42
43
44
# File 'lib/rereline/line_editor.rb', line 42

def move_left
  move_input_pos(-1)
end

#move_rightObject



46
47
48
# File 'lib/rereline/line_editor.rb', line 46

def move_right
  move_input_pos(+1)
end

#prev_input_pos_lineObject



54
55
56
# File 'lib/rereline/line_editor.rb', line 54

def prev_input_pos_line
  "#{prompt}#{input_text.slice(0, input_pos)}"
end