Class: Interactive::InputState
- Inherits:
-
Object
- Object
- Interactive::InputState
- Defined in:
- lib/interact/interactive.rb
Overview
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.
Instance Attribute Summary collapse
-
#answer ⇒ Object
Returns the value of attribute answer.
-
#options ⇒ Object
Returns the value of attribute options.
-
#position ⇒ Object
Returns the value of attribute position.
Instance Method Summary collapse
- #back(x) ⇒ Object
- #censor(what) ⇒ Object
- #clear(x) ⇒ Object
- #display(what) ⇒ Object
-
#done! ⇒ Object
Call to signal to the input reader that it can stop.
-
#done? ⇒ Boolean
Is the input finished/complete?.
- #goto(pos) ⇒ Object
-
#initialize(options = {}, answer = "", position = 0) ⇒ InputState
constructor
A new instance of InputState.
Constructor Details
#initialize(options = {}, answer = "", position = 0) ⇒ InputState
Returns a new instance of InputState.
36 37 38 39 40 41 |
# File 'lib/interact/interactive.rb', line 36 def initialize( = {}, answer = "", position = 0) @options = @answer = answer @position = position @done = false end |
Instance Attribute Details
#answer ⇒ Object
Returns the value of attribute answer.
34 35 36 |
# File 'lib/interact/interactive.rb', line 34 def answer @answer end |
#options ⇒ Object
Returns the value of attribute options.
34 35 36 |
# File 'lib/interact/interactive.rb', line 34 def @options end |
#position ⇒ Object
Returns the value of attribute position.
34 35 36 |
# File 'lib/interact/interactive.rb', line 34 def position @position end |
Instance Method Details
#back(x) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/interact/interactive.rb', line 66 def back(x) return if x == 0 print("\b" * (x * char_size)) @position -= x end |
#censor(what) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/interact/interactive.rb', line 53 def censor(what) if with = @options[:echo] with * what.size else what end end |
#clear(x) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# 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) ⇒ Object
61 62 63 64 |
# File 'lib/interact/interactive.rb', line 61 def display(what) print(censor(what)) @position += what.size end |
#done! ⇒ Object
Call to signal to the input reader that it can stop.
44 45 46 |
# File 'lib/interact/interactive.rb', line 44 def done! @done = true end |
#done? ⇒ Boolean
Is the input finished/complete?
49 50 51 |
# File 'lib/interact/interactive.rb', line 49 def done? @done end |
#goto(pos) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# 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 |