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: []) ⇒ GetString

Returns a new instance of GetString.



142
143
144
145
146
147
148
149
# File 'lib/output.rb', line 142

def initialize(win = STDSCR, str = "", i = 0, history: [])
  @win = win
  @r0, @c0 = @win.rc
  @str, @i = str, i
  @history = history
  @h = @history.length - 1
  @maxlen = 0
end

Instance Method Details

#add(ch) ⇒ Object



207
208
209
210
211
212
# File 'lib/output.rb', line 207

def add(ch)
  @str.insert(@i, ch)
  @win.right
  @win.go(@r0, @c0) { @win.print @str }
  @i += 1
end

#backspaceObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/output.rb', line 171

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

#enterObject



151
152
153
154
155
# File 'lib/output.rb', line 151

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

#history_nextObject



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

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



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/output.rb', line 183

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



157
158
159
160
161
162
# File 'lib/output.rb', line 157

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

#right_arrowObject



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

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

#valueObject



214
215
216
# File 'lib/output.rb', line 214

def value
  @str
end