Class: HighLine::List

Inherits:
Object show all
Defined in:
lib/highline/list.rb

Overview

List class with some convenience methods like #col_down.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, options = {}) ⇒ List

Returns a new instance of List.

Parameters:

  • items (#to_a)

    an array of items to compose the list.

  • options (Hash) (defaults to: {})

    a hash of options to tailor the list.

Options Hash (options):

  • :transpose (Boolean) — default: false
  • :col_down (Boolean) — default: false
  • :cols (Integer) — default: 1

    set #cols.



61
62
63
64
65
66
67
# 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

Instance Attribute Details

#col_down_modeBoolean (readonly)

Content are distributed first by column in col down mode.

Examples:

A two columns array like this:

[ [ "a", "b" ],
  [ "c", "d" ],
  [ "e", "f" ],
  [ "g", "h" ],
  [ "i", "j" ] ]

In col down mode will be like this:

[ [ "a", "f"],
  [ "b", "g"],
  [ "c", "h"],
  [ "d", "i"],
  [ "e", "j"] ]

Returns:

  • (Boolean)

See Also:



53
54
55
# File 'lib/highline/list.rb', line 53

def col_down_mode
  @col_down_mode
end

#colsInteger

Number of columns for each list row.

Returns:

  • (Integer)


14
15
16
# File 'lib/highline/list.rb', line 14

def cols
  @cols
end

#itemsArray (readonly)

Original given items argument. It’s frozen at initialization time and all later transformations will happen on #list.

Returns:

  • (Array)


10
11
12
# File 'lib/highline/list.rb', line 10

def items
  @items
end

#row_join_stringString

The String that will be used to join each cell of the list and stringfying it.

Returns:

  • (String)

    defaults to “ ” (space)



136
137
138
# File 'lib/highline/list.rb', line 136

def row_join_string
  @row_join_string ||= "  "
end

#transpose_modeBoolean (readonly)

Columns turn into rows in transpose mode.

Examples:

A two columns array like this:

[ [ "a", "b" ],
  [ "c", "d" ],
  [ "e", "f" ],
  [ "g", "h" ],
  [ "i", "j" ] ]

When in transpose mode will be like this:

[ [ "a", "c", "e", "g", "i" ],
  [ "b", "d", "f", "h", "j" ] ]

Returns:

  • (Boolean)

See Also:



32
33
34
# File 'lib/highline/list.rb', line 32

def transpose_mode
  @transpose_mode
end

Instance Method Details

#col_downself

Slice the list by rows and transpose it.

Returns:

  • (self)


81
82
83
84
85
# File 'lib/highline/list.rb', line 81

def col_down
  slice_by_rows
  transpose
  self
end

#listArray

Returns an Array representation of the list in its current state.

Returns:

  • (Array)

    @list.dup



112
113
114
# File 'lib/highline/list.rb', line 112

def list
  @list.dup
end

#row_join_str_sizeInteger

Returns the row join string size. Useful for calculating the actual size of rendered list.

Returns:

  • (Integer)


148
149
150
# File 'lib/highline/list.rb', line 148

def row_join_str_size
  row_join_string.size
end

#slice_by_colsself

Slice the list by cols based on the #cols param.

Returns:

  • (self)


97
98
99
100
# File 'lib/highline/list.rb', line 97

def slice_by_cols
  @list = items_sliced_by_cols
  self
end

#slice_by_rowsself

Slice the list by rows. The row count is calculated indirectly based on the #cols param and the items count.

Returns:

  • (self)


90
91
92
93
# File 'lib/highline/list.rb', line 90

def slice_by_rows
  @list = items_sliced_by_rows
  self
end

#to_aArray

Returns an Array representation of the list in its current state.

Returns:

  • (Array)

    @list.dup



117
118
119
# File 'lib/highline/list.rb', line 117

def to_a
  list
end

#to_sString

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.

Returns:



129
130
131
# File 'lib/highline/list.rb', line 129

def to_s
  list.map { |row| stringfy(row) }.join
end

#transposeself

Transpose the (already sliced by rows) list,

turning its rows into columns.

Returns:

  • (self)


72
73
74
75
76
77
# 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