10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/inquirer/prompts/list.rb', line 10
def prompt opts = {}
@question = opts[:message]
default = opts[:default] || 0
@position = 0
@paginator = Paginator.new
@choices = []
opts[:choices].each { |choice|
if choice[:when].is_a?(Proc)
when_parameter = opts.merge(
choice: choice,
)
ask_choice = choice[:when].call( when_parameter )
next if !ask_choice
elsif [true, false].include? choice[:when]
next if !choice[:when]
end
choice[:value] ||= choice[:name]
@choices.push(choice)
}
return nil if Array(@choices).empty?
return @choices[0][:value] if @choices.size == 1
if default.is_a?(String) || default.is_a?(Symbol)
@position = @choices.find_index { |choice| choice[:value] == default }
@position ||= 0
elsif default.is_a?(Integer) && default < @choices.size
@position = default
else
@position = 0
end
IOHelper.without_cursor do
question_backup = @question
@question += ' '
@question += Inquirer::Style::List.selection_help
render_prompt
@question = question_backup
IOHelper.read_char do |char|
key = IOChar.char_to_key(char)
case key
when 'up'
@position = (@position - 1) % @choices.length
when 'down'
@position = (@position + 1) % @choices.length
end
IOHelper.clear
render_prompt
key != 'return'
end
end
IOHelper.clear
render_result
@choices[@position][:value]
end
|