Module: Tty Private

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

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Various helper functions for interacting with TTYs.

Constant Summary collapse

COLOR_CODES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  red:     31,
  green:   32,
  yellow:  33,
  blue:    34,
  magenta: 35,
  cyan:    36,
  default: 39,
}.freeze
STYLE_CODES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  reset:         0,
  bold:          1,
  italic:        3,
  underline:     4,
  strikethrough: 9,
  no_underline:  24,
}.freeze
CODES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

COLOR_CODES.merge(STYLE_CODES).freeze

Class Method Summary collapse

Class Method Details

.append_to_escape_sequence(code) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


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

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

.color?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

77
78
79
80
81
82
# File 'Library/Homebrew/utils/tty.rb', line 77

def color?
  return false if Homebrew::EnvConfig.no_color?
  return true if Homebrew::EnvConfig.color?

  $stdout.tty?
end

.current_escape_sequenceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


53
54
55
56
57
# File 'Library/Homebrew/utils/tty.rb', line 53

def current_escape_sequence
  return "" if @escape_sequence.nil?

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

.reset_escape_sequence!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


59
60
61
# File 'Library/Homebrew/utils/tty.rb', line 59

def reset_escape_sequence!
  @escape_sequence = nil
end

.strip_ansi(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


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

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

.to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


69
70
71
72
73
74
75
# File 'Library/Homebrew/utils/tty.rb', line 69

def to_s
  return "" unless color?

  current_escape_sequence
ensure
  reset_escape_sequence!
end

.truncate(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


22
23
24
# File 'Library/Homebrew/utils/tty.rb', line 22

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

.widthObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


13
14
15
16
17
18
19
20
# File 'Library/Homebrew/utils/tty.rb', line 13

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