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, #quiet, #raw, #read_input, #render, #render_error, #required, #to_s, #validate, #validation?, #value, #value?

Constructor Details

#initialize(prompt, **options) ⇒ Keypress

Create keypress question

Parameters:

  • prompt (Prompt)
  • options (Hash)

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

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
  }
  @decimals = (@interval.to_s.split(".")[1] || []).size
  @countdown = @timeout
  time = timeout? ? Float(@timeout) : nil
  @timer = Timer.new(time, Float(@interval))

  @prompt.subscribe(self)
end

Instance Method Details

#any_key?Boolean

Check if any specific keys are set

Returns:

  • (Boolean)

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

def any_key?
  @keys == UndefinedSetting
end

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


31
32
33
34
# File 'lib/tty/prompt/keypress.rb', line 31

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

#interval_handler(time) ⇒ Object


64
65
66
67
68
69
70
71
72
73
# File 'lib/tty/prompt/keypress.rb', line 64

def interval_handler(time)
  return if @done

  question = render_question
  line_size = question.size
  total_lines = @prompt.count_screen_lines(line_size)
  @prompt.print(refresh(question.lines.count, total_lines))
  countdown(time)
  @prompt.print(render_question)
end

#keypress(event) ⇒ Object


46
47
48
49
50
51
52
53
54
# File 'lib/tty/prompt/keypress.rb', line 46

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

#process_input(question) ⇒ Object


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tty/prompt/keypress.rb', line 75

def process_input(question)
  @prompt.print(render_question)

  @timer.on_tick do |time|
    interval_handler(time)
  end

  @timer.while_remaining do |remaining|
    break if @done
    @input = @prompt.read_keypress(nonblock: true)
  end
  countdown(0) unless @done

  @evaluator.(@input)
end

#refresh(lines, lines_to_clear) ⇒ Object


91
92
93
# File 'lib/tty/prompt/keypress.rb', line 91

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

#render_questionObject


56
57
58
59
60
61
62
# File 'lib/tty/prompt/keypress.rb', line 56

def render_question
  header = super
  if timeout?
    header.gsub!(/:countdown/, format("%.#{@decimals}f", countdown))
  end
  header
end

#timeout?Boolean

Check if timeout is set

Returns:

  • (Boolean)

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

def timeout?
  @timeout != UndefinedSetting
end