Class: TTY::Prompt::Keypress
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
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
38
39
40
|
# File 'lib/tty/prompt/keypress.rb', line 38
def any_key?
@keys == UndefinedSetting
end
|
#countdown(value = (not_set = true)) ⇒ Object
31
32
33
34
35
|
# 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
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/tty/prompt/keypress.rb', line 65
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
47
48
49
50
51
52
53
54
55
|
# File 'lib/tty/prompt/keypress.rb', line 47
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
|
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/tty/prompt/keypress.rb', line 76
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
93
94
95
|
# File 'lib/tty/prompt/keypress.rb', line 93
def refresh(lines, lines_to_clear)
@prompt.clear_lines(lines)
end
|
#render_question ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/tty/prompt/keypress.rb', line 57
def render_question
= super
if timeout?
.gsub!(/:countdown/, format("%.#{@decimals}f", countdown))
end
end
|
#timeout? ⇒ Boolean
43
44
45
|
# File 'lib/tty/prompt/keypress.rb', line 43
def timeout?
@timeout != UndefinedSetting
end
|