aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorMartin Afanasjew2015-10-02 21:47:40 +0200
committerMike McQuaid2015-10-14 15:07:57 +0100
commitc3e7a646486b256ec012a51ac60f25a65f78f62e (patch)
tree4e086fcac702a962b121dfc66ae2547e70b9cf4c /Library
parente04a463f11d06ca5b6ef8142714ee651cb161815 (diff)
downloadbrew-c3e7a646486b256ec012a51ac60f25a65f78f62e.tar.bz2
utils: re-implement and improve column-wise output
Diffstat (limited to 'Library')
-rw-r--r--Library/Homebrew/utils.rb37
1 files changed, 27 insertions, 10 deletions
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb
index caf46d6fb..3822c88ce 100644
--- a/Library/Homebrew/utils.rb
+++ b/Library/Homebrew/utils.rb
@@ -260,17 +260,34 @@ def puts_columns(items, star_items = [])
items = items.map { |item| star_items.include?(item) ? "#{item}*" : item }
end
- if $stdout.tty?
- # determine the best width to display for different console sizes
- console_width = `/bin/stty size`.chomp.split(" ").last.to_i
- console_width = 80 if console_width <= 0
- max_len = items.reduce(0) { |max, item| l = item.length ; l > max ? l : max }
- optimal_col_width = (console_width.to_f / (max_len + 2).to_f).floor
- cols = optimal_col_width > 1 ? optimal_col_width : 1
-
- IO.popen("/usr/bin/pr -#{cols} -t -w#{console_width}", "w") { |io| io.puts(items) }
- else
+ 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
+ max_len = items.max_by(&:length).length
+ 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.map { |item| item.ljust(col_width) }
+ 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