From 429327cac8b75118087400fc9b5ffed4c4ce83ec Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 2 Oct 2016 08:32:25 +0200 Subject: Use `proc` to fallback to `puts`. --- Library/Homebrew/utils/puts_columns.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'Library') 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 -- cgit v1.2.3