Class: RuVim::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/ruvim/command_line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandLine

Returns a new instance of CommandLine.



7
8
9
# File 'lib/ruvim/command_line.rb', line 7

def initialize
  reset(prefix: ":")
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



5
6
7
# File 'lib/ruvim/command_line.rb', line 5

def cursor
  @cursor
end

#prefixObject (readonly)

Returns the value of attribute prefix.



5
6
7
# File 'lib/ruvim/command_line.rb', line 5

def prefix
  @prefix
end

#textObject (readonly)

Returns the value of attribute text.



5
6
7
# File 'lib/ruvim/command_line.rb', line 5

def text
  @text
end

Instance Method Details

#backspaceObject



46
47
48
49
50
51
# File 'lib/ruvim/command_line.rb', line 46

def backspace
  return if @cursor.zero?

  @text.slice!(@cursor - 1)
  @cursor -= 1
end

#clearObject



17
18
19
20
# File 'lib/ruvim/command_line.rb', line 17

def clear
  @text.clear
  @cursor = 0
end

#contentObject



61
62
63
# File 'lib/ruvim/command_line.rb', line 61

def content
  "#{@prefix}#{@text}"
end

#insert(str) ⇒ Object



41
42
43
44
# File 'lib/ruvim/command_line.rb', line 41

def insert(str)
  @text.insert(@cursor, str)
  @cursor += str.length
end

#move_leftObject



53
54
55
# File 'lib/ruvim/command_line.rb', line 53

def move_left
  @cursor -= 1 if @cursor.positive?
end

#move_rightObject



57
58
59
# File 'lib/ruvim/command_line.rb', line 57

def move_right
  @cursor += 1 if @cursor < @text.length
end

#replace_span(start_idx, end_idx, replacement, cursor_at: :end) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruvim/command_line.rb', line 27

def replace_span(start_idx, end_idx, replacement, cursor_at: :end)
  s = [[start_idx.to_i, 0].max, @text.length].min
  e = [[end_idx.to_i, s].max, @text.length].min
  rep = replacement.to_s
  @text = @text[0...s].to_s + rep + @text[e..].to_s
  @cursor =
    case cursor_at
    when :start then s
    when Integer then [[cursor_at, 0].max, @text.length].min
    else
      s + rep.length
    end
end

#replace_text(str) ⇒ Object



22
23
24
25
# File 'lib/ruvim/command_line.rb', line 22

def replace_text(str)
  @text = str.to_s.dup
  @cursor = @text.length
end

#reset(prefix: ":") ⇒ Object



11
12
13
14
15
# File 'lib/ruvim/command_line.rb', line 11

def reset(prefix: ":")
  @prefix = prefix
  @text = +""
  @cursor = 0
end