mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
parent
21863470d9
commit
d0b044a842
1 changed files with 40 additions and 5 deletions
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "reline"
|
||||||
require_relative "nop"
|
require_relative "nop"
|
||||||
require_relative "../color"
|
require_relative "../color"
|
||||||
|
|
||||||
|
@ -23,8 +24,11 @@ module IRB
|
||||||
end
|
end
|
||||||
|
|
||||||
class Output
|
class Output
|
||||||
|
MARGIN = " "
|
||||||
|
|
||||||
def initialize(grep: nil)
|
def initialize(grep: nil)
|
||||||
@grep = grep
|
@grep = grep
|
||||||
|
@line_width = screen_width
|
||||||
end
|
end
|
||||||
|
|
||||||
def dump(name, strs)
|
def dump(name, strs)
|
||||||
|
@ -32,13 +36,44 @@ module IRB
|
||||||
strs = strs.sort
|
strs = strs.sort
|
||||||
return if strs.empty?
|
return if strs.empty?
|
||||||
|
|
||||||
|
# Attempt a single line
|
||||||
print "#{Color.colorize(name, [:BOLD, :BLUE])}: "
|
print "#{Color.colorize(name, [:BOLD, :BLUE])}: "
|
||||||
if strs.size > 7
|
if fits_on_line?(strs, cols: strs.size, offset: "#{name}: ".length)
|
||||||
len = [strs.map(&:length).max, 16].min
|
puts strs.join(MARGIN)
|
||||||
puts; strs.each_slice(7) { |ss| puts " #{ss.map { |s| "%-#{len}s" % s }.join(" ")}" }
|
return
|
||||||
else
|
|
||||||
puts strs.join(" ")
|
|
||||||
end
|
end
|
||||||
|
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|
|
||||||
|
puts ss.map.with_index { |s, i| "#{MARGIN}%-#{widths[i]}s" % s }.join
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def fits_on_line?(strs, cols:, offset: 0)
|
||||||
|
width = col_widths(strs, cols: cols).sum + MARGIN.length * (cols - 1)
|
||||||
|
width <= @line_width - offset
|
||||||
|
end
|
||||||
|
|
||||||
|
def col_widths(strs, cols:)
|
||||||
|
cols.times.map do |col|
|
||||||
|
(col...strs.size).step(cols.size - 1).map do |i|
|
||||||
|
strs[i].length
|
||||||
|
end.max
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def screen_width
|
||||||
|
Reline.get_screen_size.last
|
||||||
|
rescue Errno::EINVAL # in `winsize': Invalid argument - <STDIN>
|
||||||
|
79
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
private_constant :Output
|
private_constant :Output
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue