Sha256: 32069f2f089a88d76d412f9024bd510396826d87ee909ca85bf5136937eb292a

Contents?: true

Size: 1.26 KB

Versions: 23

Compression:

Stored size: 1.26 KB

Contents

module Redwood

class HorizontalSelector
  class UnknownValue < StandardError; end

  attr_accessor :label, :changed_by_user

  def initialize label, vals, labels, base_color=:horizontal_selector_unselected_color, selected_color=:horizontal_selector_selected_color
    @label = label
    @vals = vals
    @labels = labels
    @base_color = base_color
    @selected_color = selected_color
    @selection = 0
    @changed_by_user = false
  end

  def set_to val
    raise UnknownValue, val.inspect unless can_set_to? val
    @selection = @vals.index(val)
  end

  def can_set_to? val
    @vals.include? val
  end

  def val; @vals[@selection] end

  def line width=nil
    label =
      if width
        sprintf "%#{width}s ", @label
      else
        "#{@label} "
      end

    [[@base_color, label]] +
      (0 ... @labels.length).inject([]) do |array, i|
        array + [
          if i == @selection
            [@selected_color, @labels[i]]
          else
            [@base_color, @labels[i]]
          end] + [[@base_color, "  "]]
      end + [[@base_color, ""]]
  end

  def roll_left
    @selection = (@selection - 1) % @labels.length
    @changed_by_user = true
  end

  def roll_right
    @selection = (@selection + 1) % @labels.length
    @changed_by_user = true
  end
end

end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
sup-1.2 lib/sup/horizontal_selector.rb
sup-1.1 lib/sup/horizontal_selector.rb
sup-1.0 lib/sup/horizontal_selector.rb
sup-0.23 lib/sup/horizontal_selector.rb
sup-0.22.1 lib/sup/horizontal_selector.rb
sup-0.22.0 lib/sup/horizontal_selector.rb
sup-0.21.0 lib/sup/horizontal_selector.rb
sup-0.20.0 lib/sup/horizontal_selector.rb
sup-0.19.0 lib/sup/horizontal_selector.rb
sup-0.18.0 lib/sup/horizontal_selector.rb
sup-0.17.0 lib/sup/horizontal_selector.rb
sup-0.16.0 lib/sup/horizontal_selector.rb
sup-0.15.4 lib/sup/horizontal_selector.rb
sup-0.15.3 lib/sup/horizontal_selector.rb
sup-0.15.2 lib/sup/horizontal_selector.rb
sup-0.15.1 lib/sup/horizontal_selector.rb
sup-0.15.0 lib/sup/horizontal_selector.rb
sup-0.14.1.1 lib/sup/horizontal_selector.rb
sup-0.13.2.1 lib/sup/horizontal_selector.rb
sup-0.14.1 lib/sup/horizontal_selector.rb