class HighLine::List
List
class with some convenience methods like {#col_down}.
Attributes
Content are distributed first by column in col down mode. @return [Boolean]
@example A two columns array like this:
[ [ "a", "b" ], [ "c", "d" ], [ "e", "f" ], [ "g", "h" ], [ "i", "j" ] ]
@example In col down mode will be like this:
[ [ "a", "f"], [ "b", "g"], [ "c", "h"], [ "d", "i"], [ "e", "j"] ]
@see transpose_mode
Number of columns for each list row. @return [Integer]
Original given items argument. It’s frozen at initialization time and all later transformations will happen on {#list}. @return [Array]
Set the {#row_join_string}. @see row_join_string
Columns turn into rows in transpose mode. @return [Boolean]
@example A two columns array like this:
[ [ "a", "b" ], [ "c", "d" ], [ "e", "f" ], [ "g", "h" ], [ "i", "j" ] ]
@example When in transpose mode will be like this:
[ [ "a", "c", "e", "g", "i" ], [ "b", "d", "f", "h", "j" ] ]
@see col_down_mode
Public Class Methods
@param items [#to_a] an array of items to compose the list. @param options [Hash] a hash of options to tailor the list. @option options [Boolean] :transpose (false) set {#transpose_mode}. @option options [Boolean] :col_down (false) set {#col_down_mode}. @option options [Integer] :cols (1) set {#cols}.
# File lib/highline/list.rb, line 61 def initialize(items, options = {}) @items = items.to_a.dup.freeze @transpose_mode = options.fetch(:transpose) { false } @col_down_mode = options.fetch(:col_down) { false } @cols = options.fetch(:cols) { 1 } build end
Public Instance Methods
Slice the list by rows and transpose it. @return [self]
# File lib/highline/list.rb, line 81 def col_down slice_by_rows transpose self end
Set the cols number. @return [self]
# File lib/highline/list.rb, line 104 def cols=(cols) @cols = cols build end
Returns an Array representation of the list in its current state. @return [Array] @list.dup
# File lib/highline/list.rb, line 112 def list @list.dup end
Returns the row join string size. Useful for calculating the actual size of rendered list. @return [Integer]
# File lib/highline/list.rb, line 148 def row_join_str_size row_join_string.size end
The String
that will be used to join each cell of the list and stringfying it. @return [String] defaults to “ ” (space)
# File lib/highline/list.rb, line 136 def row_join_string @row_join_string ||= " " end
Slice the list by cols based on the {#cols} param. @return [self]
# File lib/highline/list.rb, line 97 def slice_by_cols @list = items_sliced_by_cols self end
Slice the list by rows. The row count is calculated indirectly based on the {#cols} param and the items count. @return [self]
# File lib/highline/list.rb, line 90 def slice_by_rows @list = items_sliced_by_rows self end
(see list
)
# File lib/highline/list.rb, line 117 def to_a list end
Stringfies the list in its current state. It joins each individual cell with the current {#row_join_string} between them. It joins each individual row with a newline character. So the returned String
is suitable to be directly outputed to the screen, preserving row/columns divisions. @return [String]
# File lib/highline/list.rb, line 129 def to_s list.map { |row| stringfy(row) }.join end
Transpose the (already sliced by rows) list,
turning its rows into columns.
@return [self]
# File lib/highline/list.rb, line 72 def transpose first_row = @list[0] other_rows = @list[1..-1] @list = first_row.zip(*other_rows) self end
Private Instance Methods
# File lib/highline/list.rb, line 154 def build slice_by_cols transpose if transpose_mode col_down if col_down_mode self end
# File lib/highline/list.rb, line 161 def items_sliced_by_cols items.each_slice(cols).to_a end
# File lib/highline/list.rb, line 165 def items_sliced_by_rows items.each_slice(row_count).to_a end
# File lib/highline/list.rb, line 169 def row_count (items.count / cols.to_f).ceil end
# File lib/highline/list.rb, line 173 def stringfy(row) row.compact.join(row_join_string) + "\n" end