class RETerm::Components::Rocker

Rocker component, allowing use to select one of several items

Attributes

index[RW]
items[RW]
labels[RW]

Public Class Methods

new(args={}) click to toggle source

Initialize the Rocker component

@param [Hash] args rocker params

@example using the Rocker

win    = Window.new
rocker = Rocker.new
win.component = rocker
rocker.items = ['Foo', 'Bar', 'Baz']
val = rocker.activate!
Calls superclass method RETerm::Component::new
# File lib/reterm/components/rocker.rb, line 21
def initialize(args={})
  super
  @items = []
  @index  = nil
  @labels = true
  @index  = 0
end

Public Instance Methods

activatable?() click to toggle source
# File lib/reterm/components/rocker.rb, line 41
def activatable?
  true
end
activate!(*input) click to toggle source
# File lib/reterm/components/rocker.rb, line 45
def activate!(*input)
  refresh_win
  handle_input(*input)
  @items[@index]
end
draw!() click to toggle source
# File lib/reterm/components/rocker.rb, line 37
def draw!
  refresh_win
end
requested_cols() click to toggle source
# File lib/reterm/components/rocker.rb, line 33
def requested_cols
  max_item_size + 3
end
requested_rows() click to toggle source
# File lib/reterm/components/rocker.rb, line 29
def requested_rows
  @items.size + 3
end

Private Instance Methods

on_dec() click to toggle source
# File lib/reterm/components/rocker.rb, line 59
def on_dec
  @index -= 1
  @index  = 0 if @index < 0
  refresh_win
end
on_inc() click to toggle source
# File lib/reterm/components/rocker.rb, line 53
def on_inc
  @index += 1
  @index  = @items.size - 1 if @index == @items.size
  refresh_win
end
refresh_win() click to toggle source
# File lib/reterm/components/rocker.rb, line 65
def refresh_win
  pre  = post = ' '
  pre  = '-' if @labels
  post = '+' if @labels

  window.mvaddstr(1, 1, " |#{pre}|")
  @items.each_with_index { |v, i|
    sel = @index == i ? "#" : " "
    window.mvaddstr(i+2, 1, " |" + sel + "|" + @items[i])
  }
  window.mvaddstr(@items.size+2, 1, " |#{post}|")
  window.refresh
  update_reterm
end