aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Reiter2016-10-02 08:32:25 +0200
committerMarkus Reiter2016-10-15 17:13:38 +0200
commit429327cac8b75118087400fc9b5ffed4c4ce83ec (patch)
treed1fcbd767f5687b028eccf3316eac3cd2a667969
parent5ee6df6d822ee1217d2240b85ea1597efc4a3696 (diff)
downloadbrew-429327cac8b75118087400fc9b5ffed4c4ce83ec.tar.bz2
Use `proc` to fallback to `puts`.
-rw-r--r--Library/Homebrew/utils/puts_columns.rb12
1 files changed, 5 insertions, 7 deletions
diff --git a/Library/Homebrew/utils/puts_columns.rb b/Library/Homebrew/utils/puts_columns.rb
index 7ff275143..2810918c6 100644
--- a/Library/Homebrew/utils/puts_columns.rb
+++ b/Library/Homebrew/utils/puts_columns.rb
@@ -4,21 +4,19 @@ module Kernel
def puts_columns(*objects, gap_size: 2)
objects.flatten!
- if objects.empty? || (respond_to?(:tty?) ? !tty? : !$stdout.tty?)
+ fallback = proc do
puts(*objects)
return
end
- console_width = Tty.width
+ fallback.call if objects.empty?
+ fallback.call if respond_to?(:tty?) ? !tty? : !$stdout.tty?
+ 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
+ fallback.call if cols < 2
rows = (objects.count + cols - 1) / cols
cols = (objects.count + rows - 1) / rows # avoid empty trailing columns