Module: Tty

Defined in:
Library/Homebrew/utils/tty.rb

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.append_to_escape_sequence(code) ⇒ Object



40
41
42
43
44
# File 'Library/Homebrew/utils/tty.rb', line 40

def append_to_escape_sequence(code)
  @escape_sequence ||= []
  @escape_sequence << code
  self
end

.current_escape_sequenceObject



46
47
48
49
# File 'Library/Homebrew/utils/tty.rb', line 46

def current_escape_sequence
  return "" if @escape_sequence.nil?
  "\033[#{@escape_sequence.join(";")}m"
end

.reset_escape_sequence!Object



51
52
53
# File 'Library/Homebrew/utils/tty.rb', line 51

def reset_escape_sequence!
  @escape_sequence = nil
end

.strip_ansi(string) ⇒ Object



4
5
6
# File 'Library/Homebrew/utils/tty.rb', line 4

def strip_ansi(string)
  string.gsub(/\033\[\d+(;\d+)*m/, "")
end

.to_sObject



61
62
63
64
65
66
67
68
# File 'Library/Homebrew/utils/tty.rb', line 61

def to_s
  if ENV["HOMEBREW_NO_COLOR"] || !$stdout.tty?
    return ""
  end
  current_escape_sequence
ensure
  reset_escape_sequence!
end

.truncate(string) ⇒ Object



15
16
17
# File 'Library/Homebrew/utils/tty.rb', line 15

def truncate(string)
  (w = width).zero? ? string.to_s : string.to_s[0, w - 4]
end

.widthObject



8
9
10
11
12
13
# File 'Library/Homebrew/utils/tty.rb', line 8

def width
  width = `/bin/stty size 2>/dev/null`.split[1]
  width ||= `/usr/bin/tput cols 2>/dev/null`.split[0]
  width ||= 80
  width.to_i
end