class Reline::LineEditor::MenuInfo

Attributes

list[R]

Public Class Methods

new(list) click to toggle source
# File lib/reline/line_editor.rb, line 53
def initialize(list)
  @list = list
end

Public Instance Methods

lines(screen_width) click to toggle source
# File lib/reline/line_editor.rb, line 57
def lines(screen_width)
  return [] if @list.empty?

  list = @list.sort
  sizes = list.map { |item| Reline::Unicode.calculate_width(item) }
  item_width = sizes.max + 2
  num_cols = [screen_width / item_width, 1].max
  num_rows = list.size.fdiv(num_cols).ceil
  list_with_padding = list.zip(sizes).map { |item, size| item + ' ' * (item_width - size) }
  aligned = (list_with_padding + [nil] * (num_rows * num_cols - list_with_padding.size)).each_slice(num_rows).to_a.transpose
  aligned.map do |row|
    row.join.rstrip
  end
end