Class: RubyText::Window::GetString

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

Instance Method Summary collapse

Constructor Details

#initialize(win = STDSCR, str = "", i = 0, history: [], limit: nil, tab: [], capture: []) ⇒ GetString

Returns a new instance of GetString.

Raises:

  • (ArgumentError)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/output.rb', line 165

def initialize(win = STDSCR, str = "", i = 0, history: [], limit: nil, tab: [], capture: [])
  @win = win
  @r0, @c0 = @win.rc
  @limit = limit || (@win.cols - @r0 - 1)
  raise ArgumentError unless @limit.is_a?(Numeric)
  @str, @i = str[0..(@limit-1)], i
  @str ||= ""
  @win.print @str
  @win.left @str.length
  @history = history
  @h = @history.length - 1
  @maxlen = 0    # longest string in history list
  @tabcom = tab
end

Instance Method Details

#add(ch) ⇒ Object



251
252
253
254
255
256
257
258
259
260
# File 'lib/output.rb', line 251

def add(ch)
  if @str.length >= @limit
    Curses.beep
    return
  end
  @str.insert(@i, ch)
  @win.right
  @win.go(@r0, @c0) { @win.print @str }
  @i += 1
end

#backspaceObject



200
201
202
203
204
205
206
207
# File 'lib/output.rb', line 200

def backspace
  # remember: may be in middle of string
  return if @i == 0
  @i -= 1
  @str[@i] = ""
  @win.left
  @win.rcprint @r0, @c0, @str + " "
end

#completeObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/output.rb', line 233

def complete
  targets = @tabcom.find_all {|x| x.start_with?(@str) }
  if targets.nil?
    # Curses.beep
    @win.print "???"
    return
  end
  if targets.size > 1
    num, target = @win.menu(items: targets)
  else
    target = targets.first
  end
  @str = target.nil? ? "" : target.dup
  @i = @str.length
  @win.go @r0, @c0
  @win.print @str
end

#enterObject



180
181
182
183
184
# File 'lib/output.rb', line 180

def enter
  @win.crlf
  @history << @str
  @h = @history.length - 1
end

#history_nextObject



221
222
223
224
225
226
227
228
229
230
231
# File 'lib/output.rb', line 221

def history_next
  return if @history.empty?
  @h = (@h + 1) % @history.length
  @win.go @r0, @c0
  @maxlen = @history.map(&:length).max
  @win.print(" "*@maxlen)
  @str = @history[@h]
  @i = @str.length
  @win.go @r0, @c0
  @win.print @str
end

#history_prevObject



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/output.rb', line 209

def history_prev
  return if @history.empty?
  @win.go @r0, @c0
  @maxlen = @history.map(&:length).max
  @win.print(" "*@maxlen)
  @h = (@h - 1) % @history.length
  @str = @history[@h]
  @i = @str.length
  @win.go @r0, @c0
  @win.print @str
end

#left_arrowObject



186
187
188
189
190
191
# File 'lib/output.rb', line 186

def left_arrow
  if @i > 0
    @i -= 1
    @win.left
  end
end

#right_arrowObject



193
194
195
196
197
198
# File 'lib/output.rb', line 193

def right_arrow
  if @i < @str.length
    @i += 1
    @win.right
  end
end

#valueObject



262
263
264
# File 'lib/output.rb', line 262

def value
  @str
end