Module: Tty

Defined in:
brew/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



44
45
46
47
48
# File 'brew/Library/Homebrew/utils/tty.rb', line 44

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

.current_escape_sequenceObject



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

def current_escape_sequence
  return "" if @escape_sequence.nil?

  "\033[#{@escape_sequence.join(";")}m"
end

.reset_escape_sequence!Object



56
57
58
# File 'brew/Library/Homebrew/utils/tty.rb', line 56

def reset_escape_sequence!
  @escape_sequence = nil
end

.strip_ansi(string) ⇒ Object



6
7
8
# File 'brew/Library/Homebrew/utils/tty.rb', line 6

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

.to_sObject



66
67
68
69
70
71
72
# File 'brew/Library/Homebrew/utils/tty.rb', line 66

def to_s
  return "" if !ENV["HOMEBREW_COLOR"] && (ENV["HOMEBREW_NO_COLOR"] || !$stdout.tty?)

  current_escape_sequence
ensure
  reset_escape_sequence!
end

.truncate(string) ⇒ Object



19
20
21
# File 'brew/Library/Homebrew/utils/tty.rb', line 19

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

.widthObject



10
11
12
13
14
15
16
17
# File 'brew/Library/Homebrew/utils/tty.rb', line 10

def width
  @width ||= begin
    _, width = `/bin/stty size 2>/dev/null`.split
    width, = `/usr/bin/tput cols 2>/dev/null`.split if width.to_i.zero?
    width ||= 80
    width.to_i
  end
end