diff options
| author | Markus Reiter | 2016-10-02 04:11:43 +0200 |
|---|---|---|
| committer | Markus Reiter | 2016-10-15 17:13:37 +0200 |
| commit | 6e10f913714f195c2bbc77786f86342378243c1f (patch) | |
| tree | ca7cf2531077efd2d1c6e67fd6eb96d43c46d76e /Library/Homebrew/utils.rb | |
| parent | 527a62b64bd8c578af08540166a8b1fd5bce521f (diff) | |
| download | brew-6e10f913714f195c2bbc77786f86342378243c1f.tar.bz2 | |
Refactor `puts_columns`.
Diffstat (limited to 'Library/Homebrew/utils.rb')
| -rw-r--r-- | Library/Homebrew/utils.rb | 50 |
1 files changed, 7 insertions, 43 deletions
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 79e5287e8..e31339e16 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -1,16 +1,17 @@ require "pathname" require "emoji" require "exceptions" +require "utils/analytics" +require "utils/curl" +require "utils/fork" require "utils/formatter" +require "utils/git" +require "utils/github" require "utils/hash" -require "utils/json" require "utils/inreplace" +require "utils/json" require "utils/popen" -require "utils/fork" -require "utils/git" -require "utils/analytics" -require "utils/github" -require "utils/curl" +require "utils/puts_columns" require "utils/tty" def ohai(title, *sput) @@ -286,43 +287,6 @@ def quiet_system(cmd, *args) end end -def puts_columns(items) - return if items.empty? - - 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 - plain_item_lengths = items.map { |s| Tty.strip_ansi(s).length } - max_len = plain_item_lengths.max - 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.each_with_index.map do |item, index| - item + "".ljust(col_width - plain_item_lengths[index]) - end - 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 - def which(cmd, path = ENV["PATH"]) path.split(File::PATH_SEPARATOR).each do |p| begin |
