Add Formatter module.

This commit is contained in:
Markus Reiter 2016-08-30 21:38:13 +02:00
parent 6d8ee395fa
commit 75e8b59aad
18 changed files with 109 additions and 55 deletions

View File

@ -147,8 +147,8 @@ rescue Exception => e
Utils::Analytics.report_exception(e) Utils::Analytics.report_exception(e)
onoe e onoe e
if internal_cmd && defined?(OS::ISSUES_URL) if internal_cmd && defined?(OS::ISSUES_URL)
$stderr.puts "Please report this bug:" $stderr.puts "#{Tty.bold}Please report this bug:#{Tty.reset}"
$stderr.puts " #{Tty.underline}#{OS::ISSUES_URL}#{Tty.reset}" $stderr.puts " #{Formatter.url(OS::ISSUES_URL)}"
end end
$stderr.puts e.backtrace $stderr.puts e.backtrace
exit 1 exit 1

View File

@ -79,12 +79,10 @@ module Hbc
load_specification artifact_spec load_specification artifact_spec
if target.exist? if target.exist?
target_abv = " (#{target.abv})" "#{printable_target} (#{target.abv})"
else else
error = "#{Tty.red}Missing #{self.class.artifact_english_name}:#{Tty.reset} " Formatter.error(printable_target, label: "Missing #{self.class.artifact_english_name}")
end end
"#{error}#{printable_target}#{target_abv}"
end end
end end
end end

View File

@ -57,12 +57,17 @@ module Hbc
def summarize_artifact(artifact_spec) def summarize_artifact(artifact_spec)
load_specification artifact_spec load_specification artifact_spec
return unless self.class.islink?(target) if self.class.islink?(target) && target.exist? && target.readlink.exist?
"#{printable_target} -> #{target.readlink} (#{target.readlink.abv})"
else
string = if self.class.islink?(target)
"#{printable_target} -> #{target.readlink}"
else
printable_target
end
link_description = "#{Tty.red}Broken Link#{Tty.reset}: " unless target.exist? Formatter.error(string, label: "Broken Link")
target_readlink_abv = " (#{target.readlink.abv})" if target.readlink.exist? end
"#{link_description}#{printable_target} -> #{target.readlink}#{target_readlink_abv}"
end end
end end
end end

View File

@ -28,11 +28,11 @@ module Hbc
def result def result
if errors? if errors?
"#{Tty.red}failed#{Tty.reset}" Formatter.error("failed")
elsif warnings? elsif warnings?
"#{Tty.yellow}warning#{Tty.reset}" Formatter.warning("warning")
else else
"#{Tty.green}passed#{Tty.reset}" Formatter.success("passed")
end end
end end
@ -40,11 +40,11 @@ module Hbc
summary = ["#{summary_header}: #{result}"] summary = ["#{summary_header}: #{result}"]
errors.each do |error| errors.each do |error|
summary << " #{Tty.red}-#{Tty.reset} #{error}" summary << " #{Formatter.error("-")} #{error}"
end end
warnings.each do |warning| warnings.each do |warning|
summary << " #{Tty.yellow}-#{Tty.reset} #{warning}" summary << " #{Formatter.warning("-")} #{warning}"
end end
summary.join("\n") summary.join("\n")

View File

@ -122,11 +122,11 @@ module Hbc
end end
def self.notfound_string def self.notfound_string
"#{Tty.red}Not Found - Unknown Error#{Tty.reset}" Formatter.error("Not Found - Unknown Error")
end end
def self.error_string(string = "Error") def self.error_string(string = "Error")
"#{Tty.red}(#{string})#{Tty.reset}" Formatter.error("(#{string})")
end end
def self.render_with_none(string) def self.render_with_none(string)

View File

@ -18,9 +18,9 @@ module Hbc
def self.info(cask) def self.info(cask)
puts "#{cask.token}: #{cask.version}" puts "#{cask.token}: #{cask.version}"
puts formatted_url(cask.homepage) if cask.homepage puts Formatter.url(cask.homepage) if cask.homepage
installation_info(cask) installation_info(cask)
puts "From: #{formatted_url(github_info(cask))}" if github_info(cask) puts "From: #{Formatter.url(github_info(cask))}" if github_info(cask)
name_info(cask) name_info(cask)
artifact_info(cask) artifact_info(cask)
Installer.print_caveats(cask) Installer.print_caveats(cask)
@ -37,7 +37,7 @@ module Hbc
puts versioned_staged_path.to_s puts versioned_staged_path.to_s
.concat(" (") .concat(" (")
.concat(versioned_staged_path.exist? ? versioned_staged_path.abv : "#{Tty.red}does not exist#{Tty.reset}") .concat(versioned_staged_path.exist? ? versioned_staged_path.abv : Formatter.error("does not exist"))
.concat(")") .concat(")")
end end
else else
@ -47,7 +47,7 @@ module Hbc
def self.name_info(cask) def self.name_info(cask)
ohai cask.name.size > 1 ? "Names" : "Name" ohai cask.name.size > 1 ? "Names" : "Name"
puts cask.name.empty? ? "#{Tty.red}None#{Tty.reset}" : cask.name puts cask.name.empty? ? Formatter.error("None") : cask.name
end end
def self.github_info(cask) def self.github_info(cask)

View File

@ -87,7 +87,7 @@ module Hbc
s = if MacOS.version >= :lion && !ENV["HOMEBREW_NO_EMOJI"] s = if MacOS.version >= :lion && !ENV["HOMEBREW_NO_EMOJI"]
(ENV["HOMEBREW_INSTALL_BADGE"] || "\xf0\x9f\x8d\xba") + " " (ENV["HOMEBREW_INSTALL_BADGE"] || "\xf0\x9f\x8d\xba") + " "
else else
"#{Tty.blue}==>#{Tty.reset} #{Tty.bold}Success!#{Tty.reset} " Formatter.headline("Success! ", color: :blue)
end end
s << "#{@cask} was successfully installed!" s << "#{@cask} was successfully installed!"
end end

View File

@ -32,7 +32,7 @@ end
def odebug(title, *sput) def odebug(title, *sput)
return unless Hbc.respond_to?(:debug) return unless Hbc.respond_to?(:debug)
return unless Hbc.debug return unless Hbc.debug
puts "#{Tty.magenta}==>#{Tty.reset} #{Tty.white}#{title}#{Tty.reset}" puts Formatter.headline(title, color: :magenta)
puts sput unless sput.empty? puts sput unless sput.empty?
end end
@ -146,12 +146,10 @@ module Hbc
def self.error_message_with_suggestions def self.error_message_with_suggestions
<<-EOS.undent <<-EOS.undent
Most likely, this means you have an outdated version of Homebrew-Cask. Please run: Most likely, this means you have an outdated version of Homebrew-Cask. Please run:
#{UPDATE_CMD}
#{Tty.green}#{UPDATE_CMD}#{Tty.reset}
If this doesnt fix the problem, please report this bug: If this doesnt fix the problem, please report this bug:
#{Formatter.url(ISSUES_URL)}
#{Tty.underline}#{ISSUES_URL}#{Tty.reset}
EOS EOS
end end

View File

@ -117,7 +117,7 @@ module Homebrew
puts "#{f.full_name}: #{specs * ", "}#{" [#{attrs * ", "}]" unless attrs.empty?}" puts "#{f.full_name}: #{specs * ", "}#{" [#{attrs * ", "}]" unless attrs.empty?}"
puts f.desc if f.desc puts f.desc if f.desc
puts "#{Tty.underline}#{f.homepage}#{Tty.reset}" if f.homepage puts Formatter.url(f.homepage) if f.homepage
conflicts = f.conflicts.map(&:name).sort! conflicts = f.conflicts.map(&:name).sort!
puts "Conflicts with: #{conflicts*", "}" unless conflicts.empty? puts "Conflicts with: #{conflicts*", "}" unless conflicts.empty?
@ -133,7 +133,7 @@ module Homebrew
end end
end end
puts "From: #{Tty.underline}#{github_info(f)}#{Tty.reset}" puts "From: #{Formatter.url(github_info(f))}"
unless f.deps.empty? unless f.deps.empty?
ohai "Dependencies" ohai "Dependencies"

View File

@ -119,7 +119,7 @@ class Descriptions
# Take search results -- a hash mapping formula names to descriptions -- and # Take search results -- a hash mapping formula names to descriptions -- and
# print them. # print them.
def print def print
blank = "#{Tty.yellow}[no description]#{Tty.reset}" blank = Formatter.warning("[no description]")
@descriptions.keys.sort.each do |full_name| @descriptions.keys.sort.each do |full_name|
short_name = short_names[full_name] short_name = short_names[full_name]
printed_name = short_name_counts[short_name] == 1 ? short_name : full_name printed_name = short_name_counts[short_name] == 1 ? short_name : full_name

View File

@ -54,7 +54,7 @@ module Homebrew
return if @put_filenames.include? filename return if @put_filenames.include? filename
puts "#{Tty.red}#{filename}#{Tty.reset}" puts Formatter.error(filename.to_s)
@put_filenames << filename @put_filenames << filename
end end

View File

@ -318,7 +318,7 @@ class BuildError < RuntimeError
def dump def dump
if !ARGV.verbose? if !ARGV.verbose?
puts puts
puts "#{Tty.red.underline}READ THIS#{Tty.reset.red}:#{Tty.reset} #{Tty.underline}#{OS::ISSUES_URL}#{Tty.reset}" puts Formatter.error("READ THIS: #{Formatter.url(OS::ISSUES_URL)}")
if formula.tap if formula.tap
case formula.tap.name case formula.tap.name
when "homebrew/boneyard" when "homebrew/boneyard"

View File

@ -214,7 +214,7 @@ class FormulaInstaller
opoo "#{formula.full_name}: #{old_flag} was deprecated; using #{new_flag} instead!" opoo "#{formula.full_name}: #{old_flag} was deprecated; using #{new_flag} instead!"
end end
oh1 "Installing #{Tty.green}#{formula.full_name}#{Tty.reset}" if show_header? oh1 "Installing #{Formatter.identifier(formula.full_name)}" if show_header?
if formula.tap && !formula.tap.private? if formula.tap && !formula.tap.private?
options = [] options = []
@ -426,7 +426,7 @@ class FormulaInstaller
if deps.empty? && only_deps? if deps.empty? && only_deps?
puts "All dependencies for #{formula.full_name} are satisfied." puts "All dependencies for #{formula.full_name} are satisfied."
elsif !deps.empty? elsif !deps.empty?
oh1 "Installing dependencies for #{formula.full_name}: #{Tty.green}#{deps.map(&:first)*", "}#{Tty.reset}", oh1 "Installing dependencies for #{formula.full_name}: #{deps.map(&:first).map(&Formatter.method(:identifier)).join(", ")}",
truncate: false truncate: false
deps.each { |dep, options| install_dependency(dep, options) } deps.each { |dep, options| install_dependency(dep, options) }
end end
@ -476,7 +476,7 @@ class FormulaInstaller
fi.verbose = verbose? && !quieter? fi.verbose = verbose? && !quieter?
fi.debug = debug? fi.debug = debug?
fi.prelude fi.prelude
oh1 "Installing #{formula.full_name} dependency: #{Tty.green}#{dep.name}#{Tty.reset}" oh1 "Installing #{formula.full_name} dependency: #{Formatter.identifier(dep.name)}"
fi.install fi.install
fi.finish fi.finish
rescue Exception rescue Exception

View File

@ -153,7 +153,7 @@ class Migrator
end end
begin begin
oh1 "Migrating #{Tty.green}#{oldname}#{Tty.reset} to #{Tty.green.bold}#{newname}#{Tty.reset}" oh1 "Migrating #{Formatter.identifier(oldname)} to #{Formatter.identifier(newname)}"
lock lock
unlink_oldname unlink_oldname
move_to_new_directory move_to_new_directory
@ -200,7 +200,7 @@ class Migrator
end end
def unlink_oldname def unlink_oldname
oh1 "Unlinking #{Tty.green}#{oldname}#{Tty.reset}" oh1 "Unlinking #{Formatter.identifier(oldname)}"
old_cellar.subdirs.each do |d| old_cellar.subdirs.each do |d|
keg = Keg.new(d) keg = Keg.new(d)
keg.unlink keg.unlink
@ -208,7 +208,7 @@ class Migrator
end end
def link_newname def link_newname
oh1 "Linking #{Tty.green}#{newname}#{Tty.reset}" oh1 "Linking #{Formatter.identifier(newname)}"
new_keg = Keg.new(new_linked_keg_record) new_keg = Keg.new(new_linked_keg_record)
# If old_keg wasn't linked then we just optlink a keg. # If old_keg wasn't linked then we just optlink a keg.

View File

@ -1,6 +1,7 @@
require "pathname" require "pathname"
require "emoji" require "emoji"
require "exceptions" require "exceptions"
require "utils/formatter"
require "utils/hash" require "utils/hash"
require "utils/json" require "utils/json"
require "utils/inreplace" require "utils/inreplace"
@ -14,7 +15,7 @@ require "utils/tty"
def ohai(title, *sput) def ohai(title, *sput)
title = Tty.truncate(title) if $stdout.tty? && !ARGV.verbose? title = Tty.truncate(title) if $stdout.tty? && !ARGV.verbose?
puts "#{Tty.blue}==>#{Tty.reset} #{Tty.bold}#{title}#{Tty.reset}" puts Formatter.headline(title, color: :blue)
puts sput puts sput
end end
@ -22,16 +23,16 @@ def oh1(title, options = {})
if $stdout.tty? && !ARGV.verbose? && options.fetch(:truncate, :auto) == :auto if $stdout.tty? && !ARGV.verbose? && options.fetch(:truncate, :auto) == :auto
title = Tty.truncate(title) title = Tty.truncate(title)
end end
puts "#{Tty.green}==>#{Tty.reset} #{Tty.bold}#{title}#{Tty.reset}" puts Formatter.headline(title, color: :green)
end end
# Print a warning (do this rarely) # Print a warning (do this rarely)
def opoo(warning) def opoo(message)
$stderr.puts "#{Tty.yellow.underline}Warnin#{Tty.reset.yellow}g:#{Tty.reset} #{warning}" $stderr.puts Formatter.warning(message, label: "Warning")
end end
def onoe(error) def onoe(message)
$stderr.puts "#{Tty.red.underline}Error#{Tty.reset.red}:#{Tty.reset} #{error}" $stderr.puts Formatter.error(message, label: "Error")
end end
def ofail(error) def ofail(error)
@ -97,9 +98,9 @@ def pretty_installed(f)
if !$stdout.tty? if !$stdout.tty?
f.to_s f.to_s
elsif Emoji.enabled? elsif Emoji.enabled?
"#{Tty.bold}#{f} #{Tty.green}#{Emoji.tick}#{Tty.reset}" "#{Tty.bold}#{f} #{Formatter.success(Emoji.tick)}#{Tty.reset}"
else else
"#{Tty.green.bold}#{f} (installed)#{Tty.reset}" Formatter.success("#{Tty.bold}#{f} (installed)#{Tty.reset}")
end end
end end
@ -107,9 +108,9 @@ def pretty_uninstalled(f)
if !$stdout.tty? if !$stdout.tty?
f.to_s f.to_s
elsif Emoji.enabled? elsif Emoji.enabled?
"#{Tty.bold}#{f} #{Tty.red}#{Emoji.cross}#{Tty.reset}" "#{Tty.bold}#{f} #{Formatter.error(Emoji.cross)}#{Tty.reset}"
else else
"#{Tty.red.bold}#{f} (uninstalled)#{Tty.reset}" Formatter.error("#{Tty.bold}#{f} (uninstalled)#{Tty.reset}")
end end
end end

View File

@ -0,0 +1,52 @@
require "utils/tty"
module Formatter
module_function
def arrow(string, color: nil)
prefix("==>", string, color)
end
def headline(string, color: nil)
arrow("#{Tty.bold}#{string}#{Tty.reset}", color: color)
end
def identifier(string)
"#{Tty.green}#{string}#{Tty.reset}"
end
def success(string, label: nil)
label(label, string, :green)
end
def warning(string, label: nil)
label(label, string, :yellow)
end
def error(string, label: nil)
label(label, string, :red)
end
def url(string)
"#{Tty.underline}#{string}#{Tty.no_underline}"
end
def label(label, string, color)
label = "#{label}:" unless label.nil?
prefix(label, string, color)
end
private_class_method :label
def prefix(prefix, string, color)
if prefix.nil? && color.nil?
string
elsif prefix.nil?
"#{Tty.send(color)}#{string}#{Tty.reset}"
elsif color.nil?
"#{prefix} #{string}"
else
"#{Tty.send(color)}#{prefix}#{Tty.reset} #{string}"
end
end
private_class_method :prefix
end

View File

@ -14,7 +14,7 @@ module GitHub
super <<-EOS.undent super <<-EOS.undent
GitHub API Error: #{error} GitHub API Error: #{error}
Try again in #{pretty_ratelimit_reset(reset)}, or create a personal access token: Try again in #{pretty_ratelimit_reset(reset)}, or create a personal access token:
#{Tty.underline}https://github.com/settings/tokens/new?scopes=&description=Homebrew#{Tty.reset} #{Formatter.url("https://github.com/settings/tokens/new?scopes=&description=Homebrew")}
and then set the token as: export HOMEBREW_GITHUB_API_TOKEN="your_new_token" and then set the token as: export HOMEBREW_GITHUB_API_TOKEN="your_new_token"
EOS EOS
end end
@ -30,7 +30,7 @@ module GitHub
if ENV["HOMEBREW_GITHUB_API_TOKEN"] if ENV["HOMEBREW_GITHUB_API_TOKEN"]
message << <<-EOS.undent message << <<-EOS.undent
HOMEBREW_GITHUB_API_TOKEN may be invalid or expired; check: HOMEBREW_GITHUB_API_TOKEN may be invalid or expired; check:
#{Tty.underline}https://github.com/settings/tokens#{Tty.reset} #{Formatter.url("https://github.com/settings/tokens")}
EOS EOS
else else
message << <<-EOS.undent message << <<-EOS.undent
@ -38,7 +38,7 @@ module GitHub
Clear them with: Clear them with:
printf "protocol=https\\nhost=github.com\\n" | git credential-osxkeychain erase printf "protocol=https\\nhost=github.com\\n" | git credential-osxkeychain erase
Or create a personal access token: Or create a personal access token:
#{Tty.underline}https://github.com/settings/tokens/new?scopes=&description=Homebrew#{Tty.reset} #{Formatter.url("https://github.com/settings/tokens/new?scopes=&description=Homebrew")}
and then set the token as: export HOMEBREW_GITHUB_API_TOKEN="your_new_token" and then set the token as: export HOMEBREW_GITHUB_API_TOKEN="your_new_token"
EOS EOS
end end