Refactor puts_columns.
This commit is contained in:
parent
527a62b64b
commit
6e10f91371
@ -1,16 +1,17 @@
|
|||||||
require "pathname"
|
require "pathname"
|
||||||
require "emoji"
|
require "emoji"
|
||||||
require "exceptions"
|
require "exceptions"
|
||||||
require "utils/formatter"
|
|
||||||
require "utils/hash"
|
|
||||||
require "utils/json"
|
|
||||||
require "utils/inreplace"
|
|
||||||
require "utils/popen"
|
|
||||||
require "utils/fork"
|
|
||||||
require "utils/git"
|
|
||||||
require "utils/analytics"
|
require "utils/analytics"
|
||||||
require "utils/github"
|
|
||||||
require "utils/curl"
|
require "utils/curl"
|
||||||
|
require "utils/fork"
|
||||||
|
require "utils/formatter"
|
||||||
|
require "utils/git"
|
||||||
|
require "utils/github"
|
||||||
|
require "utils/hash"
|
||||||
|
require "utils/inreplace"
|
||||||
|
require "utils/json"
|
||||||
|
require "utils/popen"
|
||||||
|
require "utils/puts_columns"
|
||||||
require "utils/tty"
|
require "utils/tty"
|
||||||
|
|
||||||
def ohai(title, *sput)
|
def ohai(title, *sput)
|
||||||
@ -286,43 +287,6 @@ def quiet_system(cmd, *args)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def puts_columns(items)
|
|
||||||
return if items.empty?
|
|
||||||
|
|
||||||
unless $stdout.tty?
|
|
||||||
puts items
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
# TTY case: If possible, output using multiple columns.
|
|
||||||
console_width = Tty.width
|
|
||||||
console_width = 80 if console_width <= 0
|
|
||||||
plain_item_lengths = items.map { |s| Tty.strip_ansi(s).length }
|
|
||||||
max_len = plain_item_lengths.max
|
|
||||||
col_gap = 2 # number of spaces between columns
|
|
||||||
gap_str = " " * col_gap
|
|
||||||
cols = (console_width + col_gap) / (max_len + col_gap)
|
|
||||||
cols = 1 if cols < 1
|
|
||||||
rows = (items.size + cols - 1) / cols
|
|
||||||
cols = (items.size + rows - 1) / rows # avoid empty trailing columns
|
|
||||||
|
|
||||||
if cols >= 2
|
|
||||||
col_width = (console_width + col_gap) / cols - col_gap
|
|
||||||
items = items.each_with_index.map do |item, index|
|
|
||||||
item + "".ljust(col_width - plain_item_lengths[index])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if cols == 1
|
|
||||||
puts items
|
|
||||||
else
|
|
||||||
rows.times do |row_index|
|
|
||||||
item_indices_for_row = row_index.step(items.size - 1, rows).to_a
|
|
||||||
puts items.values_at(*item_indices_for_row).join(gap_str)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def which(cmd, path = ENV["PATH"])
|
def which(cmd, path = ENV["PATH"])
|
||||||
path.split(File::PATH_SEPARATOR).each do |p|
|
path.split(File::PATH_SEPARATOR).each do |p|
|
||||||
begin
|
begin
|
||||||
|
|||||||
49
Library/Homebrew/utils/puts_columns.rb
Normal file
49
Library/Homebrew/utils/puts_columns.rb
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
require "utils/tty"
|
||||||
|
|
||||||
|
class IO
|
||||||
|
def puts_columns(*objects, gap_size: 2)
|
||||||
|
objects.flatten!
|
||||||
|
|
||||||
|
if objects.empty? || !tty?
|
||||||
|
puts(*objects)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
console_width = Tty.width
|
||||||
|
|
||||||
|
object_lengths = objects.map { |obj| Tty.strip_ansi(obj.to_s).length }
|
||||||
|
|
||||||
|
cols = (console_width + gap_size) / (object_lengths.max + gap_size)
|
||||||
|
|
||||||
|
if cols < 2
|
||||||
|
puts(*objects)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
rows = (objects.count + cols - 1) / cols
|
||||||
|
cols = (objects.count + rows - 1) / rows # avoid empty trailing columns
|
||||||
|
|
||||||
|
col_width = (console_width + gap_size) / cols - gap_size
|
||||||
|
|
||||||
|
gap_string = "".rjust(gap_size)
|
||||||
|
|
||||||
|
rows.times do |row_index|
|
||||||
|
item_indices_for_row = row_index.step(objects.size - 1, rows).to_a
|
||||||
|
|
||||||
|
first_n = item_indices_for_row[0...-1].map { |index|
|
||||||
|
objects[index] + "".rjust(col_width - object_lengths[index])
|
||||||
|
}
|
||||||
|
|
||||||
|
# don't add trailing whitespace to last column
|
||||||
|
last = objects.values_at(item_indices_for_row.last)
|
||||||
|
|
||||||
|
puts (first_n + last).join(gap_string)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module Kernel
|
||||||
|
def puts_columns(*objects)
|
||||||
|
$stdout.puts_columns(*objects)
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
x
Reference in New Issue
Block a user