2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-08-09 02:59:35 +02:00
|
|
|
# Various helper functions for interacting with TTYs.
|
|
|
|
#
|
|
|
|
# @api private
|
2016-08-26 16:04:47 +02:00
|
|
|
module Tty
|
2020-09-17 04:18:13 +05:30
|
|
|
@stream = $stdout
|
|
|
|
|
2020-10-10 17:53:31 +02:00
|
|
|
class << self
|
|
|
|
extend T::Sig
|
|
|
|
|
|
|
|
sig { params(stream: T.any(IO, StringIO), _block: T.proc.params(arg0: T.any(IO, StringIO)).void).void }
|
|
|
|
def with(stream, &_block)
|
|
|
|
previous_stream = @stream
|
|
|
|
@stream = stream
|
|
|
|
|
|
|
|
yield stream
|
|
|
|
ensure
|
|
|
|
@stream = previous_stream
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { params(string: String).returns(String) }
|
|
|
|
def strip_ansi(string)
|
|
|
|
string.gsub(/\033\[\d+(;\d+)*m/, "")
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { returns(Integer) }
|
|
|
|
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
|
|
|
|
|
|
|
|
sig { params(string: String).returns(String) }
|
|
|
|
def truncate(string)
|
|
|
|
(w = width).zero? ? string.to_s : (string.to_s[0, w - 4] || "")
|
2018-06-14 22:14:46 +02:00
|
|
|
end
|
2020-10-10 17:53:31 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
SPECIAL_CODES = {
|
|
|
|
up: "1A",
|
|
|
|
down: "1B",
|
|
|
|
right: "1C",
|
|
|
|
left: "1D",
|
|
|
|
erase_line: "K",
|
|
|
|
erase_char: "P",
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
CODES = COLOR_CODES.merge(STYLE_CODES).freeze
|
|
|
|
|
|
|
|
sig { returns(String) }
|
|
|
|
def current_escape_sequence
|
|
|
|
return "" if @escape_sequence.nil?
|
|
|
|
|
|
|
|
"\033[#{@escape_sequence.join(";")}m"
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { void }
|
|
|
|
def reset_escape_sequence!
|
|
|
|
@escape_sequence = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
CODES.each do |name, code|
|
|
|
|
define_method(name) do
|
|
|
|
@escape_sequence ||= []
|
|
|
|
@escape_sequence << code
|
|
|
|
self
|
|
|
|
end
|
2016-08-26 16:04:47 +02:00
|
|
|
end
|
2020-10-10 17:53:31 +02:00
|
|
|
|
|
|
|
SPECIAL_CODES.each do |name, code|
|
|
|
|
define_method(name) do
|
|
|
|
if @stream.tty?
|
|
|
|
"\033[#{code}"
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
2020-09-03 00:41:16 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-10 17:53:31 +02:00
|
|
|
sig { returns(String) }
|
|
|
|
def to_s
|
|
|
|
return "" unless color?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2020-10-10 17:53:31 +02:00
|
|
|
current_escape_sequence
|
|
|
|
ensure
|
|
|
|
reset_escape_sequence!
|
|
|
|
end
|
2019-10-04 09:17:44 +02:00
|
|
|
|
2020-10-10 17:53:31 +02:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def color?
|
|
|
|
return false if Homebrew::EnvConfig.no_color?
|
|
|
|
return true if Homebrew::EnvConfig.color?
|
2019-10-04 09:17:44 +02:00
|
|
|
|
2020-10-10 17:53:31 +02:00
|
|
|
@stream.tty?
|
|
|
|
end
|
|
|
|
end
|
2016-08-26 16:04:47 +02:00
|
|
|
end
|