Class: RawLine::LineEditor

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

Instance Method Summary collapse

Constructor Details

#initialize(line, sync_with: -> {}) ⇒ LineEditor

Returns a new instance of LineEditor.



3
4
5
6
# File 'lib/rawline/line_editor.rb', line 3

def initialize(line, sync_with: -> {})
  @line = line
  @sync_with_proc = sync_with
end

Instance Method Details

#clear_lineObject

Clear the current line, i.e. @line.text and @line.position. This action is bound to ctrl+k by default.



13
14
15
16
17
18
# File 'lib/rawline/line_editor.rb', line 13

def clear_line
  @line.text = ""
  @line.position = 0
  sync!
  true
end

#delete_character(no_line_history = false) ⇒ Object

Delete the character under the cursor. If no_line_hisytory is set to true, the deletion won’t be recorded in the line history. This action is bound to the delete key by default.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rawline/line_editor.rb', line 50

def delete_character(no_line_history=false)
  unless @line.position > @line.eol
    # save characters to shift
    chars = (@line.eol?) ? ' ' : select_characters_from_cursor(1)
    #remove character from line
    @line[@line.position] = ''
    sync!
    return true
  end
  false
end

#delete_left_characterObject

Delete the character at the left of the cursor. If no_line_hisytory is set to true, the deletion won’t be recorded in the line history. This action is bound to the backspace key by default.



26
27
28
29
30
31
32
33
# File 'lib/rawline/line_editor.rb', line 26

def delete_left_character
  if move_left then
    delete_character
    sync!
    return true
  end
  false
end

#delete_n_characters(number_of_characters_to_delete) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/rawline/line_editor.rb', line 35

def delete_n_characters(number_of_characters_to_delete)
  number_of_characters_to_delete.times do |n|
    @line[@line.position] = ''
    @line.left
  end
  sync!
  true
end

#highlight_text_up_to(text, position) ⇒ Object



62
63
64
# File 'lib/rawline/line_editor.rb', line 62

def highlight_text_up_to(text, position)
  ANSIString.new("\e[1m#{text[0...position]}\e[0m#{text[position..-1]}")
end

#insert(string) ⇒ Object

Inserts a string at the current line position, shifting characters to right if necessary.



70
71
72
73
74
75
76
# File 'lib/rawline/line_editor.rb', line 70

def insert(string)
  return false if string.empty?
  @line.text.insert @line.position, string
  string.length.times { @line.right }
  sync!
  true
end

#kill_forwardObject



78
79
80
81
82
83
# File 'lib/rawline/line_editor.rb', line 78

def kill_forward
  @line.text[@line.position..-1].tap do
    @line.text[@line.position..-1] = ANSIString.new("")
    sync!
  end
end

#move_leftObject

Move the cursor left (if possible) by printing a backspace, updating @line.position accordingly. This action is bound to the left arrow key by default.



112
113
114
115
116
117
118
119
# File 'lib/rawline/line_editor.rb', line 112

def move_left
  unless @line.bol? then
    @line.left
    sync!
    return true
  end
  false
end

#move_rightObject



121
122
123
124
125
126
127
128
# File 'lib/rawline/line_editor.rb', line 121

def move_right
  unless @line.position > @line.eol then
    @line.right
    sync!
    return true
  end
  false
end

#move_to_beginning_of_inputObject



130
131
132
133
134
# File 'lib/rawline/line_editor.rb', line 130

def move_to_beginning_of_input
  @line.position = @line.bol
  sync!
  true
end

#move_to_end_of_inputObject



136
137
138
139
140
# File 'lib/rawline/line_editor.rb', line 136

def move_to_end_of_input
  @line.position = @line.length
  sync!
  true
end

#overwrite_line(new_line, position: nil, highlight_up_to: nil) ⇒ Object

Overwrite the current line (@line.text) with new_line, and optionally reset the cursor position to position.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rawline/line_editor.rb', line 147

def overwrite_line(new_line, position: nil, highlight_up_to: nil)
  position = @line.position if position == :preserve
  text = @line.text
  @highlighting = false

  if highlight_up_to
    @highlighting = true
    new_line = highlight_text_up_to(new_line, highlight_up_to)
  end

  @line.position = position || new_line.length
  @line.text = new_line
  sync!
  true
end

#position=(position) ⇒ Object



163
164
165
166
# File 'lib/rawline/line_editor.rb', line 163

def position=(position)
  @line.position = position
  sync!
end

#textObject



168
169
170
# File 'lib/rawline/line_editor.rb', line 168

def text
  @line.text
end

#write(string) ⇒ Object

Write a string starting from the cursor position ovewriting any character at the current position if necessary.



89
90
91
92
93
94
95
96
97
98
# File 'lib/rawline/line_editor.rb', line 89

def write(string)
  if @line.eol?
    @line.text[@line.position] = string
  else
    @line.text << string
  end
  string.length.times { @line.right }
  sync!
  true
end

#yank_forward(text) ⇒ Object



100
101
102
103
104
105
# File 'lib/rawline/line_editor.rb', line 100

def yank_forward(text)
  @line.text[@line.position...@line.position] = text
  @line.position = @line.position + text.length
  sync!
  @line.text
end