class Sc20XX::UI::Table

responsible for drawing our table of tracks

Constants

SEPARATOR

Attributes

collection[R]
current[R]
header[RW]
keys[RW]

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/sc20XX/ui/table.rb, line 12
def initialize(*args)
  super

  @sizes = []
  @rows = []
  @current = 0
  @top = 0
  @selected = nil

  reset
end

Public Instance Methods

bind_to(collection) click to toggle source
# File lib/sc20XX/ui/table.rb, line 24
def bind_to(collection)
  fail ArgumentError if @collection

  @collection = collection
  @collection.events.on(:append) { render }
  @collection.events.on(:replace) { clear; render }
end
body_height() click to toggle source
# File lib/sc20XX/ui/table.rb, line 36
def body_height
  rect.height - @header.size
end
bottom?() click to toggle source
# File lib/sc20XX/ui/table.rb, line 40
def bottom?
  current + 1 >= length
end
deselect() click to toggle source
# File lib/sc20XX/ui/table.rb, line 65
def deselect
  @selected = nil
  render
end
down() click to toggle source
# File lib/sc20XX/ui/table.rb, line 52
def down
  if (@current + 1) < length
    @current += 1
    @top += 1 if @current > body_height
    render
  end
end
length() click to toggle source
# File lib/sc20XX/ui/table.rb, line 32
def length
  @collection.size
end
select() click to toggle source
# File lib/sc20XX/ui/table.rb, line 60
def select
  @selected = @current
  render
end
up() click to toggle source
# File lib/sc20XX/ui/table.rb, line 44
def up
  if @current > 0
    @current -= 1
    @top -= 1 if @current < @top
    render
  end
end

Protected Instance Methods

color_for(index) click to toggle source
# File lib/sc20XX/ui/table.rb, line 106
def color_for(index)
  if @top + index == @current
    :cyan
  elsif @top + index == @selected
    :black
  else
    :white
  end
end
draw() click to toggle source
# File lib/sc20XX/ui/table.rb, line 95
def draw
  draw_header
  draw_body
end
draw_body() click to toggle source
# File lib/sc20XX/ui/table.rb, line 116
def draw_body
  rows(@top, body_height + 1).each_with_index do |row, index|
    with_color(color_for(index)) do
      draw_values(row)
    end
  end
end
draw_header() click to toggle source
# File lib/sc20XX/ui/table.rb, line 100
def draw_header
  with_color(:green_reverse) do
    draw_values(header)
  end
end
draw_values(values) click to toggle source
# File lib/sc20XX/ui/table.rb, line 124
def draw_values(values)
  i = -1
  content = values.map { |value| value.ljust(@sizes[i += 1]) }.join(SEPARATOR)

  line content
end
perform_layout() click to toggle source
# File lib/sc20XX/ui/table.rb, line 83
def perform_layout
  @sizes = []
  (rows + [header]).each do |row|
    row.each_with_index do |value, index|
      current, max = value.to_s.length, @sizes[index] || 0
      @sizes[index] = current if max < current
    end
  end

  @sizes[-1] = rest_width(@sizes[0...-1])
end
rest_width(elements) click to toggle source
# File lib/sc20XX/ui/table.rb, line 78
def rest_width(elements)
  rect.width - elements.size * SEPARATOR.size -
    elements.inject(0) { |_a, e| + e }
end
rows(start = 0, size = collection.size) click to toggle source
# File lib/sc20XX/ui/table.rb, line 72
def rows(start = 0, size = collection.size)
  collection[start, size].map do |record|
    keys.map { |key| record.send(key).to_s }
  end
end