From 6e10f913714f195c2bbc77786f86342378243c1f Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 2 Oct 2016 04:11:43 +0200 Subject: Refactor `puts_columns`. --- Library/Homebrew/utils/puts_columns.rb | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Library/Homebrew/utils/puts_columns.rb (limited to 'Library/Homebrew/utils') diff --git a/Library/Homebrew/utils/puts_columns.rb b/Library/Homebrew/utils/puts_columns.rb new file mode 100644 index 000000000..94a5354ad --- /dev/null +++ b/Library/Homebrew/utils/puts_columns.rb @@ -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 -- cgit v1.2.3 From 4bfeaaf21259637c1ad1a98b479fc0e4fdaea56e Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 2 Oct 2016 07:34:53 +0200 Subject: `puts_columns` only has to be defined inside `module Kernel`. --- Library/Homebrew/utils/puts_columns.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'Library/Homebrew/utils') diff --git a/Library/Homebrew/utils/puts_columns.rb b/Library/Homebrew/utils/puts_columns.rb index 94a5354ad..7f8e2d319 100644 --- a/Library/Homebrew/utils/puts_columns.rb +++ b/Library/Homebrew/utils/puts_columns.rb @@ -1,10 +1,10 @@ require "utils/tty" -class IO +module Kernel def puts_columns(*objects, gap_size: 2) objects.flatten! - if objects.empty? || !tty? + if objects.empty? || (respond_to?(:tty?) && !tty?) puts(*objects) return end @@ -41,9 +41,3 @@ class IO end end end - -module Kernel - def puts_columns(*objects) - $stdout.puts_columns(*objects) - end -end -- cgit v1.2.3 From 5ee6df6d822ee1217d2240b85ea1597efc4a3696 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 2 Oct 2016 08:19:06 +0200 Subject: Fallback to `$stdout.tty?` unless `respond_to?(:tty?)`. --- Library/Homebrew/utils/puts_columns.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Library/Homebrew/utils') diff --git a/Library/Homebrew/utils/puts_columns.rb b/Library/Homebrew/utils/puts_columns.rb index 7f8e2d319..7ff275143 100644 --- a/Library/Homebrew/utils/puts_columns.rb +++ b/Library/Homebrew/utils/puts_columns.rb @@ -4,7 +4,7 @@ module Kernel def puts_columns(*objects, gap_size: 2) objects.flatten! - if objects.empty? || (respond_to?(:tty?) && !tty?) + if objects.empty? || (respond_to?(:tty?) ? !tty? : !$stdout.tty?) puts(*objects) return end -- cgit v1.2.3 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/Homebrew/utils') 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 From 198bf4d3bdeaf3047f9af80788ea18cd192541af Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 2 Oct 2016 20:39:43 +0200 Subject: Convert `puts_columns` to `puts Formatter.columns`. --- Library/Homebrew/utils/formatter.rb | 1 + Library/Homebrew/utils/formatter/columns.rb | 46 +++++++++++++++++++++++++++++ Library/Homebrew/utils/puts_columns.rb | 41 ------------------------- 3 files changed, 47 insertions(+), 41 deletions(-) create mode 100644 Library/Homebrew/utils/formatter/columns.rb delete mode 100644 Library/Homebrew/utils/puts_columns.rb (limited to 'Library/Homebrew/utils') diff --git a/Library/Homebrew/utils/formatter.rb b/Library/Homebrew/utils/formatter.rb index cb4f041f4..3759e2b4f 100644 --- a/Library/Homebrew/utils/formatter.rb +++ b/Library/Homebrew/utils/formatter.rb @@ -1,3 +1,4 @@ +require "utils/formatter/columns" require "utils/tty" module Formatter diff --git a/Library/Homebrew/utils/formatter/columns.rb b/Library/Homebrew/utils/formatter/columns.rb new file mode 100644 index 000000000..6488ef35f --- /dev/null +++ b/Library/Homebrew/utils/formatter/columns.rb @@ -0,0 +1,46 @@ +require "utils/tty" + +module Formatter + module_function + + def columns(*objects, gap_size: 2) + objects = objects.flatten.map(&:to_s) + + fallback = proc do + return objects.join("\n").concat("\n") + end + + 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).length } + cols = (console_width + gap_size) / (object_lengths.max + gap_size) + + fallback.call if cols < 2 + + 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) + + output = "" + + 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) + + output.concat((first_n + last).join(gap_string)).concat("\n") + end + + output + end +end diff --git a/Library/Homebrew/utils/puts_columns.rb b/Library/Homebrew/utils/puts_columns.rb deleted file mode 100644 index 2810918c6..000000000 --- a/Library/Homebrew/utils/puts_columns.rb +++ /dev/null @@ -1,41 +0,0 @@ -require "utils/tty" - -module Kernel - def puts_columns(*objects, gap_size: 2) - objects.flatten! - - fallback = proc do - puts(*objects) - return - end - - 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) - - fallback.call if cols < 2 - - 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 -- cgit v1.2.3 From 581a1245bf2c038a2f35fb10445592c7655108e3 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 15 Oct 2016 17:15:20 +0200 Subject: Move `Formatter.columns` into `utils/formatter`. --- Library/Homebrew/utils/formatter.rb | 42 +++++++++++++++++++++++++- Library/Homebrew/utils/formatter/columns.rb | 46 ----------------------------- 2 files changed, 41 insertions(+), 47 deletions(-) delete mode 100644 Library/Homebrew/utils/formatter/columns.rb (limited to 'Library/Homebrew/utils') diff --git a/Library/Homebrew/utils/formatter.rb b/Library/Homebrew/utils/formatter.rb index 3759e2b4f..a29a0d491 100644 --- a/Library/Homebrew/utils/formatter.rb +++ b/Library/Homebrew/utils/formatter.rb @@ -1,4 +1,3 @@ -require "utils/formatter/columns" require "utils/tty" module Formatter @@ -50,4 +49,45 @@ module Formatter end end private_class_method :prefix + + def columns(*objects, gap_size: 2) + objects = objects.flatten.map(&:to_s) + + fallback = proc do + return objects.join("\n").concat("\n") + end + + 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).length } + cols = (console_width + gap_size) / (object_lengths.max + gap_size) + + fallback.call if cols < 2 + + 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) + + output = "" + + 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) + + output.concat((first_n + last).join(gap_string)).concat("\n") + end + + output + end end diff --git a/Library/Homebrew/utils/formatter/columns.rb b/Library/Homebrew/utils/formatter/columns.rb deleted file mode 100644 index 6488ef35f..000000000 --- a/Library/Homebrew/utils/formatter/columns.rb +++ /dev/null @@ -1,46 +0,0 @@ -require "utils/tty" - -module Formatter - module_function - - def columns(*objects, gap_size: 2) - objects = objects.flatten.map(&:to_s) - - fallback = proc do - return objects.join("\n").concat("\n") - end - - 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).length } - cols = (console_width + gap_size) / (object_lengths.max + gap_size) - - fallback.call if cols < 2 - - 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) - - output = "" - - 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) - - output.concat((first_n + last).join(gap_string)).concat("\n") - end - - output - end -end -- cgit v1.2.3