Refactor Tty.
This commit is contained in:
		
							parent
							
								
									19e633f190
								
							
						
					
					
						commit
						6d8ee395fa
					
				@ -147,8 +147,8 @@ rescue Exception => e
 | 
			
		||||
  Utils::Analytics.report_exception(e)
 | 
			
		||||
  onoe e
 | 
			
		||||
  if internal_cmd && defined?(OS::ISSUES_URL)
 | 
			
		||||
    $stderr.puts "#{Tty.white}Please report this bug:"
 | 
			
		||||
    $stderr.puts "    #{Tty.em}#{OS::ISSUES_URL}#{Tty.reset}"
 | 
			
		||||
    $stderr.puts "Please report this bug:"
 | 
			
		||||
    $stderr.puts "    #{Tty.underline}#{OS::ISSUES_URL}#{Tty.reset}"
 | 
			
		||||
  end
 | 
			
		||||
  $stderr.puts e.backtrace
 | 
			
		||||
  exit 1
 | 
			
		||||
 | 
			
		||||
@ -81,11 +81,10 @@ module Hbc
 | 
			
		||||
        if target.exist?
 | 
			
		||||
          target_abv = " (#{target.abv})"
 | 
			
		||||
        else
 | 
			
		||||
          warning = "Missing #{self.class.artifact_english_name}"
 | 
			
		||||
          warning = "#{Tty.red}#{warning}#{Tty.reset}: "
 | 
			
		||||
          error = "#{Tty.red}Missing #{self.class.artifact_english_name}:#{Tty.reset} "
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        "#{warning}#{printable_target}#{target_abv}"
 | 
			
		||||
        "#{error}#{printable_target}#{target_abv}"
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@ -27,7 +27,7 @@ module Hbc
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def self.formatted_url(url)
 | 
			
		||||
        "#{Tty.em}#{url}#{Tty.reset}"
 | 
			
		||||
        "#{Tty.underline}#{url}#{Tty.reset}"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def self.installation_info(cask)
 | 
			
		||||
 | 
			
		||||
@ -30,7 +30,7 @@ module Hbc
 | 
			
		||||
          begin
 | 
			
		||||
            Homebrew.install_gem_setup_path! "rubocop-cask", RUBOCOP_CASK_VERSION, "rubocop"
 | 
			
		||||
          rescue SystemExit
 | 
			
		||||
            raise CaskError, $stderr.string.chomp.sub("#{Tty.red}Error#{Tty.reset}: ", "")
 | 
			
		||||
            raise CaskError, Tty.strip_ansi($stderr.string).chomp.sub(/\AError: /, "")
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
@ -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.white}Success!#{Tty.reset} "
 | 
			
		||||
            "#{Tty.blue}==>#{Tty.reset} #{Tty.bold}Success!#{Tty.reset} "
 | 
			
		||||
          end
 | 
			
		||||
      s << "#{@cask} was successfully installed!"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -32,11 +32,6 @@ end
 | 
			
		||||
def odebug(title, *sput)
 | 
			
		||||
  return unless Hbc.respond_to?(:debug)
 | 
			
		||||
  return unless Hbc.debug
 | 
			
		||||
 | 
			
		||||
  width = Tty.width * 4 - 6
 | 
			
		||||
  if $stdout.tty? && title.to_s.length > width
 | 
			
		||||
    title = title.to_s[0, width - 3] + "..."
 | 
			
		||||
  end
 | 
			
		||||
  puts "#{Tty.magenta}==>#{Tty.reset} #{Tty.white}#{title}#{Tty.reset}"
 | 
			
		||||
  puts sput unless sput.empty?
 | 
			
		||||
end
 | 
			
		||||
@ -152,11 +147,11 @@ module Hbc
 | 
			
		||||
      <<-EOS.undent
 | 
			
		||||
        Most likely, this means you have an outdated version of Homebrew-Cask. Please run:
 | 
			
		||||
 | 
			
		||||
            #{Tty.green}#{UPDATE_CMD}
 | 
			
		||||
          #{Tty.green}#{UPDATE_CMD}#{Tty.reset}
 | 
			
		||||
 | 
			
		||||
        #{Tty.reset}If this doesn’t fix the problem, please report this bug:
 | 
			
		||||
        If this doesn’t fix the problem, please report this bug:
 | 
			
		||||
 | 
			
		||||
            #{Tty.em}#{ISSUES_URL}#{Tty.reset}
 | 
			
		||||
          #{Tty.underline}#{ISSUES_URL}#{Tty.reset}
 | 
			
		||||
 | 
			
		||||
      EOS
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -42,7 +42,7 @@ module Homebrew
 | 
			
		||||
      next if out.nil? || out.empty?
 | 
			
		||||
      if first_warning
 | 
			
		||||
        $stderr.puts <<-EOS.undent
 | 
			
		||||
            #{Tty.white}Please note that these warnings are just used to help the Homebrew maintainers
 | 
			
		||||
            #{Tty.bold}Please note that these warnings are just used to help the Homebrew maintainers
 | 
			
		||||
            with debugging if you file an issue. If everything you use Homebrew for is
 | 
			
		||||
            working fine: please don't worry and just ignore them. Thanks!#{Tty.reset}
 | 
			
		||||
        EOS
 | 
			
		||||
 | 
			
		||||
@ -78,11 +78,11 @@ module Homebrew
 | 
			
		||||
      HOMEBREW_HELP
 | 
			
		||||
    else
 | 
			
		||||
      help_lines.map do |line|
 | 
			
		||||
        line.slice(2..-1)
 | 
			
		||||
            .sub(/^  \* /, "#{Tty.highlight}brew#{Tty.reset} ")
 | 
			
		||||
            .gsub(/`(.*?)`/, "#{Tty.highlight}\\1#{Tty.reset}")
 | 
			
		||||
            .gsub(/<(.*?)>/, "#{Tty.em}\\1#{Tty.reset}")
 | 
			
		||||
            .gsub("@hide_from_man_page", "")
 | 
			
		||||
        line.slice(2..-1).
 | 
			
		||||
          sub(/^  \* /, "#{Tty.bold}brew#{Tty.reset} ").
 | 
			
		||||
          gsub(/`(.*?)`/, "#{Tty.bold}\\1#{Tty.reset}").
 | 
			
		||||
          gsub(/<(.*?)>/, "#{Tty.underline}\\1#{Tty.reset}").
 | 
			
		||||
          gsub("@hide_from_man_page", "")
 | 
			
		||||
      end.join.strip
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@ -117,7 +117,7 @@ module Homebrew
 | 
			
		||||
 | 
			
		||||
    puts "#{f.full_name}: #{specs * ", "}#{" [#{attrs * ", "}]" unless attrs.empty?}"
 | 
			
		||||
    puts f.desc if f.desc
 | 
			
		||||
    puts "#{Tty.em}#{f.homepage}#{Tty.reset}" if f.homepage
 | 
			
		||||
    puts "#{Tty.underline}#{f.homepage}#{Tty.reset}" 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.em}#{github_info(f)}#{Tty.reset}"
 | 
			
		||||
    puts "From: #{Tty.underline}#{github_info(f)}#{Tty.reset}"
 | 
			
		||||
 | 
			
		||||
    unless f.deps.empty?
 | 
			
		||||
      ohai "Dependencies"
 | 
			
		||||
 | 
			
		||||
@ -282,7 +282,7 @@ module Homebrew
 | 
			
		||||
    EOS
 | 
			
		||||
  rescue => e
 | 
			
		||||
    ofail <<-EOS.undent
 | 
			
		||||
      #{Tty.white}Failed to migrate HOMEBREW_REPOSITORY to #{new_homebrew_repository}!
 | 
			
		||||
      #{Tty.bold}Failed to migrate HOMEBREW_REPOSITORY to #{new_homebrew_repository}!#{Tty.reset}
 | 
			
		||||
      The error was:
 | 
			
		||||
        #{e}
 | 
			
		||||
      Please try to resolve this error yourself and then run `brew update` again to
 | 
			
		||||
 | 
			
		||||
@ -111,7 +111,7 @@ module Debrew
 | 
			
		||||
 | 
			
		||||
    begin
 | 
			
		||||
      puts e.backtrace.first.to_s
 | 
			
		||||
      puts "#{Tty.red}#{e.class.name}#{Tty.reset}: #{e}"
 | 
			
		||||
      puts Formatter.error(e, label: e.class.name)
 | 
			
		||||
 | 
			
		||||
      loop do
 | 
			
		||||
        Menu.choose do |menu|
 | 
			
		||||
 | 
			
		||||
@ -124,7 +124,7 @@ class Descriptions
 | 
			
		||||
      short_name = short_names[full_name]
 | 
			
		||||
      printed_name = short_name_counts[short_name] == 1 ? short_name : full_name
 | 
			
		||||
      description = @descriptions[full_name] || blank
 | 
			
		||||
      puts "#{Tty.white}#{printed_name}:#{Tty.reset} #{description}"
 | 
			
		||||
      puts "#{Tty.bold}#{printed_name}:#{Tty.reset} #{description}"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -93,7 +93,7 @@ module Homebrew
 | 
			
		||||
      next unless ARGV.verbose? && !text_matches.empty?
 | 
			
		||||
      print_filename string, file
 | 
			
		||||
      text_matches.first(MAXIMUM_STRING_MATCHES).each do |match, offset|
 | 
			
		||||
        puts " #{Tty.gray}-->#{Tty.reset} match '#{match}' at offset #{Tty.em}0x#{offset}#{Tty.reset}"
 | 
			
		||||
        puts " #{Tty.gray}-->#{Tty.reset} match '#{match}' at offset #{Tty.bold}0x#{offset}#{Tty.reset}"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      if text_matches.size > MAXIMUM_STRING_MATCHES
 | 
			
		||||
 | 
			
		||||
@ -318,7 +318,7 @@ class BuildError < RuntimeError
 | 
			
		||||
  def dump
 | 
			
		||||
    if !ARGV.verbose?
 | 
			
		||||
      puts
 | 
			
		||||
      puts "#{Tty.red}READ THIS#{Tty.reset}: #{Tty.em}#{OS::ISSUES_URL}#{Tty.reset}"
 | 
			
		||||
      puts "#{Tty.red.underline}READ THIS#{Tty.reset.red}:#{Tty.reset} #{Tty.underline}#{OS::ISSUES_URL}#{Tty.reset}"
 | 
			
		||||
      if formula.tap
 | 
			
		||||
        case formula.tap.name
 | 
			
		||||
        when "homebrew/boneyard"
 | 
			
		||||
 | 
			
		||||
@ -153,7 +153,7 @@ class Migrator
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    begin
 | 
			
		||||
      oh1 "Migrating #{Tty.green}#{oldname}#{Tty.white} to #{Tty.green}#{newname}#{Tty.reset}"
 | 
			
		||||
      oh1 "Migrating #{Tty.green}#{oldname}#{Tty.reset} to #{Tty.green.bold}#{newname}#{Tty.reset}"
 | 
			
		||||
      lock
 | 
			
		||||
      unlink_oldname
 | 
			
		||||
      move_to_new_directory
 | 
			
		||||
 | 
			
		||||
@ -5,8 +5,11 @@ require "utils/shell"
 | 
			
		||||
 | 
			
		||||
class TtyTests < Homebrew::TestCase
 | 
			
		||||
  def test_strip_ansi
 | 
			
		||||
    assert_equal "hello",
 | 
			
		||||
      Tty.strip_ansi("\033\[36;7mhello\033\[0m")
 | 
			
		||||
    assert_equal "hello", Tty.strip_ansi("\033\[36;7mhello\033\[0m")
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_width
 | 
			
		||||
    assert_kind_of Integer, Tty.width
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_truncate
 | 
			
		||||
@ -21,15 +24,26 @@ class TtyTests < Homebrew::TestCase
 | 
			
		||||
 | 
			
		||||
  def test_no_tty_formatting
 | 
			
		||||
    $stdout.stubs(:tty?).returns false
 | 
			
		||||
    assert_nil Tty.blue
 | 
			
		||||
    assert_nil Tty.white
 | 
			
		||||
    assert_nil Tty.red
 | 
			
		||||
    assert_nil Tty.green
 | 
			
		||||
    assert_nil Tty.gray
 | 
			
		||||
    assert_nil Tty.yellow
 | 
			
		||||
    assert_nil Tty.reset
 | 
			
		||||
    assert_nil Tty.em
 | 
			
		||||
    assert_nil Tty.highlight
 | 
			
		||||
    assert_equal "", Tty.to_s
 | 
			
		||||
    assert_equal "", Tty.red.to_s
 | 
			
		||||
    assert_equal "", Tty.green.to_s
 | 
			
		||||
    assert_equal "", Tty.yellow.to_s
 | 
			
		||||
    assert_equal "", Tty.blue.to_s
 | 
			
		||||
    assert_equal "", Tty.magenta.to_s
 | 
			
		||||
    assert_equal "", Tty.cyan.to_s
 | 
			
		||||
    assert_equal "", Tty.default.to_s
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_formatting
 | 
			
		||||
    $stdout.stubs(:tty?).returns(true)
 | 
			
		||||
    assert_equal "",         Tty.to_s
 | 
			
		||||
    assert_equal "\033[31m", Tty.red.to_s
 | 
			
		||||
    assert_equal "\033[32m", Tty.green.to_s
 | 
			
		||||
    assert_equal "\033[33m", Tty.yellow.to_s
 | 
			
		||||
    assert_equal "\033[34m", Tty.blue.to_s
 | 
			
		||||
    assert_equal "\033[35m", Tty.magenta.to_s
 | 
			
		||||
    assert_equal "\033[36m", Tty.cyan.to_s
 | 
			
		||||
    assert_equal "\033[39m", Tty.default.to_s
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -10,85 +10,11 @@ require "utils/git"
 | 
			
		||||
require "utils/analytics"
 | 
			
		||||
require "utils/github"
 | 
			
		||||
require "utils/curl"
 | 
			
		||||
 | 
			
		||||
class Tty
 | 
			
		||||
  class << self
 | 
			
		||||
    def strip_ansi(string)
 | 
			
		||||
      string.gsub(/\033\[\d+(;\d+)*m/, "")
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def blue
 | 
			
		||||
      bold 34
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def white
 | 
			
		||||
      bold 39
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def magenta
 | 
			
		||||
      bold 35
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def red
 | 
			
		||||
      underline 31
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def yellow
 | 
			
		||||
      underline 33
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def reset
 | 
			
		||||
      escape 0
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def em
 | 
			
		||||
      underline 39
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def green
 | 
			
		||||
      bold 32
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def gray
 | 
			
		||||
      bold 30
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def highlight
 | 
			
		||||
      bold 39
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def width
 | 
			
		||||
      `/usr/bin/tput cols`.strip.to_i
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def truncate(str)
 | 
			
		||||
      w = width
 | 
			
		||||
      w > 10 ? str.to_s[0, w - 4] : str
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    private
 | 
			
		||||
 | 
			
		||||
    def color(n)
 | 
			
		||||
      escape "0;#{n}"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def bold(n)
 | 
			
		||||
      escape "1;#{n}"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def underline(n)
 | 
			
		||||
      escape "4;#{n}"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def escape(n)
 | 
			
		||||
      "\033[#{n}m" if $stdout.tty?
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
require "utils/tty"
 | 
			
		||||
 | 
			
		||||
def ohai(title, *sput)
 | 
			
		||||
  title = Tty.truncate(title) if $stdout.tty? && !ARGV.verbose?
 | 
			
		||||
  puts "#{Tty.blue}==>#{Tty.white} #{title}#{Tty.reset}"
 | 
			
		||||
  puts "#{Tty.blue}==>#{Tty.reset} #{Tty.bold}#{title}#{Tty.reset}"
 | 
			
		||||
  puts sput
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -96,16 +22,16 @@ def oh1(title, options = {})
 | 
			
		||||
  if $stdout.tty? && !ARGV.verbose? && options.fetch(:truncate, :auto) == :auto
 | 
			
		||||
    title = Tty.truncate(title)
 | 
			
		||||
  end
 | 
			
		||||
  puts "#{Tty.green}==>#{Tty.white} #{title}#{Tty.reset}"
 | 
			
		||||
  puts "#{Tty.green}==>#{Tty.reset} #{Tty.bold}#{title}#{Tty.reset}"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
# Print a warning (do this rarely)
 | 
			
		||||
def opoo(warning)
 | 
			
		||||
  $stderr.puts "#{Tty.yellow}Warning#{Tty.reset}: #{warning}"
 | 
			
		||||
  $stderr.puts "#{Tty.yellow.underline}Warnin#{Tty.reset.yellow}g:#{Tty.reset} #{warning}"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
def onoe(error)
 | 
			
		||||
  $stderr.puts "#{Tty.red}Error#{Tty.reset}: #{error}"
 | 
			
		||||
  $stderr.puts "#{Tty.red.underline}Error#{Tty.reset.red}:#{Tty.reset} #{error}"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
def ofail(error)
 | 
			
		||||
@ -171,9 +97,9 @@ def pretty_installed(f)
 | 
			
		||||
  if !$stdout.tty?
 | 
			
		||||
    f.to_s
 | 
			
		||||
  elsif Emoji.enabled?
 | 
			
		||||
    "#{Tty.highlight}#{f} #{Tty.green}#{Emoji.tick}#{Tty.reset}"
 | 
			
		||||
    "#{Tty.bold}#{f} #{Tty.green}#{Emoji.tick}#{Tty.reset}"
 | 
			
		||||
  else
 | 
			
		||||
    "#{Tty.highlight}#{Tty.green}#{f} (installed)#{Tty.reset}"
 | 
			
		||||
    "#{Tty.green.bold}#{f} (installed)#{Tty.reset}"
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -181,9 +107,9 @@ def pretty_uninstalled(f)
 | 
			
		||||
  if !$stdout.tty?
 | 
			
		||||
    f.to_s
 | 
			
		||||
  elsif Emoji.enabled?
 | 
			
		||||
    "#{f} #{Tty.red}#{Emoji.cross}#{Tty.reset}"
 | 
			
		||||
    "#{Tty.bold}#{f} #{Tty.red}#{Emoji.cross}#{Tty.reset}"
 | 
			
		||||
  else
 | 
			
		||||
    "#{Tty.red}#{f} (uninstalled)#{Tty.reset}"
 | 
			
		||||
    "#{Tty.red.bold}#{f} (uninstalled)#{Tty.reset}"
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										64
									
								
								Library/Homebrew/utils/tty.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								Library/Homebrew/utils/tty.rb
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user