Module: Tty Private

Defined in:
utils/tty.rb,
sorbet/rbi/dsl/tty.rbi

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.

T.let(
  {
    red:     31,
    green:   32,
    yellow:  33,
    blue:    34,
    magenta: 35,
    cyan:    36,
    default: 39,
  }.freeze,
  T::Hash[Symbol, Integer],
)
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.

T.let(
  {
    reset:         0,
    bold:          1,
    italic:        3,
    underline:     4,
    strikethrough: 9,
    no_underline:  24,
  }.freeze,
  T::Hash[Symbol, Integer],
)
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.

T.let(
  {
    up:         "1A",
    down:       "1B",
    right:      "1C",
    left:       "1D",
    erase_line: "K",
    erase_char: "P",
  }.freeze,
  T::Hash[Symbol, String],
)
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.

T.let(COLOR_CODES.merge(STYLE_CODES).freeze, T::Hash[Symbol, Integer])

Class Method Summary collapse

Class Method Details

.begin_synchronized_updateString

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:



131
132
133
# File 'utils/tty.rb', line 131

def begin_synchronized_update
  "\033[?2026h"
end

.blueString

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:



11
# File 'sorbet/rbi/dsl/tty.rbi', line 11

def blue; end

.boldString

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:



14
# File 'sorbet/rbi/dsl/tty.rbi', line 14

def bold; end

.clear_to_endString

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:



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

def clear_to_end
  "\033[K"
end

.collapse_carriage_returns(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.

Simulates a terminal rendering \r overwrites (e.g. curl's --progress-bar).

Parameters:

Returns:



93
94
95
96
97
98
# File 'utils/tty.rb', line 93

def collapse_carriage_returns(string)
  string.split("\n", -1).map do |line|
    # `\r` resets the cursor, it doesn't erase, so keep the last non-empty segment.
    line.split("\r", -1).reject(&:empty?).last || ""
  end.join("\n")
end

.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)


212
213
214
215
216
217
218
219
220
# File 'utils/tty.rb', line 212

def color?
  require "env_config"

  return false if Homebrew::EnvConfig.no_color?
  return true if Homebrew::EnvConfig.color?
  return false if @stream.blank?

  @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:



172
173
174
175
176
# File 'utils/tty.rb', line 172

def current_escape_sequence
  return "" if @escape_sequence.nil?

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

.cyanString

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:



17
# File 'sorbet/rbi/dsl/tty.rbi', line 17

def cyan; end

.defaultString

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:



20
# File 'sorbet/rbi/dsl/tty.rbi', line 20

def default; end

.downString

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:



23
# File 'sorbet/rbi/dsl/tty.rbi', line 23

def down; end

.end_synchronized_updateString

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:



136
137
138
# File 'utils/tty.rb', line 136

def end_synchronized_update
  "\033[?2026l"
end

.erase_charString

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:



26
# File 'sorbet/rbi/dsl/tty.rbi', line 26

def erase_char; end

.erase_lineString

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:



29
# File 'sorbet/rbi/dsl/tty.rbi', line 29

def erase_line; end

.greenString

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:



32
# File 'sorbet/rbi/dsl/tty.rbi', line 32

def green; end

.heightInteger

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:



150
151
152
153
154
155
# File 'utils/tty.rb', line 150

def height
  @height ||= T.let(
    size&.first || Utils.popen_read_text("/usr/bin/tput", "lines", err: File::NULL).presence&.to_i || 40,
    T.nilable(Integer),
  )
end

.hide_cursorString

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:



121
122
123
# File 'utils/tty.rb', line 121

def hide_cursor
  "\033[?25l"
end

.italicString

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:



35
# File 'sorbet/rbi/dsl/tty.rbi', line 35

def italic; end

.leftString

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:



38
# File 'sorbet/rbi/dsl/tty.rbi', line 38

def left; end

.magentaString

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:



41
# File 'sorbet/rbi/dsl/tty.rbi', line 41

def magenta; end

.move_cursor_beginningString

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
# File 'utils/tty.rb', line 106

def move_cursor_beginning
  "\033[0G"
end

.move_cursor_down(line_count) ⇒ 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:



111
112
113
# File 'utils/tty.rb', line 111

def move_cursor_down(line_count)
  "\033[#{line_count}B"
end

.move_cursor_up_beginning(line_count) ⇒ 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:



101
102
103
# File 'utils/tty.rb', line 101

def move_cursor_up_beginning(line_count)
  "\033[#{line_count}F"
end

.no_underlineString

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:



44
# File 'sorbet/rbi/dsl/tty.rbi', line 44

def no_underline; end

.redString

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:



47
# File 'sorbet/rbi/dsl/tty.rbi', line 47

def red; end

.resetString

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:



50
# File 'sorbet/rbi/dsl/tty.rbi', line 50

def reset; 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.



179
180
181
# File 'utils/tty.rb', line 179

def reset_escape_sequence!
  @escape_sequence = T.let(nil, T.nilable(T::Array[Integer]))
end

.rightString

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:



53
# File 'sorbet/rbi/dsl/tty.rbi', line 53

def right; end

.show_cursorString

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:



126
127
128
# File 'utils/tty.rb', line 126

def show_cursor
  "\033[?25h"
end

.sizeArray<(Integer, Integer)>?

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:



141
142
143
144
145
146
147
# File 'utils/tty.rb', line 141

def size
  return @size if defined?(@size)

  height, width = Utils.popen_read_text("/bin/stty", "size", err: File::NULL).presence&.split&.map(&:to_i)
  size = [height, width] if height && width
  @size = T.let(size, T.nilable([Integer, Integer]))
end

.strikethroughString

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:



56
# File 'sorbet/rbi/dsl/tty.rbi', line 56

def strikethrough; 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:



83
84
85
86
87
88
89
# File 'utils/tty.rb', line 83

def strip_ansi(string)
  if string.ascii_only? || (string.encoding == Encoding::UTF_8 && string.valid_encoding?)
    string.gsub(TERMINAL_CONTROL_SEQUENCE, "")
  else
    string.b.gsub(BINARY_TERMINAL_CONTROL_SEQUENCE, "".b).force_encoding(string.encoding)
  end
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:



167
168
169
# File 'utils/tty.rb', line 167

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

.underlineString

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:



59
# File 'sorbet/rbi/dsl/tty.rbi', line 59

def underline; end

.upString

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:



62
# File 'sorbet/rbi/dsl/tty.rbi', line 62

def up; 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.

Keep in sync with columns in Library/Homebrew/utils/tty.sh.

Returns:



159
160
161
162
163
164
# File 'utils/tty.rb', line 159

def width
  @width ||= T.let(
    size&.second || Utils.popen_read_text("/usr/bin/tput", "cols", err: File::NULL).presence&.to_i || 80,
    T.nilable(Integer),
  )
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)


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

def with(stream, &_block)
  previous_stream = @stream
  @stream = T.let(stream, T.nilable(T.any(IO, StringIO)))

  yield stream
ensure
  @stream = T.let(previous_stream, T.nilable(T.any(IO, StringIO)))
end

.yellowString

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:



65
# File 'sorbet/rbi/dsl/tty.rbi', line 65

def yellow; end