Convert puts_columns to puts Formatter.columns.

This commit is contained in:
Markus Reiter 2016-10-02 20:39:43 +02:00
parent 429327cac8
commit 198bf4d3bd
14 changed files with 40 additions and 27 deletions

View File

@ -42,12 +42,14 @@ module Hbc
def self.warn_unavailable_with_suggestion(cask_token, e)
exact_match, partial_matches = Search.search(cask_token)
error_message = e.message
if exact_match
onoe e.message.concat(". Did you mean:\n#{exact_match}")
error_message.concat(". Did you mean:\n#{exact_match}")
elsif !partial_matches.empty?
onoe e.message.concat(". Did you mean one of:")
$stderr.puts_columns(partial_matches.take(20))
error_message.concat(". Did you mean one of:\n")
.concat(Formatter.columns(partial_matches.take(20)))
end
onoe error_message
end
def self.help

View File

@ -68,7 +68,7 @@ module Hbc
elsif @options[:versions]
puts installed_casks.map(&method(:format_versioned))
else
puts_columns installed_casks.map(&:to_s)
puts Formatter.columns(installed_casks.map(&:to_s))
end
installed_casks.empty? ? nil : true

View File

@ -47,7 +47,7 @@ module Hbc
else
ohai "Partial matches"
end
puts_columns partial_matches
puts Formatter.columns(partial_matches)
end
end

View File

@ -12,22 +12,22 @@ module Homebrew
cmds = internal_commands + external_commands
cmds += internal_developer_commands
cmds += HOMEBREW_INTERNAL_COMMAND_ALIASES.keys if ARGV.include? "--include-aliases"
puts_columns cmds.sort
puts Formatter.columns(cmds.sort)
else
# Find commands in Homebrew/cmd
puts "Built-in commands"
puts_columns internal_commands
puts Formatter.columns(internal_commands)
# Find commands in Homebrew/dev-cmd
puts
puts "Built-in developer commands"
puts_columns internal_developer_commands
puts Formatter.columns(internal_developer_commands)
# Find commands in the path
unless (exts = external_commands).empty?
puts
puts "External commands"
puts_columns exts
puts Formatter.columns(exts)
end
end
end

View File

@ -183,7 +183,7 @@ module Homebrew
puts "To install it, run:\n brew install #{formulae_search_results.first}"
else
puts "These similarly named formulae were found:"
puts_columns(formulae_search_results)
puts Formatter.columns(formulae_search_results)
puts "To install one of them, run (for example):\n brew install #{formulae_search_results.first}"
end
@ -198,7 +198,7 @@ module Homebrew
puts "To install it, run:\n brew install #{taps_search_results.first}"
else
puts "These formulae were found in taps:"
puts_columns(taps_search_results)
puts Formatter.columns(taps_search_results)
puts "To install one of them, run (for example):\n brew install #{taps_search_results.first}"
end
end

View File

@ -49,7 +49,7 @@ module Homebrew
end
end
return if full_names.empty?
puts_columns full_names
puts Formatter.columns(full_names)
else
ENV["CLICOLOR"] = nil
exec "ls", *ARGV.options_only << HOMEBREW_CELLAR

View File

@ -27,7 +27,7 @@ module Homebrew
def search
if ARGV.empty?
puts_columns Formula.full_names
puts Formatter.columns(Formula.full_names)
elsif ARGV.include? "--macports"
exec_browser "https://www.macports.org/ports.php?by=name&substr=#{ARGV.next}"
elsif ARGV.include? "--fink"
@ -54,14 +54,15 @@ module Homebrew
result = search_tap(user, repo, name)
end
puts_columns Array(result)
results = Array(result)
puts Formatter.columns(results) unless results.empty?
else
query = ARGV.first
regex = query_regexp(query)
local_results = search_formulae(regex)
puts_columns(local_results) unless local_results.empty?
puts Formatter.columns(local_results) unless local_results.empty?
tap_results = search_taps(regex)
puts_columns(tap_results) unless tap_results.empty?
puts Formatter.columns(tap_results) unless tap_results.empty?
if $stdout.tty?
count = local_results.length + tap_results.length

View File

@ -553,7 +553,7 @@ class ReporterHub
return if formulae.empty?
# Dump formula list.
ohai title
puts_columns(formulae)
puts Formatter.columns(formulae)
end
def installed?(formula)

View File

@ -94,6 +94,6 @@ module Homebrew
end
return if uses.empty?
puts_columns uses.map(&:full_name)
puts Formatter.columns(uses.map(&:full_name))
end
end

View File

@ -16,3 +16,8 @@ module Tty
reset.bold
end
end
def puts_columns(items)
odeprecated "puts_columns", "puts Formatter.columns"
puts Formatter.columns(items)
end

View File

@ -111,7 +111,7 @@ class UtilTests < Homebrew::TestCase
def test_put_columns_empty
out, err = capture_io do
puts_columns []
puts Formatter.columns([])
end
assert_equal out, "\n"

View File

@ -11,7 +11,6 @@ require "utils/hash"
require "utils/inreplace"
require "utils/json"
require "utils/popen"
require "utils/puts_columns"
require "utils/tty"
def ohai(title, *sput)

View File

@ -1,3 +1,4 @@
require "utils/formatter/columns"
require "utils/tty"
module Formatter

View File

@ -1,19 +1,20 @@
require "utils/tty"
module Kernel
def puts_columns(*objects, gap_size: 2)
objects.flatten!
module Formatter
module_function
def columns(*objects, gap_size: 2)
objects = objects.flatten.map(&:to_s)
fallback = proc do
puts(*objects)
return
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.to_s).length }
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
@ -25,6 +26,8 @@ module Kernel
gap_string = "".rjust(gap_size)
output = ""
rows.times do |row_index|
item_indices_for_row = row_index.step(objects.size - 1, rows).to_a
@ -35,7 +38,9 @@ module Kernel
# don't add trailing whitespace to last column
last = objects.values_at(item_indices_for_row.last)
puts (first_n + last).join(gap_string)
output.concat((first_n + last).join(gap_string)).concat("\n")
end
output
end
end