Class: Reline::LineEditor::MenuInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/reline/line_editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ MenuInfo

Returns a new instance of MenuInfo.



52
53
54
# File 'lib/reline/line_editor.rb', line 52

def initialize(list)
  @list = list
end

Instance Attribute Details

#listObject (readonly)

Returns the value of attribute list.



50
51
52
# File 'lib/reline/line_editor.rb', line 50

def list
  @list
end

Instance Method Details

#lines(screen_width) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/reline/line_editor.rb', line 56

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