class TheFox::TermKit::TableView
View
sub-class.
Provides functionalities to show data in a scrollable table.
Attributes
cells[R]
cells_height_total[R]
cursor_direction[R]
cursor_position[R]
cursor_position_old[R]
data[R]
attr_reader :table
header[R]
header_height[R]
highlighted_cell[RW]
page_begin[R]
page_direction[R]
page_end[R]
page_height[R]
Public Class Methods
new(name = nil)
click to toggle source
Calls superclass method
TheFox::TermKit::View::new
# File lib/termkit/view/view_table.rb, line 28 def initialize(name = nil) # puts "TableView initialize '#{name.inspect}'" super(name) @header = nil @header_height = 0 @data = [] @cells = [] @cells_height_total = 0 @highlighted_cell = nil @cursor_position = 0 @cursor_position_old = 0 @cursor_direction = 0 @page_begin = 0 @page_end = 0 @page_height = 0 @page_direction = 0 @page_range = nil @needs_refresh = true @table = View.new("#{@name}_table") @table.is_visible = true add_subview(@table) end
Public Instance Methods
cursor_down()
click to toggle source
# File lib/termkit/view/view_table.rb, line 157 def cursor_down self.cursor_position = @cursor_position + 1 end
cursor_position=(cursor_position)
click to toggle source
# File lib/termkit/view/view_table.rb, line 144 def cursor_position=(cursor_position) @cursor_position_old = @cursor_position @cursor_position = cursor_position @needs_refresh = true calc_cursor calc_page end
cursor_up()
click to toggle source
# File lib/termkit/view/view_table.rb, line 153 def cursor_up self.cursor_position = @cursor_position - 1 end
data=(data)
click to toggle source
# File lib/termkit/view/view_table.rb, line 97 def data=(data) if !data.is_a?(Array) raise ArgumentError, "Argument is not a Array -- #{data.class} given" end @data = data @cells = [] cell_n = 0 y_pos = 0 @data.each do |row| cell = nil row_name = "row_#{cell_n}" case row when String text_view = TextView.new(row, "text_#{row_name}") text_view.is_visible = true # text_view.text = row cell = CellTableView.new(text_view, "cell_#{row_name}") when CellTableView cell = row else raise NotImplementedError, "Class '#{row.class}' not implemented yet" end @cells.push(cell) #cell.is_visible = false cell.is_visible = true #cell.position = Point.new(0, y_pos) #@table.add_subview(cell) y_pos += cell.height cell_n += 1 end @cells_height_total = y_pos @needs_refresh = true calc_page_height calc_cursor calc_page end
header=(header)
click to toggle source
# File lib/termkit/view/view_table.rb, line 65 def header=(header) unless header.is_a?(View) raise ArgumentError, "Argument is not a View -- #{header.class} given" end unless @header.nil? remove_subview(@header) end @header = header unless header.nil? @header_height = @header.height add_subview(@header) end @table.position = Point.new(0, @header_height) @needs_refresh = true calc_page_height end
is_cursor_at_bottom?()
click to toggle source
# File lib/termkit/view/view_table.rb, line 161 def is_cursor_at_bottom? @cursor_position == @cells_height_total - 1 end
refresh()
click to toggle source
# File lib/termkit/view/view_table.rb, line 174 def refresh new_page_range = Range.new(@page_begin, @page_end) affected_cells = @cells[new_page_range] y_pos = 0 cell_n = 0 affected_cells.each do |cell| highlighted = @cursor_position == (cell_n + @page_begin) # puts "#{@name} -- [+] #{cell} n=#{cell_n} y=#{y_pos} h=#{highlighted ? 'Y' : 'N'}/#{cell.highlighted ? 'Y' : 'N'}" cell.highlighted = highlighted if highlighted @highlighted_cell = cell end # cell.size = Size.new(@size.width, nil) # puts "#{@name} -- [+] #{cell} y=#{y_pos} position" cell.position = Point.new(0, y_pos) unless @table.is_subview?(cell) # puts "#{@name} -- [+] #{cell} y=#{y_pos} add_subview" @table.add_subview(cell) end # puts "#{@name} -- [+] #{cell} y=#{y_pos} END" y_pos += cell.height cell_n += 1 end # Hide out-of-scope cell(s) here. In the best case it's only ONE cell that will # be hidden. If you scroll down the top cell will be hidden, if you scroll up # only the bottom cell will be hidden. (@cells - affected_cells).select{ |cell| cell.is_visible? }.each do |cell| # puts "#{@name} -- [-] #{cell} y=#{cell.position.y} r?=#{cell.needs_rendering? ? 'Y' : 'N'}" @table.remove_subview(cell) end @page_range = new_page_range @needs_refresh = false end
remove_header()
click to toggle source
# File lib/termkit/view/view_table.rb, line 87 def remove_header @header = nil @header_height = 0 @table.position = Point.new(0, @header_height) @needs_refresh = true calc_page_height end
render(area = nil)
click to toggle source
Calls superclass method
TheFox::TermKit::View#render
# File lib/termkit/view/view_table.rb, line 165 def render(area = nil) puts "#{@name} -- render r?=#{@needs_refresh ? 'Y' : 'N'}" if @needs_refresh refresh end super(area) end
size=(size)
click to toggle source
Calls superclass method
TheFox::TermKit::View#size=
# File lib/termkit/view/view_table.rb, line 56 def size=(size) super(size) @needs_refresh = true calc_page_height calc_cursor calc_page end
Private Instance Methods
calc_cursor()
click to toggle source
# File lib/termkit/view/view_table.rb, line 233 def calc_cursor # puts "calc_cursor @cursor_position '#{@cursor_position}' '#{@cells_height_total}'" if @cursor_position > @cells_height_total - 1 @cursor_position = @cells_height_total - 1 # puts "#{@name} -- calc_cursor A #{@cursor_position}" end if @cursor_position < 0 @cursor_position = 0 # puts "#{@name} -- calc_cursor B #{@cursor_position}" end # -1 up # 0 unchanged # +1 down if @cursor_position == @cursor_position_old @cursor_direction = 0 elsif @cursor_position > @cursor_position_old @cursor_direction = 1 else @cursor_direction = -1 end # puts "cursor n='#{@cursor_position}' o='#{@cursor_position_old}' d='#{cursor_direction}'" end
calc_page()
click to toggle source
# File lib/termkit/view/view_table.rb, line 259 def calc_page # -1 up # 0 unchanged # +1 down if @cursor_position > @page_end @page_direction = 1 elsif @cursor_position < @page_begin @page_direction = -1 else @page_direction = 0 end if @page_direction == 1 @page_begin = @cursor_position - @page_height + 1 # puts "#{@name} -- calc_page page_begin A #{page_begin} = #{@cursor_position} - #{@page_height} + 1" elsif @page_direction == -1 @page_begin = @cursor_position # puts "#{@name} -- calc_page page_begin B #{page_begin}" end page_begin_max = @cells_height_total - @page_height if page_begin_max < 0 page_begin_max = 0 end if @page_begin > page_begin_max @page_begin = page_begin_max # puts "#{@name} -- calc_page page_begin C #{page_begin}" end @page_end = @page_begin + @page_height - 1 # puts "#{@name} -- calc_page b=#{@page_begin} e=#{@page_end} h=#{@page_height} d=#{@page_direction}" end
calc_page_height()
click to toggle source
# File lib/termkit/view/view_table.rb, line 221 def calc_page_height # puts "calc_page_height" if @size.nil? || @size.height.nil? # puts "calc_page_height size is nil" @page_height = @cells_height_total # puts "#{@name} -- calc_page_height A #{@page_height}" else @page_height = @size.height - @header_height # puts "#{@name} -- calc_page_height B #{@page_height} = #{@size.height} - #{@header_height}" end end