]> git.cworth.org Git - sup/blob - lib/sup/horizontal-selector.rb
Add new :crypto_default configuration option.
[sup] / lib / sup / horizontal-selector.rb
1 module Redwood
2
3 class HorizontalSelector
4   attr_accessor :label
5
6   def initialize label, vals, labels, base_color=:horizontal_selector_unselected_color, selected_color=:horizontal_selector_selected_color
7     @label = label
8     @vals = vals
9     @labels = labels
10     @base_color = base_color
11     @selected_color = selected_color
12     @selection = 0
13   end
14
15   def set_to val
16     if @vals.index(val)
17       @selection = @vals.index(val)
18     else
19       error "Invalid option ", val.inspect, " (valid options: ", @vals.inspect, ")"
20     end
21   end
22
23   def val; @vals[@selection] end
24
25   def line width=nil
26     label =
27       if width
28         sprintf "%#{width}s ", @label
29       else
30         "#{@label} "
31       end
32
33     [[@base_color, label]] + 
34       (0 ... @labels.length).inject([]) do |array, i|
35         array + [
36           if i == @selection
37             [@selected_color, @labels[i]]
38           else
39             [@base_color, @labels[i]]
40           end] + [[@base_color, "  "]]
41       end + [[@base_color, ""]]
42   end
43
44   def roll_left
45     @selection = (@selection - 1) % @labels.length
46   end
47
48   def roll_right
49     @selection = (@selection + 1) % @labels.length
50   end
51 end
52
53 end