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
- .begin_synchronized_update ⇒ String private
- .blue ⇒ String private
- .bold ⇒ String private
- .clear_to_end ⇒ String private
- .clear_to_end_of_screen ⇒ String private
-
.collapse_carriage_returns(string) ⇒ String
private
Simulates a terminal rendering
\roverwrites (e.g. curl's--progress-bar). - .color? ⇒ Boolean private
- .current_escape_sequence ⇒ String private
- .cyan ⇒ String private
- .default ⇒ String private
-
.disable_line_wrap ⇒ String
private
Save auto-wrap mode and disable it so echoed input cannot wrap a progress row.
- .down ⇒ String private
- .end_synchronized_update ⇒ String private
- .erase_char ⇒ String private
- .erase_line ⇒ String private
- .green ⇒ String private
- .height ⇒ Integer private
- .hide_cursor ⇒ String private
- .italic ⇒ String private
- .left ⇒ String private
- .magenta ⇒ String private
- .move_cursor_beginning ⇒ String private
- .move_cursor_down(line_count) ⇒ String private
- .move_cursor_up_beginning(line_count) ⇒ String private
- .no_underline ⇒ String private
- .red ⇒ String private
- .reset ⇒ String private
- .reset_escape_sequence! ⇒ void private
-
.restore_line_wrap ⇒ String
private
Enable wrapping as a fallback for terminals that ignore mode save/restore.
- .right ⇒ String private
- .show_cursor ⇒ String private
- .size ⇒ Array<(Integer, Integer)>? private
-
.size_stale! ⇒ void
private
Mark the cached terminal size as stale so it is re-read on next use, e.g.
- .strikethrough ⇒ String private
- .strip_ansi(string) ⇒ String private
- .truncate(string) ⇒ String private
- .underline ⇒ String private
- .up ⇒ String private
-
.width ⇒ Integer
private
Keep in sync with
columnsin Library/Homebrew/utils/tty.sh. - .with(stream, &_block) ⇒ void private
- .yellow ⇒ String private
Class Method Details
.begin_synchronized_update ⇒ 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.
151 152 153 |
# File 'utils/tty.rb', line 151 def begin_synchronized_update "\033[?2026h" end |
.blue ⇒ 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.
11 |
# File 'sorbet/rbi/dsl/tty.rbi', line 11 def blue; end |
.bold ⇒ 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.
14 |
# File 'sorbet/rbi/dsl/tty.rbi', line 14 def bold; end |
.clear_to_end ⇒ 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.
119 120 121 |
# File 'utils/tty.rb', line 119 def clear_to_end "\033[K" end |
.clear_to_end_of_screen ⇒ 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.
124 125 126 |
# File 'utils/tty.rb', line 124 def clear_to_end_of_screen "\033[J" 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).
96 97 98 99 100 101 |
# File 'utils/tty.rb', line 96 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.
236 237 238 239 240 241 242 243 244 |
# File 'utils/tty.rb', line 236 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_sequence ⇒ 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.
196 197 198 199 200 |
# File 'utils/tty.rb', line 196 def current_escape_sequence return "" if @escape_sequence.nil? "\033[#{@escape_sequence.join(";")}m" end |
.cyan ⇒ 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.
17 |
# File 'sorbet/rbi/dsl/tty.rbi', line 17 def cyan; end |
.default ⇒ 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.
20 |
# File 'sorbet/rbi/dsl/tty.rbi', line 20 def default; end |
.disable_line_wrap ⇒ 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.
Save auto-wrap mode and disable it so echoed input cannot wrap a progress row.
140 141 142 |
# File 'utils/tty.rb', line 140 def disable_line_wrap "\033[?7s\033[?7l" end |
.down ⇒ 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.
23 |
# File 'sorbet/rbi/dsl/tty.rbi', line 23 def down; end |
.end_synchronized_update ⇒ 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.
156 157 158 |
# File 'utils/tty.rb', line 156 def end_synchronized_update "\033[?2026l" end |
.erase_char ⇒ 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.
26 |
# File 'sorbet/rbi/dsl/tty.rbi', line 26 def erase_char; end |
.erase_line ⇒ 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.
29 |
# File 'sorbet/rbi/dsl/tty.rbi', line 29 def erase_line; end |
.green ⇒ 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.
32 |
# File 'sorbet/rbi/dsl/tty.rbi', line 32 def green; end |
.height ⇒ 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.
177 178 179 |
# File 'utils/tty.rb', line 177 def height size&.first || @height ||= Utils.popen_read_text("/usr/bin/tput", "lines", err: File::NULL).presence&.to_i || 40 end |
.hide_cursor ⇒ 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.
129 130 131 |
# File 'utils/tty.rb', line 129 def hide_cursor "\033[?25l" end |
.italic ⇒ 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.
35 |
# File 'sorbet/rbi/dsl/tty.rbi', line 35 def italic; end |
.left ⇒ 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.
38 |
# File 'sorbet/rbi/dsl/tty.rbi', line 38 def left; end |
.magenta ⇒ 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.
41 |
# File 'sorbet/rbi/dsl/tty.rbi', line 41 def magenta; end |
.move_cursor_beginning ⇒ 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.
109 110 111 |
# File 'utils/tty.rb', line 109 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.
114 115 116 |
# File 'utils/tty.rb', line 114 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.
104 105 106 |
# File 'utils/tty.rb', line 104 def move_cursor_up_beginning(line_count) "\033[#{line_count}F" end |
.no_underline ⇒ 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.
44 |
# File 'sorbet/rbi/dsl/tty.rbi', line 44 def no_underline; end |
.red ⇒ 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.
47 |
# File 'sorbet/rbi/dsl/tty.rbi', line 47 def red; end |
.reset ⇒ 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.
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.
203 204 205 |
# File 'utils/tty.rb', line 203 def reset_escape_sequence! @escape_sequence = T.let(nil, T.nilable(T::Array[Integer])) end |
.restore_line_wrap ⇒ 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.
Enable wrapping as a fallback for terminals that ignore mode save/restore.
146 147 148 |
# File 'utils/tty.rb', line 146 def restore_line_wrap "\033[?7h\033[?7r" end |
.right ⇒ 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.
53 |
# File 'sorbet/rbi/dsl/tty.rbi', line 53 def right; end |
.show_cursor ⇒ 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.
134 135 136 |
# File 'utils/tty.rb', line 134 def show_cursor "\033[?25h" end |
.size ⇒ Array<(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.
161 162 163 164 165 166 |
# File 'utils/tty.rb', line 161 def size clear_stale_size! @size ||= ($stdin.winsize if $stdin.tty?) rescue IOError, SystemCallError nil end |
.size_stale! ⇒ 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.
Mark the cached terminal size as stale so it is re-read on next use,
e.g. after a resize. Only set a flag as a SIGWINCH handler may call
this while size, height or width are caching an old value.
172 173 174 |
# File 'utils/tty.rb', line 172 def size_stale! @size_stale = T.let(true, T.nilable(T::Boolean)) end |
.strikethrough ⇒ 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.
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.
86 87 88 89 90 91 92 |
# File 'utils/tty.rb', line 86 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.
188 189 190 191 192 193 |
# File 'utils/tty.rb', line 188 def truncate(string) w = width return string.to_s if w.zero? string.to_s[0, w - 4] || "" end |
.underline ⇒ 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.
59 |
# File 'sorbet/rbi/dsl/tty.rbi', line 59 def underline; end |
.up ⇒ 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.
62 |
# File 'sorbet/rbi/dsl/tty.rbi', line 62 def up; end |
.width ⇒ 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.
Keep in sync with columns in Library/Homebrew/utils/tty.sh.
183 184 185 |
# File 'utils/tty.rb', line 183 def width size&.second || @width ||= Utils.popen_read_text("/usr/bin/tput", "cols", err: File::NULL).presence&.to_i || 80 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.
76 77 78 79 80 81 82 83 |
# File 'utils/tty.rb', line 76 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 |
.yellow ⇒ 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.
65 |
# File 'sorbet/rbi/dsl/tty.rbi', line 65 def yellow; end |