class HighLine::ListRenderer


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

def actual_length(text)

def actual_length(text)
  HighLine::Wrapper.actual_length text
end

def actual_lengths_for(line)

def actual_lengths_for(line)
  line.map do |item|
    actual_length(item)
  end
end

def col_count

def col_count
  option || col_count_calculate
end

def col_count_calculate

def col_count_calculate
  result = (line_size_limit + row_join_str_size) / (items_max_length + row_join_str_size)
  result == 0 ? 1 : result
end

def get_col_widths(lines)

def get_col_widths(lines)
  lines = transpose(lines)
  get_segment_widths(lines)
end

def get_row_widths(lines)

def get_row_widths(lines)
  get_segment_widths(lines)
end

def get_segment_widths(lines)

def get_segment_widths(lines)
  lines.map do |col|
    actual_lengths_for(col).max
  end
end

def initialize(items, mode = :rows, option = nil, highline)

def initialize(items, mode = :rows, option = nil, highline)
  @highline = highline
  @mode     = mode
  @option   = option
  @items    = render_list_items(items)
end

def inside_line_size_limit?(widths)

def inside_line_size_limit?(widths)
  line_size = widths.reduce(0) { |sum, n| sum + n + row_join_str_size }
  line_size <= line_size_limit + row_join_str_size
end

def items_max_length

def items_max_length
  @items_max_length ||= max_length(items)
end

def line_size_limit

def line_size_limit
  @line_size_limit ||= (highline.wrap_at || 80)
end

def list_columns_across_mode

def list_columns_across_mode
  HighLine::List.new(right_padded_items, cols: col_count).to_s
end

def list_columns_down_mode

def list_columns_down_mode
  HighLine::List.new(
    right_padded_items,
    cols: col_count,
    col_down: true
  ).to_s
end

def list_default_mode

def list_default_mode
  items.map { |i| "#{i}\n" }.join
end

def list_inline_mode

def list_inline_mode
  end_separator = option || " or "
  if items.size == 1
    items.first
  else
    items[0..-2].join(", ") + "#{end_separator}#{items.last}"
  end
end

def list_uneven_columns_down_mode

def list_uneven_columns_down_mode
  list = HighLine::List.new(items, col_down: true)
  list_uneven_columns_mode(list)
end

def list_uneven_columns_mode(list = nil)

def list_uneven_columns_mode(list = nil)
  list ||= HighLine::List.new(items)
  col_max = option || items.size
  col_max.downto(1) do |column_count|
    list.cols = column_count
    widths = get_col_widths(list)
    if column_count == 1 || # last guess
       inside_line_size_limit?(widths) || # good guess
       option # defined by user
      return pad_uneven_rows(list, widths)
    end
  end
end

def max_length(items)

def max_length(items)
  items.map { |item| actual_length(item) }.max
end

def pad_char

def pad_char
  " "
end

def pad_uneven_rows(list, widths)

def pad_uneven_rows(list, widths)
  right_padded_list = Array(list).map do |row|
    right_pad_row(row.compact, widths)
  end
  stringfy_list(right_padded_list)
end

def render

Returns:
  • (String) - rendered list as String
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

def render_list_items(items)

def render_list_items(items)
  items.to_ary.map do |item|
    item = String(item)
    template = if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
      ERB.new(item, trim_mode: "%")
    else
      ERB.new(item, nil, "%")
    end
    template_renderer =
      HighLine::TemplateRenderer.new(template, self, highline)
    template_renderer.render
  end
end

def right_pad_field(field, width)

def right_pad_field(field, width)
  field = String(field) # nil protection
  pad_size = width - actual_length(field)
  field + (pad_char * pad_size)
end

def right_pad_row(row, widths)

def right_pad_row(row, widths)
  row.zip(widths).map do |field, width|
    right_pad_field(field, width)
  end
end

def right_padded_items

def right_padded_items
  items.map do |item|
    right_pad_field(item, items_max_length)
  end
end

def row_count

def row_count
  (items.count / col_count.to_f).ceil
end

def row_join_str_size

def row_join_str_size
  row_join_string.size
end

def row_join_string

def row_join_string
  @row_join_string ||= "  "
end

def row_to_s(row)

def row_to_s(row)
  row.compact.join(row_join_string) + "\n"
end

def stringfy_list(list)

def stringfy_list(list)
  list.map { |row| row_to_s(row) }.join
end

def transpose(lines)

def transpose(lines)
  lines = Array(lines)
  first_line = lines.shift
  first_line.zip(*lines)
end