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)
onoe e
if internal_cmd && defined?(OS::ISSUES_URL)
$stderr.puts "Please report this bug:"
$stderr.puts " #{Tty.underline}#{OS::ISSUES_URL}#{Tty.reset}"
$stderr.puts "#{Tty.bold}Please report this bug:#{Tty.reset}"
$stderr.puts " #{Formatter.url(OS::ISSUES_URL)}"
end
$stderr.puts e.backtrace
exit 1

View File

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

View File

@ -57,12 +57,17 @@ module Hbc
def summarize_artifact(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?
target_readlink_abv = " (#{target.readlink.abv})" if target.readlink.exist?
"#{link_description}#{printable_target} -> #{target.readlink}#{target_readlink_abv}"
Formatter.error(string, label: "Broken Link")
end
end
end
end

View File

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

View File

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

View File

@ -18,9 +18,9 @@ module Hbc
def self.info(cask)
puts "#{cask.token}: #{cask.version}"
puts formatted_url(cask.homepage) if cask.homepage
puts Formatter.url(cask.homepage) if cask.homepage
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)
artifact_info(cask)
Installer.print_caveats(cask)
@ -37,7 +37,7 @@ module Hbc
puts versioned_staged_path.to_s
.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(")")
end
else
@ -47,7 +47,7 @@ module Hbc
def self.name_info(cask)
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
def self.github_info(cask)

View File

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

View File

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

View File

@ -117,7 +117,7 @@ module Homebrew
puts "#{f.full_name}: #{specs * ", "}#{" [#{attrs * ", "}]" unless attrs.empty?}"
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!
puts "Conflicts with: #{conflicts*", "}" unless conflicts.empty?
@ -133,7 +133,7 @@ module Homebrew
end
end
puts "From: #{Tty.underline}#{github_info(f)}#{Tty.reset}"
puts "From: #{Formatter.url(github_info(f))}"
unless f.deps.empty?
ohai "Dependencies"

View File

@ -119,7 +119,7 @@ class Descriptions
# Take search results -- a hash mapping formula names to descriptions -- and
# print them.
def print
blank = "#{Tty.yellow}[no description]#{Tty.reset}"
blank = Formatter.warning("[no description]")
@descriptions.keys.sort.each do |full_name|
short_name = short_names[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
puts "#{Tty.red}#{filename}#{Tty.reset}"
puts Formatter.error(filename.to_s)
@put_filenames << filename
end

View File

@ -318,7 +318,7 @@ class BuildError < RuntimeError
def dump
if !ARGV.verbose?
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
case formula.tap.name
when "homebrew/boneyard"

View File

@ -214,7 +214,7 @@ class FormulaInstaller
opoo "#{formula.full_name}: #{old_flag} was deprecated; using #{new_flag} instead!"
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?
options = []
@ -426,7 +426,7 @@ class FormulaInstaller
if deps.empty? && only_deps?
puts "All dependencies for #{formula.full_name} are satisfied."
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
deps.each { |dep, options| install_dependency(dep, options) }
end
@ -476,7 +476,7 @@ class FormulaInstaller
fi.verbose = verbose? && !quieter?
fi.debug = debug?
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.finish
rescue Exception

View File

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

View File

@ -1,6 +1,7 @@
require "pathname"
require "emoji"
require "exceptions"
require "utils/formatter"
require "utils/hash"
require "utils/json"
require "utils/inreplace"
@ -14,7 +15,7 @@ require "utils/tty"
def ohai(title, *sput)
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
end
@ -22,16 +23,16 @@ def oh1(title, options = {})
if $stdout.tty? && !ARGV.verbose? && options.fetch(:truncate, :auto) == :auto
title = Tty.truncate(title)
end
puts "#{Tty.green}==>#{Tty.reset} #{Tty.bold}#{title}#{Tty.reset}"
puts Formatter.headline(title, color: :green)
end
# Print a warning (do this rarely)
def opoo(warning)
$stderr.puts "#{Tty.yellow.underline}Warnin#{Tty.reset.yellow}g:#{Tty.reset} #{warning}"
def opoo(message)
$stderr.puts Formatter.warning(message, label: "Warning")
end
def onoe(error)
$stderr.puts "#{Tty.red.underline}Error#{Tty.reset.red}:#{Tty.reset} #{error}"
def onoe(message)
$stderr.puts Formatter.error(message, label: "Error")
end
def ofail(error)
@ -97,9 +98,9 @@ def pretty_installed(f)
if !$stdout.tty?
f.to_s
elsif Emoji.enabled?
"#{Tty.bold}#{f} #{Tty.green}#{Emoji.tick}#{Tty.reset}"
"#{Tty.bold}#{f} #{Formatter.success(Emoji.tick)}#{Tty.reset}"
else
"#{Tty.green.bold}#{f} (installed)#{Tty.reset}"
Formatter.success("#{Tty.bold}#{f} (installed)#{Tty.reset}")
end
end
@ -107,9 +108,9 @@ def pretty_uninstalled(f)
if !$stdout.tty?
f.to_s
elsif Emoji.enabled?
"#{Tty.bold}#{f} #{Tty.red}#{Emoji.cross}#{Tty.reset}"
"#{Tty.bold}#{f} #{Formatter.error(Emoji.cross)}#{Tty.reset}"
else
"#{Tty.red.bold}#{f} (uninstalled)#{Tty.reset}"
Formatter.error("#{Tty.bold}#{f} (uninstalled)#{Tty.reset}")
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
GitHub API Error: #{error}
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"
EOS
end
@ -30,7 +30,7 @@ module GitHub
if ENV["HOMEBREW_GITHUB_API_TOKEN"]
message << <<-EOS.undent
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
else
message << <<-EOS.undent
@ -38,7 +38,7 @@ module GitHub
Clear them with:
printf "protocol=https\\nhost=github.com\\n" | git credential-osxkeychain erase
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"
EOS
end