Class: VER::Status

Inherits:
Entry
  • Object
show all
Defined in:
lib/ver/status.rb

Overview

The status bar

Constant Summary collapse

HISTORY =
Hash.new{|k,v| k[v] = [] }

Constants inherited from Entry

Entry::BACKWARD_WORD, Entry::FORWARD_WORD, Entry::STYLE_NAME_POOL, Entry::STYLE_NAME_REGISTER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Entry

#accept_line, #backward_char, #backward_delete_char, #backward_word, #beginning_of_history, #beginning_of_line, #delete_char, #delete_motion, #end_of_history, #end_of_line, #error, #forward_backward_delete_char, #forward_char, #forward_word, #insert, #insert_string, #message, #next_history, #noop, obtain_style_name, #previous_history, #quit, return_style_name, #style, #transpose_chars

Constructor Details

#initialize(view, options = {}) ⇒ Status

Returns a new instance of Status.



9
10
11
12
13
14
15
16
17
# File 'lib/ver/status.rb', line 9

def initialize(view, options = {})
  options[:style] ||= self.class.obtain_style_name
  super
  self.view = view
  @question = ''

  keymap_name = VER.options.keymap
  self.keymap = Keymap.get(name: keymap_name, receiver: self)
end

Instance Attribute Details

#keymapObject

Returns the value of attribute keymap.



4
5
6
# File 'lib/ver/status.rb', line 4

def keymap
  @keymap
end

#modeObject

Returns the value of attribute mode.



5
6
7
# File 'lib/ver/status.rb', line 5

def mode
  @mode
end

#viewObject

Returns the value of attribute view.



4
5
6
# File 'lib/ver/status.rb', line 4

def view
  @view
end

Instance Method Details

#ask(question, options = {}, &callback) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/ver/status.rb', line 34

def ask(question, options = {}, &callback)
  @backup_value, @callback = value, callback
  @history_idx = -1

  self.question = question
  submit_when_taken(options[:take])
  focus
end

#ask_abortObject



72
73
74
75
76
# File 'lib/ver/status.rb', line 72

def ask_abort
  self.question = ''
  self.value = @backup_value
  text.focus
end

#ask_submitObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ver/status.rb', line 55

def ask_submit
  answer = value
  history = HISTORY[@question]
  history.uniq!
  history << answer
  self.question = ''

  case result = @callback.call(answer)
  when String
    message result
  when Symbol
    result
  else
    message result.inspect
  end
end

#cursor=(pos) ⇒ Object



102
103
104
105
# File 'lib/ver/status.rb', line 102

def cursor=(pos)
  return if pos < @question.size
  super
end

#delete(from, to = Tk::None) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/ver/status.rb', line 78

def delete(from, to = Tk::None)
  if from < @question.size
    from = @question.size
  end

  super(from, to)
end

#destroyObject



19
20
21
22
23
24
# File 'lib/ver/status.rb', line 19

def destroy
  style_name = style
  super
ensure
  self.class.return_style_name(style_name)
end

#history_completeObject



135
136
137
138
139
140
141
142
143
# File 'lib/ver/status.rb', line 135

def history_complete
  history = HISTORY[@question]
  so_far = value.sub(@question, '')
  needle = Regexp.escape(so_far)
  list = history.grep(/#{needle}/i)
  return if list.empty?

  self.value = answer
end

#history_nextObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ver/status.rb', line 121

def history_next
  @history_idx += 1
  history = HISTORY[@question]

  if @history_idx > history.size
    @history_idx = 0
  end

  p next: [history, @history_idx]
  answer = history[@history_idx]
  return unless answer
  self.value = answer
end

#history_prevObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ver/status.rb', line 107

def history_prev
  @history_idx -= 1
  history = HISTORY[@question]

  if @history_idx < 0
    @history_idx = history.size - 1
  end

  p prev: [history, @history_idx]
  answer = history[@history_idx]
  return unless answer
  self.value = answer
end

#question=(string) ⇒ Object



95
96
97
98
99
100
# File 'lib/ver/status.rb', line 95

def question=(string)
  execute_only(:delete, 0, :end)
  execute_only(:insert, 0, string)
  @question = string.to_s
  Tk::Event.generate(self, '<<Modified>>')
end

#submit_when_taken(length) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ver/status.rb', line 43

def submit_when_taken(length)
  return unless length
  target = value.size + length

  bind '<<Modified>>' do |event|
    if value.size >= target
      bind('<<Modified>>'){}
      ask_submit
    end
  end
end

#textObject



30
31
32
# File 'lib/ver/status.rb', line 30

def text
  view.text
end

#valueObject



90
91
92
93
# File 'lib/ver/status.rb', line 90

def value
  regex = Regexp.escape(@question)
  get.sub(/^#{regex}/, '')
end

#value=(string) ⇒ Object



86
87
88
# File 'lib/ver/status.rb', line 86

def value=(string)
  super([@question, string].join)
end