aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/utils
diff options
context:
space:
mode:
authorMarkus Reiter2016-08-26 16:04:47 +0200
committerMarkus Reiter2016-10-01 20:00:49 +0200
commit6d8ee395fa5878282a1ce3975b632103448be042 (patch)
tree557520df749494a6a58ee54a139d4238ad41380d /Library/Homebrew/utils
parent19e633f1900ea910c2a9db85dc7c7bea0825676d (diff)
downloadbrew-6d8ee395fa5878282a1ce3975b632103448be042.tar.bz2
Refactor Tty.
Diffstat (limited to 'Library/Homebrew/utils')
-rw-r--r--Library/Homebrew/utils/github.rb6
-rw-r--r--Library/Homebrew/utils/tty.rb64
2 files changed, 67 insertions, 3 deletions
diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb
index b7ec538f9..e71039e12 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}
+ #{Tty.underline}https://github.com/settings/tokens/new?scopes=&description=Homebrew#{Tty.reset}
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}
+ #{Tty.underline}https://github.com/settings/tokens#{Tty.reset}
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}
+ #{Tty.underline}https://github.com/settings/tokens/new?scopes=&description=Homebrew#{Tty.reset}
and then set the token as: export HOMEBREW_GITHUB_API_TOKEN="your_new_token"
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