class ColorLS::Layout

Public Class Methods

new(contents, widths, line_size) click to toggle source
# File lib/colorls/layout.rb, line 5
def initialize(contents, widths, line_size)
  @max_widths = widths
  @contents = contents
  @screen_width = line_size
end

Public Instance Methods

each_line() { |compact, max_widths| ... } click to toggle source
# File lib/colorls/layout.rb, line 11
def each_line
  return if @contents.empty?

  get_chunks(chunk_size).each { |line| yield(line.compact, @max_widths) }
end

Private Instance Methods

chunk_size() click to toggle source
# File lib/colorls/layout.rb, line 19
def chunk_size
  min_size = @max_widths.min
  max_chunks = [1, @screen_width / min_size].max
  max_chunks = [max_chunks, @max_widths.size].min
  min_chunks = 1

  loop do
    mid = ((max_chunks + min_chunks).to_f / 2).ceil

    size, max_widths = column_widths(mid)

    if min_chunks < max_chunks && not_in_line(max_widths)
      max_chunks = mid - 1
    elsif min_chunks < mid
      min_chunks = mid
    else
      @max_widths = max_widths
      return size
    end
  end
end
not_in_line(max_widths) click to toggle source
# File lib/colorls/layout.rb, line 41
def not_in_line(max_widths)
  max_widths.sum > @screen_width
end