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
|
# File 'lib/diskman/chooser.rb', line 18
def select
if @items.length == 0
$stderr.puts 'No %s found'.yellow % @plural
raise Interrupt
end
if @items.length == 1
$stderr.puts 'Found the following %s.' % label
else
$stderr.puts 'Choose from the following %s.' % @plural
end
$stderr.puts
@items.each_with_index do |device, i|
$stderr.puts '%5d. %s' % [i + 1, device]
end
$stderr.puts
if @items.length == 1
$stderr.puts 'Press enter to select it.'
$stdin.gets
return @items.first
end
$stderr.puts 'Enter your selection.'
$stderr.puts
$stderr.print '> '
selection = $stdin.gets.chomp
$stderr.puts
if selection.length == 0
raise Interrupt
end
selection = selection.to_i
if selection <= 0 || selection > @items.length
$stderr.puts 'Invalid selection'.red
raise Interrupt
end
@items[selection - 1]
end
|