class Interactive::InputState
Wrap around the input options, the current answer, and the current position.
Passed to handlers, which are expected to mutate answer
and
position
as they handle incoming events.
Attributes
answer[RW]
options[RW]
position[RW]
Public Class Methods
new(options = {}, answer = "", position = 0)
click to toggle source
# File lib/interact/interactive.rb, line 36 def initialize(options = {}, answer = "", position = 0) @options = options @answer = answer @position = position @done = false end
Public Instance Methods
back(x)
click to toggle source
# File lib/interact/interactive.rb, line 66 def back(x) return if x == 0 print("\b" * (x * char_size)) @position -= x end
censor(what)
click to toggle source
# File lib/interact/interactive.rb, line 53 def censor(what) if with = @options[:echo] with * what.size else what end end
clear(x)
click to toggle source
# File lib/interact/interactive.rb, line 74 def clear(x) return if x == 0 print(" " * (x * char_size)) @position += x back(x) end
display(what)
click to toggle source
# File lib/interact/interactive.rb, line 61 def display(what) print(censor(what)) @position += what.size end
done!()
click to toggle source
Call to signal to the input reader that it can stop.
# File lib/interact/interactive.rb, line 44 def done! @done = true end
done?()
click to toggle source
Is the input finished/complete?
# File lib/interact/interactive.rb, line 49 def done? @done end
goto(pos)
click to toggle source
# File lib/interact/interactive.rb, line 84 def goto(pos) return if pos == position if pos > position display(answer[position .. pos]) else print("\b" * (position - pos) * char_size) end @position = pos end
Private Instance Methods
char_size()
click to toggle source
# File lib/interact/interactive.rb, line 98 def char_size @options[:echo] ? @options[:echo].size : 1 end