Class: HighLine::ListRenderer

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

Overview

This class is a utility for quickly and easily laying out lists to be used by HighLine.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • items (Array)

    the Array of items to list

  • mode (Symbol) (defaults to: :rows)

    controls how that list is formed

  • option (defaults to: nil)

    has different effects, depending on the mode.

  • highline (HighLine)

    a HighLine instance to direct the output to.



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

#highlineHighLine (readonly)

Returns context.

Returns:



27
28
29
# File 'lib/highline/list_renderer.rb', line 27

def highline
  @highline
end

#itemsArray (readonly)

Items list

Returns:

  • (Array)


15
16
17
# File 'lib/highline/list_renderer.rb', line 15

def items
  @items
end

#modeSymbol (readonly)

Returns the current mode the List is being rendered.

Returns:

  • (Symbol)

    the current mode the List is being rendered

See Also:



19
20
21
# File 'lib/highline/list_renderer.rb', line 19

def mode
  @mode
end

#optionObject (readonly)

Changes the behaviour of some modes. Example, in :inline mode the option is treated as the ‘end separator’ (defaults to “ or ”)

Returns:

  • option parameter that changes the behaviour of some modes.



24
25
26
# File 'lib/highline/list_renderer.rb', line 24

def option
  @option
end

Instance Method Details

#renderString

Render the list using the appropriate mode and options.

Returns:

  • (String)

    rendered list as String



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