Class: HighLine::ListRenderer
Overview
This class is a utility for quickly and easily laying out lists to be used by HighLine.
Instance Attribute Summary collapse
-
#highline ⇒ HighLine
readonly
Context.
-
#items ⇒ Array
readonly
Items list.
-
#mode ⇒ Symbol
readonly
The current mode the List is being rendered.
-
#option ⇒ Object
readonly
Changes the behaviour of some modes.
Instance Method Summary collapse
-
#initialize(items, mode = :rows, option = nil, highline) ⇒ ListRenderer
constructor
The only required parameters are items and highline.
-
#render ⇒ String
Render the list using the appropriate mode and options.
Constructor Details
#initialize(items, mode = :rows, option = nil, highline) ⇒ ListRenderer
The only required parameters are items and highline. Recognized modes are:
:columns_across
-
items will be placed in columns, flowing from left to right. If given, option is the number of columns to be used. When absent, columns will be determined based on wrap_at or a default of 80 characters.
:columns_down
-
Identical to
:columns_across
, save flow goes down. :uneven_columns_across
-
Like
:columns_across
but each column is sized independently. :uneven_columns_down
-
Like
:columns_down
but each column is sized independently. :inline
-
All items are placed on a single line. The last two items are separated by option or a default of “ or ”. All other items are separated by “, ”.
:rows
-
The default mode. Each of the items is placed on its own line. The option parameter is ignored in this mode.
Each member of the items Array is passed through ERb and thus can contain their own expansions. Color escape expansions do not contribute to the final field width.
62 63 64 65 66 67 |
# File 'lib/highline/list_renderer.rb', line 62 def initialize(items, mode = :rows, option = nil, highline) @highline = highline @mode = mode @option = option @items = render_list_items(items) end |
Instance Attribute Details
#highline ⇒ HighLine (readonly)
Returns context.
27 28 29 |
# File 'lib/highline/list_renderer.rb', line 27 def highline @highline end |
#items ⇒ Array (readonly)
Items list
15 16 17 |
# File 'lib/highline/list_renderer.rb', line 15 def items @items end |
#mode ⇒ Symbol (readonly)
Returns the current mode the List is being rendered.
19 20 21 |
# File 'lib/highline/list_renderer.rb', line 19 def mode @mode end |
#option ⇒ Object (readonly)
Changes the behaviour of some modes. Example, in :inline mode the option is treated as the ‘end separator’ (defaults to “ or ”)
24 25 26 |
# File 'lib/highline/list_renderer.rb', line 24 def option @option end |
Instance Method Details
#render ⇒ String
Render the list using the appropriate mode and options.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/highline/list_renderer.rb', line 71 def render return "" if items.empty? case mode when :inline list_inline_mode when :columns_across list_columns_across_mode when :columns_down list_columns_down_mode when :uneven_columns_across list_uneven_columns_mode when :uneven_columns_down list_uneven_columns_down_mode else list_default_mode end end |