Module: Tty Private

Extended by:
T::Sig
Defined in:
utils/tty.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Various helper functions for interacting with TTYs.

Constant Summary collapse

COLOR_CODES =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

{
  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. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

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

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

{
  up:         "1A",
  down:       "1B",
  right:      "1C",
  left:       "1D",
  erase_line: "K",
  erase_char: "P",
}.freeze
CODES =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

COLOR_CODES.merge(STYLE_CODES).freeze

Class Method Summary collapse

Class Method Details

.color?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


115
116
117
118
119
120
# File 'utils/tty.rb', line 115

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

  @stream.tty?
end

.current_escape_sequenceString

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



76
77
78
79
80
# File 'utils/tty.rb', line 76

def current_escape_sequence
  return "" if @escape_sequence.nil?

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

.reset_escape_sequence!void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



83
84
85
# File 'utils/tty.rb', line 83

def reset_escape_sequence!
  @escape_sequence = nil
end

.strip_ansi(string) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



26
27
28
# File 'utils/tty.rb', line 26

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

.to_sString

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



106
107
108
109
110
111
112
# File 'utils/tty.rb', line 106

def to_s
  return "" unless color?

  current_escape_sequence
ensure
  reset_escape_sequence!
end

.truncate(string) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



41
42
43
# File 'utils/tty.rb', line 41

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

.widthInteger

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Integer)


31
32
33
34
35
36
37
38
# File 'utils/tty.rb', line 31

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

.with(stream, &_block) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • stream (IO, StringIO)
  • _block (T.proc.params(arg0: T.any(IO, StringIO)).void)


16
17
18
19
20
21
22
23
# File 'utils/tty.rb', line 16

def with(stream, &_block)
  previous_stream = @stream
  @stream = stream

  yield stream
ensure
  @stream = previous_stream
end