Add Formatter::pluralize
.
This commit is contained in:
parent
666ab1438c
commit
c594ffefbc
@ -83,7 +83,7 @@ module Hbc
|
||||
@modified_casks = modified_cask_files.map { |f| Hbc.load(f) }
|
||||
if @modified_casks.any?
|
||||
num_modified = @modified_casks.size
|
||||
ohai "#{num_modified} modified cask#{plural(num_modified)}: " \
|
||||
ohai "#{Formatter.pluralize(num_modified, "modified cask")}: " \
|
||||
"#{@modified_casks.join(" ")}"
|
||||
end
|
||||
@modified_casks
|
||||
@ -120,7 +120,7 @@ module Hbc
|
||||
def report_failures
|
||||
return if failed_casks.empty?
|
||||
num_failed = failed_casks.size
|
||||
odie "audit failed for #{num_failed} cask#{plural(num_failed)}: " \
|
||||
odie "audit failed for #{Formatter.pluralize(num_failed, "cask")}: " \
|
||||
"#{failed_casks.join(" ")}"
|
||||
end
|
||||
|
||||
|
@ -43,7 +43,7 @@ module Homebrew
|
||||
if ARGV.named.empty?
|
||||
if HOMEBREW_CELLAR.exist?
|
||||
count = Formula.racks.length
|
||||
puts "#{count} keg#{plural(count)}, #{HOMEBREW_CELLAR.abv}"
|
||||
puts "#{Formatter.pluralize(count, "keg")}, #{HOMEBREW_CELLAR.abv}"
|
||||
end
|
||||
else
|
||||
ARGV.named.each_with_index do |f, i|
|
||||
|
@ -67,7 +67,7 @@ module Homebrew
|
||||
if link_count.zero?
|
||||
puts "No apps linked to #{target_dir}" if ARGV.verbose?
|
||||
else
|
||||
puts "Linked #{link_count} app#{plural(link_count)} to #{target_dir}"
|
||||
puts "Linked #{Formatter.pluralize(link_count, "app")} to #{target_dir}"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -50,11 +50,11 @@ module Homebrew
|
||||
pinned_count += 1 if tap.pinned?
|
||||
private_count += 1 if tap.private?
|
||||
end
|
||||
info = "#{tap_count} tap#{plural(tap_count)}"
|
||||
info = Formatter.pluralize(tap_count, "tap").to_s
|
||||
info += ", #{pinned_count} pinned"
|
||||
info += ", #{private_count} private"
|
||||
info += ", #{formula_count} formula#{plural(formula_count, "e")}"
|
||||
info += ", #{command_count} command#{plural(command_count)}"
|
||||
info += ", #{Formatter.pluralize(formula_count, "formula")}"
|
||||
info += ", #{Formatter.pluralize(command_count, "command")}"
|
||||
info += ", #{Tap::TAP_DIRECTORY.abv}" if Tap::TAP_DIRECTORY.directory?
|
||||
puts info
|
||||
else
|
||||
@ -65,10 +65,10 @@ module Homebrew
|
||||
info += tap.pinned? ? "pinned" : "unpinned"
|
||||
info += ", private" if tap.private?
|
||||
if (formula_count = tap.formula_files.size) > 0
|
||||
info += ", #{formula_count} formula#{plural(formula_count, "e")}"
|
||||
info += ", #{Formatter.pluralize(formula_count, "formula")}"
|
||||
end
|
||||
if (command_count = tap.command_files.size) > 0
|
||||
info += ", #{command_count} command#{plural(command_count)}"
|
||||
info += ", #{Formatter.pluralize(command_count, "command")}"
|
||||
end
|
||||
info += ", no formulae/commands" if (formula_count + command_count).zero?
|
||||
info += "\n#{tap.path} (#{tap.path.abv})"
|
||||
|
@ -65,7 +65,7 @@ module Homebrew
|
||||
puts "No apps unlinked from #{target_dir}" if ARGV.verbose?
|
||||
else
|
||||
n = ObserverPathnameExtension.total
|
||||
puts "Unlinked #{n} app#{plural(n)} from #{target_dir}"
|
||||
puts "Unlinked #{Formatter.pluralize(n, "app")} from #{target_dir}"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -80,7 +80,7 @@ module Homebrew
|
||||
|
||||
unless updated_taps.empty?
|
||||
update_preinstall_header
|
||||
puts "Updated #{updated_taps.size} tap#{plural(updated_taps.size)} " \
|
||||
puts "Updated #{Formatter.pluralize(updated_taps.size, "tap")} " \
|
||||
"(#{updated_taps.join(", ")})."
|
||||
updated = true
|
||||
end
|
||||
|
@ -66,12 +66,12 @@ module Homebrew
|
||||
if formulae_to_install.empty?
|
||||
oh1 "No packages to upgrade"
|
||||
else
|
||||
oh1 "Upgrading #{formulae_to_install.length} outdated package#{plural(formulae_to_install.length)}, with result:"
|
||||
oh1 "Upgrading #{Formatter.pluralize(formulae_to_install.length, "outdated package")}, with result:"
|
||||
puts formulae_to_install.map { |f| "#{f.full_specified_name} #{f.pkg_version}" } * ", "
|
||||
end
|
||||
|
||||
unless upgrade_pinned? || pinned.empty?
|
||||
oh1 "Not upgrading #{pinned.length} pinned package#{plural(pinned.length)}:"
|
||||
oh1 "Not upgrading #{Formatter.pluralize(pinned.length, "pinned package")}:"
|
||||
puts pinned.map { |f| "#{f.full_specified_name} #{f.pkg_version}" } * ", "
|
||||
end
|
||||
|
||||
|
@ -91,9 +91,7 @@ module Homebrew
|
||||
|
||||
return if problem_count.zero?
|
||||
|
||||
problems = "problem" + plural(problem_count)
|
||||
formulae = "formula" + plural(formula_count, "e")
|
||||
ofail "#{problem_count} #{problems} in #{formula_count} #{formulae}"
|
||||
ofail "#{Formatter.pluralize(problem_count, "problem")} in #{Formatter.pluralize(formula_count, "formula")}"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -239,7 +239,7 @@ class Tap
|
||||
link_completions_and_manpages
|
||||
|
||||
formula_count = formula_files.size
|
||||
puts "Tapped #{formula_count} formula#{plural(formula_count, "e")} (#{path.abv})" unless quiet
|
||||
puts "Tapped #{Formatter.pluralize(formula_count, "formula")} (#{path.abv})" unless quiet
|
||||
Descriptions.cache_formulae(formula_names)
|
||||
|
||||
return if options[:clone_target]
|
||||
@ -273,7 +273,7 @@ class Tap
|
||||
Utils::Link.unlink_completions(path)
|
||||
path.rmtree
|
||||
path.parent.rmdir_if_possible
|
||||
puts "Untapped #{formula_count} formula#{plural(formula_count, "e")}"
|
||||
puts "Untapped #{Formatter.pluralize(formula_count, "formula")}"
|
||||
clear_cache
|
||||
end
|
||||
|
||||
|
@ -131,12 +131,12 @@ def pretty_duration(s)
|
||||
if s > 59
|
||||
m = s / 60
|
||||
s %= 60
|
||||
res = "#{m} minute#{plural m}"
|
||||
res = Formatter.pluralize(m, "minute")
|
||||
return res if s.zero?
|
||||
res << " "
|
||||
end
|
||||
|
||||
res + "#{s} second#{plural s}"
|
||||
res << Formatter.pluralize(s, "second")
|
||||
end
|
||||
|
||||
def plural(n, s = "s")
|
||||
|
@ -90,4 +90,20 @@ module Formatter
|
||||
|
||||
output
|
||||
end
|
||||
|
||||
def pluralize(count, singular, plural = nil)
|
||||
return "#{count} #{singular}" if count == 1
|
||||
|
||||
*adjectives, noun = singular.split(" ")
|
||||
|
||||
if plural.nil?
|
||||
plural = {
|
||||
"formula" => "formulae",
|
||||
}.fetch(noun, "#{noun}s")
|
||||
end
|
||||
|
||||
words = adjectives << plural
|
||||
|
||||
"#{count} #{words.join(" ")}"
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user