Class: Question::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/question/input.rb

Instance Method Summary collapse

Constructor Details

#initialize(question, default: nil) ⇒ Input

Returns a new instance of Input.



3
4
5
6
7
8
# File 'lib/question/input.rb', line 3

def initialize(question, default:nil)
  @question = question
  @finished = false
  @default = default
  @answer = nil
end

Instance Method Details

#askObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/question/input.rb', line 10

def ask
  print TTY::CODE::SAVE
  question = colorized_question
  question += "(#{@default}) ".light_white if @default

  # Use readline so keyboard shortcuts like alt-backspace work
  @answer = Readline.readline(question + TTY::CODE::NOOP, true)
  @answer = @default if @default && @answer.length == 0

  render
  @answer
rescue Interrupt
  exit 1
end

#colorized_questionObject



32
33
34
35
36
# File 'lib/question/input.rb', line 32

def colorized_question
  colorized_question = "? ".cyan
  colorized_question += @question
  colorized_question += ": "
end

#renderObject



25
26
27
28
29
30
# File 'lib/question/input.rb', line 25

def render
  TTY.clear
  print colorized_question
  print @answer.green
  print "\n"
end