class BBS::Selector::Columns
The items one under the other in one or more columns, a page of height rows at a time: the layout of a BBS::Selector without across.
Attributes
The rows of items a page shows, and the columns side by side.
The rows of items a page shows, and the columns side by side.
Public Class Methods
# File lib/bbs/selector/layouts.rb, line 15 def initialize(count, height:, columns:, fill:) @count = count @columns = columns.clamp(1, count) natural = (count + @columns - 1) / @columns @height = (height || natural).clamp(1, fill && height ? height : count) end
Lays out count items in columns columns of height rows (default: all the items, shared out among the columns); with fill a page keeps height rows even when the items take fewer.
Public Instance Methods
Source
# File lib/bbs/selector/layouts.rb, line 39 def column_of(index, width) = 1 + (width / @columns * (index % per_page / @height))
The screen column item index starts at, from 1, in a menu width wide.
Source
# File lib/bbs/selector/layouts.rb, line 23 def item_rows = @height
The rows of items on the screen: height.
Source
# File lib/bbs/selector/layouts.rb, line 35 def line_of(index) = (index % per_page % @height) + 1
The screen row of item index, from 1.
Source
# File lib/bbs/selector/layouts.rb, line 32 def page_of(index) = index / per_page
The page item index is on, from 0.
Source
# File lib/bbs/selector/layouts.rb, line 29 def pages = (@count + per_page - 1) / per_page
The pages the items take.
Source
# File lib/bbs/selector/layouts.rb, line 26 def per_page = @height * @columns
The items on a page.
Source
# File lib/bbs/selector/layouts.rb, line 44 def segments(number, selector) first = (selector.page * per_page) + number - 1 cell = selector.width / @columns (0...@columns).flat_map do |c| width = c == @columns - 1 ? selector.width - (cell * c) : cell selector.cell(first + (c * @height), width) end end
The cells of screen row number of selector‘s page: the cell of each column side by side, the last column taking what the others leave.
Source
# File lib/bbs/selector/layouts.rb, line 55 def side(selector, dir) return selector.turn(dir) if @columns == 1 target = selector.index + (dir * @height) return selector.turn(dir) unless same_page?(selector, target) column = column_from(selector, target) selector.jump(column.min_by { |i| [(i - target).abs, i < target ? 1 : 0] }) unless column.empty? end
Moves selector sideways by dir: to the nearest selectable item in the column beside, or to the page beside when there is none.