Class: TTY::Prompt::Keypress

Inherits:
Question show all
Defined in:
lib/tty/prompt/keypress.rb

Constant Summary

Constants inherited from Question

Question::UndefinedSetting

Instance Attribute Summary

Attributes inherited from Question

#message, #messages, #modifier, #validation

Instance Method Summary collapse

Methods inherited from Question

#call, #convert, #convert?, #convert_result, #default, #default?, #echo, #in, #in?, #inspect, #message_for, #modify, #raw, #read_input, #render, #render_error, #required, #to_s, #validate, #validation?

Constructor Details

#initialize(prompt, options = {}) ⇒ Keypress

Create keypress question

Parameters:

  • prompt (Prompt)
  • options (Hash) (defaults to: {})


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tty/prompt/keypress.rb', line 16

def initialize(prompt, options = {})
  super
  @echo    = options.fetch(:echo) { false }
  @keys    = options.fetch(:keys) { UndefinedSetting }
  @timeout = options.fetch(:timeout) { UndefinedSetting }
  @interval = options.fetch(:interval) {
    (@timeout != UndefinedSetting && @timeout < 1) ? @timeout : 1
  }
  @pause   = true
  @countdown = @timeout
  @interval_handler = proc { |time|
    question = render_question
    @prompt.print(refresh(question.lines.count))
    countdown(time)
    @prompt.print(render_question)
  }

  @prompt.subscribe(self)
end

Instance Method Details

#any_key?Boolean

Check if any specific keys are set

Returns:

  • (Boolean)


42
43
44
# File 'lib/tty/prompt/keypress.rb', line 42

def any_key?
  @keys == UndefinedSetting
end

#countdown(value = (not_set = true)) ⇒ Object



36
37
38
39
# File 'lib/tty/prompt/keypress.rb', line 36

def countdown(value = (not_set = true))
  return @countdown if not_set
  @countdown = value
end

#keypress(event) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/tty/prompt/keypress.rb', line 51

def keypress(event)
  if any_key?
    @pause = false
  elsif @keys.is_a?(Array) && @keys.include?(event.key.name)
    @pause = false
  else
    @pause = true
  end
end

#process_input(question) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/tty/prompt/keypress.rb', line 67

def process_input(question)
  time do
    while @pause
      @input = @prompt.read_keypress
    end
    @pause
  end
  @evaluator.(@input)
end

#refresh(lines) ⇒ Object



77
78
79
# File 'lib/tty/prompt/keypress.rb', line 77

def refresh(lines)
  @prompt.clear_lines(lines)
end

#render_questionObject



61
62
63
64
65
# File 'lib/tty/prompt/keypress.rb', line 61

def render_question
  header = super
  header.gsub!(/:countdown/, countdown.to_s)
  header
end

#time(&block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tty/prompt/keypress.rb', line 81

def time(&block)
  if timeout?
    time = Float(@timeout)
    interval = Float(@interval)
    scheduler = Timeout.new(interval_handler: @interval_handler)
    scheduler.timeout(time, interval, &block)
  else
    block.()
  end
rescue Timeout::Error
end

#timeout?Boolean

Check if timeout is set

Returns:

  • (Boolean)


47
48
49
# File 'lib/tty/prompt/keypress.rb', line 47

def timeout?
  @timeout != UndefinedSetting
end