123456789_123456789_123456789_123456789_123456789_

Class: IRB::Command::Ls::Output

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/irb/command/ls.rb

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

.new(grep: nil) ⇒ Output

[ GitHub ]

  
# File 'lib/irb/command/ls.rb', line 95

def initialize(grep: nil)
  @grep = grep
  @line_width = screen_width - MARGIN.length # right padding
  @io = StringIO.new
end

Instance Method Details

#col_widths(strs, cols:) (private)

[ GitHub ]

  
# File 'lib/irb/command/ls.rb', line 136

def col_widths(strs, cols:)
  cols.times.map do |col|
    (col...strs.size).step(cols).map do |i|
      strs[i].length
    end.max
  end
end

#dump(name, strs)

[ GitHub ]

  
# File 'lib/irb/command/ls.rb', line 105

def dump(name, strs)
  strs = strs.grep(@grep) if @grep
  strs = strs.sort
  return if strs.empty?

  # Attempt a single line
  @io.print "#{Color.colorize(name, [:BOLD, :BLUE])}: "
  if fits_on_line?(strs, cols: strs.size, offset: "#{name}: ".length)
    @io.puts strs.join(MARGIN)
    return
  end
  @io.puts

  # Dump with the largest # of columns that fits on a line
  cols = strs.size
  until fits_on_line?(strs, cols: cols, offset: MARGIN.length) || cols == 1
    cols -= 1
  end
  widths = col_widths(strs, cols: cols)
  strs.each_slice(cols) do |ss|
    @io.puts ss.map.with_index { |s, i| "#{MARGIN}%-#{widths[i]}s" % s }.join
  end
end

#fits_on_line?(strs, cols:, offset: 0) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/irb/command/ls.rb', line 131

def fits_on_line?(strs, cols:, offset: 0)
  width = col_widths(strs, cols: cols).sum + MARGIN.length * (cols - 1)
  width <= @line_width - offset
end

#screen_width (private)

[ GitHub ]

  
# File 'lib/irb/command/ls.rb', line 144

def screen_width
  Reline.get_screen_size.last
rescue Errno::EINVAL # in `winsize': Invalid argument - <STDIN>
  80
end