Class: RubyText::Window::GetString

Inherits:
Object
  • Object
show all
Defined in:
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)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'output.rb', line 150

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



236
237
238
239
240
241
242
243
244
245
# File 'output.rb', line 236

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



185
186
187
188
189
190
191
192
# File 'output.rb', line 185

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



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'output.rb', line 218

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



165
166
167
168
169
# File 'output.rb', line 165

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

#history_nextObject



206
207
208
209
210
211
212
213
214
215
216
# File 'output.rb', line 206

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



194
195
196
197
198
199
200
201
202
203
204
# File 'output.rb', line 194

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



171
172
173
174
175
176
# File 'output.rb', line 171

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

#right_arrowObject



178
179
180
181
182
183
# File 'output.rb', line 178

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

#valueObject



247
248
249
# File 'output.rb', line 247

def value
  @str
end