123456789_123456789_123456789_123456789_123456789_

Class: Reline::LineEditor::MenuInfo

Relationships & Source Files
Inherits: Object
Defined in: lib/reline/line_editor.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(list) ⇒ MenuInfo

[ GitHub ]

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

def initialize(list)
  @list = list
end

Instance Attribute Details

#list (readonly)

[ GitHub ]

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

attr_reader :list

Instance Method Details

#lines(screen_width)

[ GitHub ]

  
# 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