aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorMartin Afanasjew2015-10-02 21:56:05 +0200
committerMike McQuaid2015-10-14 15:07:57 +0100
commit4cf703d5d8f7fffc86866084c6abb8003d78ed9e (patch)
tree4f3e9a3f105f0418d6ab92cdfdcfd5886aac938d /Library
parentc3e7a646486b256ec012a51ac60f25a65f78f62e (diff)
downloadbrew-4cf703d5d8f7fffc86866084c6abb8003d78ed9e.tar.bz2
utils: highlight items in column-wise output
Closes Homebrew/homebrew#44343. Signed-off-by: Mike McQuaid <mike@mikemcquaid.com>
Diffstat (limited to 'Library')
-rw-r--r--Library/Homebrew/utils.rb21
1 files changed, 15 insertions, 6 deletions
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb
index 3822c88ce..528bedd79 100644
--- a/Library/Homebrew/utils.rb
+++ b/Library/Homebrew/utils.rb
@@ -42,6 +42,10 @@ class Tty
bold 30
end
+ def highlight
+ bold 43
+ end
+
def width
`/usr/bin/tput cols`.strip.to_i
end
@@ -253,13 +257,9 @@ def curl(*args)
safe_system curl, *args
end
-def puts_columns(items, star_items = [])
+def puts_columns(items, highlight = [])
return if items.empty?
- if star_items && star_items.any?
- items = items.map { |item| star_items.include?(item) ? "#{item}*" : item }
- end
-
unless $stdout.tty?
puts items
return
@@ -276,9 +276,18 @@ def puts_columns(items, star_items = [])
rows = (items.size + cols - 1) / cols
cols = (items.size + rows - 1) / rows # avoid empty trailing columns
+ plain_item_lengths = items.map(&:length) if cols >= 2
+ if highlight && highlight.any?
+ items = items.map do |item|
+ highlight.include?(item) ? "#{Tty.highlight}#{item}#{Tty.reset}" : item
+ end
+ end
+
if cols >= 2
col_width = (console_width + col_gap) / cols - col_gap
- items = items.map { |item| item.ljust(col_width) }
+ items = items.each_with_index.map do |item, index|
+ item + "".ljust(col_width - plain_item_lengths[index])
+ end
end
if cols == 1