diff options
| author | Markus Reiter | 2016-10-02 05:36:50 +0200 |
|---|---|---|
| committer | GitHub | 2016-10-02 05:36:50 +0200 |
| commit | 8b06a01e5b94fec9876a4fdfea1177496a0b7c93 (patch) | |
| tree | 90ed60f4960de7642276534e568419f7d61d7c86 /Library/Homebrew/utils | |
| parent | fa14c262454c2678ca9c8a1caa2f0080833ac67a (diff) | |
| parent | 1eab17235ca80c86850d1079ccb255b076d4b557 (diff) | |
| download | brew-8b06a01e5b94fec9876a4fdfea1177496a0b7c93.tar.bz2 | |
Merge pull request #823 from reitermarkus/refactoring-tty
Refactor Tty.
Diffstat (limited to 'Library/Homebrew/utils')
| -rw-r--r-- | Library/Homebrew/utils/formatter.rb | 52 | ||||
| -rw-r--r-- | Library/Homebrew/utils/github.rb | 10 | ||||
| -rw-r--r-- | Library/Homebrew/utils/tty.rb | 64 |
3 files changed, 121 insertions, 5 deletions
diff --git a/Library/Homebrew/utils/formatter.rb b/Library/Homebrew/utils/formatter.rb new file mode 100644 index 000000000..cb4f041f4 --- /dev/null +++ b/Library/Homebrew/utils/formatter.rb @@ -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 diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index b7ec538f9..5611f9aad 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -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.em}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.em}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.em}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 @@ -106,14 +106,14 @@ module GitHub onoe <<-EOS.undent Your macOS keychain GitHub credentials do not have sufficient scope! Scopes they have: #{credentials_scopes} - Create a personal access token: https://github.com/settings/tokens + Create a personal access token: #{Formatter.url("https://github.com/settings/tokens")} and then set HOMEBREW_GITHUB_API_TOKEN as the authentication method instead. EOS when :environment onoe <<-EOS.undent Your HOMEBREW_GITHUB_API_TOKEN does not have sufficient scope! Scopes it has: #{credentials_scopes} - Create a new personal access token: https://github.com/settings/tokens + Create a new personal access token: #{Formatter.url("https://github.com/settings/tokens")} and then set the new HOMEBREW_GITHUB_API_TOKEN as the authentication method instead. EOS end diff --git a/Library/Homebrew/utils/tty.rb b/Library/Homebrew/utils/tty.rb new file mode 100644 index 000000000..505165dc5 --- /dev/null +++ b/Library/Homebrew/utils/tty.rb @@ -0,0 +1,64 @@ +module Tty + module_function + + def strip_ansi(string) + string.gsub(/\033\[\d+(;\d+)*m/, "") + end + + def width + `/usr/bin/tput cols`.strip.to_i + end + + def truncate(string) + (w = width).zero? ? string.to_s : string.to_s[0, w - 4] + end + + COLOR_CODES = { + red: 31, + green: 32, + yellow: 33, + blue: 34, + magenta: 35, + cyan: 36, + default: 39, + }.freeze + + STYLE_CODES = { + reset: 0, + bold: 1, + italic: 3, + underline: 4, + strikethrough: 9, + no_underline: 24, + }.freeze + + CODES = COLOR_CODES.merge(STYLE_CODES).freeze + + def append_to_escape_sequence(code) + @escape_sequence ||= [] + @escape_sequence << code + self + end + + def current_escape_sequence + return "" if @escape_sequence.nil? + "\033[#{@escape_sequence.join(";")}m" + end + + def reset_escape_sequence! + @escape_sequence = nil + end + + CODES.each do |name, code| + define_singleton_method(name) do + append_to_escape_sequence(code) + end + end + + def to_s + return "" unless $stdout.tty? + current_escape_sequence + ensure + reset_escape_sequence! + end +end |
