Class: CommandT::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/command-t/prompt.rb

Overview

Abuse the status line as a prompt.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Prompt

Returns a new instance of Prompt.



9
10
11
12
13
14
# File 'lib/command-t/prompt.rb', line 9

def initialize(options = {})
  @abbrev       = ''  # abbreviation entered so far
  @col          = 0   # cursor position
  @cursor_color = options[:cursor_color] || 'Underlined'
  @has_focus    = false
end

Instance Attribute Details

#abbrevObject

Returns the value of attribute abbrev.



7
8
9
# File 'lib/command-t/prompt.rb', line 7

def abbrev
  @abbrev
end

Instance Method Details

#add!(char) ⇒ Object

Insert a character at (before) the current cursor position.



48
49
50
51
52
53
# File 'lib/command-t/prompt.rb', line 48

def add!(char)
  left, cursor, right = abbrev_segments
  @abbrev = left + char + cursor + right
  @col += 1
  redraw
end

#backspace!Object

Delete a character to the left of the current cursor position.



56
57
58
59
60
61
62
63
# File 'lib/command-t/prompt.rb', line 56

def backspace!
  if @col > 0
    left, cursor, right = abbrev_segments
    @abbrev = left.chop! + cursor + right
    @col -= 1
    redraw
  end
end

#clear!Object

Clear any entered text.



24
25
26
27
28
# File 'lib/command-t/prompt.rb', line 24

def clear!
  @abbrev = ''
  @col    = 0
  redraw
end

#clear_prev_word!Object

Remove word before cursor



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/command-t/prompt.rb', line 31

def clear_prev_word!
  suffix_length = @abbrev.length - @col
  if @abbrev.match(
    %r{
      (.*)                  # prefix
      \b\w.*                # "word" to clear
      (.{#{suffix_length}}) # suffix
      \z
    }x
  )
    @abbrev = $~[1] + $~[2]
    @col = @abbrev.length - suffix_length
    redraw
  end
end

#cursor_endObject



88
89
90
91
92
93
# File 'lib/command-t/prompt.rb', line 88

def cursor_end
  if @col < @abbrev.length
    @col = @abbrev.length
    redraw
  end
end

#cursor_leftObject



74
75
76
77
78
79
# File 'lib/command-t/prompt.rb', line 74

def cursor_left
  if @col > 0
    @col -= 1
    redraw
  end
end

#cursor_rightObject



81
82
83
84
85
86
# File 'lib/command-t/prompt.rb', line 81

def cursor_right
  if @col < @abbrev.length
    @col += 1
    redraw
  end
end

#cursor_startObject



95
96
97
98
99
100
# File 'lib/command-t/prompt.rb', line 95

def cursor_start
  if @col != 0
    @col = 0
    redraw
  end
end

#delete!Object

Delete a character at the current cursor position.



66
67
68
69
70
71
72
# File 'lib/command-t/prompt.rb', line 66

def delete!
  if @col < @abbrev.length
    left, cursor, right = abbrev_segments
    @abbrev = left + right
    redraw
  end
end

#disposeObject

Erase whatever is displayed in the prompt line, effectively disposing of the prompt



18
19
20
21
# File 'lib/command-t/prompt.rb', line 18

def dispose
  ::VIM::command 'echo'
  ::VIM::command 'redraw'
end

#focusObject



102
103
104
105
106
107
# File 'lib/command-t/prompt.rb', line 102

def focus
  unless @has_focus
    @has_focus = true
    redraw
  end
end

#redrawObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/command-t/prompt.rb', line 116

def redraw
  if @has_focus
    prompt_highlight = 'Comment'
    normal_highlight = 'None'
    cursor_highlight = @cursor_color
  else
    prompt_highlight = 'NonText'
    normal_highlight = 'NonText'
    cursor_highlight = 'NonText'
  end
  left, cursor, right = abbrev_segments
  components = [prompt_highlight, '>>', 'None', ' ']
  components += [normal_highlight, left] unless left.empty?
  components += [cursor_highlight, cursor] unless cursor.empty?
  components += [normal_highlight, right] unless right.empty?
  components += [cursor_highlight, ' '] if cursor.empty?
  set_status *components
end

#unfocusObject



109
110
111
112
113
114
# File 'lib/command-t/prompt.rb', line 109

def unfocus
  if @has_focus
    @has_focus = false
    redraw
  end
end