class Soundcloud9000::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/soundcloud9000/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/soundcloud9000/ui/table.rb, line 24 def bind_to(collection) raise 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/soundcloud9000/ui/table.rb, line 36 def body_height rect.height - @header.size end
bottom?()
click to toggle source
# File lib/soundcloud9000/ui/table.rb, line 40 def bottom? current + 1 >= length end
deselect()
click to toggle source
# File lib/soundcloud9000/ui/table.rb, line 72 def deselect @selected = nil render end
down()
click to toggle source
# File lib/soundcloud9000/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/soundcloud9000/ui/table.rb, line 32 def length @collection.size end
random()
click to toggle source
# File lib/soundcloud9000/ui/table.rb, line 60 def random @current = rand(length) @top -= 1 if @current < @top @top += 1 if @current > body_height render end
select()
click to toggle source
# File lib/soundcloud9000/ui/table.rb, line 67 def select @selected = @current render end
up()
click to toggle source
# File lib/soundcloud9000/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/soundcloud9000/ui/table.rb, line 114 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/soundcloud9000/ui/table.rb, line 103 def draw draw_header draw_body end
draw_body()
click to toggle source
# File lib/soundcloud9000/ui/table.rb, line 124 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/soundcloud9000/ui/table.rb, line 108 def draw_header with_color(:green_reverse) do draw_values(header) end end
draw_values(values)
click to toggle source
# File lib/soundcloud9000/ui/table.rb, line 132 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/soundcloud9000/ui/table.rb, line 90 def perform_layout @sizes = [] (rows + [header]).each do |row| row.each_with_index do |value, index| current = value.to_s.length max = @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/soundcloud9000/ui/table.rb, line 85 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/soundcloud9000/ui/table.rb, line 79 def rows(start = 0, size = collection.size) collection[start, size].map do |record| keys.map { |key| record.send(key).to_s } end end