154 lines
3.1 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
2020-08-09 02:59:35 +02:00
# Various helper functions for interacting with TTYs.
2016-08-26 16:04:47 +02:00
module Tty
2020-09-17 04:18:13 +05:30
@stream = $stdout
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
class << self
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
2024-08-14 21:41:01 +02:00
sig { params(line_count: Integer).returns(String) }
def move_cursor_up(line_count)
"\033[#{line_count}A"
end
sig { params(line_count: Integer).returns(String) }
def move_cursor_down(line_count)
"\033[#{line_count}B"
end
sig { returns(String) }
def clear_to_end
"\033[K"
end
sig { returns(String) }
def hide_cursor
"\033[?25l"
end
sig { returns(String) }
def show_cursor
"\033[?25h"
end
sig { returns(T.nilable([Integer, Integer])) }
def size
2024-07-15 21:09:26 -04:00
return @size if defined?(@size)
height, width = `/bin/stty size 2>/dev/null`.presence&.split&.map(&:to_i)
return if height.nil? || width.nil?
@size = [height, width]
end
sig { returns(Integer) }
def height
2024-07-15 21:09:26 -04:00
@height ||= size&.first || `/usr/bin/tput lines 2>/dev/null`.presence&.to_i || 40
end
sig { returns(Integer) }
def width
2024-07-15 21:09:26 -04:00
@width ||= size&.second || `/usr/bin/tput cols 2>/dev/null`.presence&.to_i || 80
end
sig { params(string: String).returns(String) }
def truncate(string)
(w = width).zero? ? string.to_s : (string.to_s[0, w - 4] || "")
end
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
SPECIAL_CODES.each do |name, code|
define_method(name) do
if @stream.tty?
"\033[#{code}"
else
""
end
end
end
sig { returns(String) }
def to_s
return "" unless color?
2018-09-17 02:45:00 +02:00
current_escape_sequence
ensure
reset_escape_sequence!
end
2019-10-04 09:17:44 +02:00
sig { returns(T::Boolean) }
def color?
require "env_config"
return false if Homebrew::EnvConfig.no_color?
return true if Homebrew::EnvConfig.color?
2019-10-04 09:17:44 +02:00
@stream.tty?
end
2023-02-24 13:50:07 +00:00
end
2016-08-26 16:04:47 +02:00
end