Class: Interactive::InputState

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

Instance Method Summary collapse

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(options = {}, answer = "", position = 0)
  @options = options
  @answer = answer
  @position = position
  @done = false
end

Instance Attribute Details

#answerObject

Returns the value of attribute answer.



34
35
36
# File 'lib/interact/interactive.rb', line 34

def answer
  @answer
end

#optionsObject

Returns the value of attribute options.



34
35
36
# File 'lib/interact/interactive.rb', line 34

def options
  @options
end

#positionObject

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?

Returns:

  • (Boolean)


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