brew/Library/Homebrew/utils/formatter.rb
Issy Long 45978435e7
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 15:24:27 +01:00

145 lines
4.1 KiB
Ruby

# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "utils/tty"
# Helper module for formatting output.
#
# @api internal
module Formatter
COMMAND_DESC_WIDTH = 80
OPTION_DESC_WIDTH = 45
def self.arrow(string, color: nil)
prefix("==>", string, color)
end
# Format a string as headline.
#
# @api internal
def self.headline(string, color: nil)
arrow("#{Tty.bold}#{string}#{Tty.reset}", color:)
end
def self.identifier(string)
"#{Tty.green}#{string}#{Tty.default}"
end
def self.option(string)
"#{Tty.bold}#{string}#{Tty.reset}"
end
# Format a string as success, with an optional label.
#
# @api internal
def self.success(string, label: nil)
label(label, string, :green)
end
# Format a string as warning, with an optional label.
#
# @api internal
def self.warning(string, label: nil)
label(label, string, :yellow)
end
# Format a string as error, with an optional label.
#
# @api internal
def self.error(string, label: nil)
label(label, string, :red)
end
# Wraps text to fit within a given number of columns using regular expressions that:
#
# 1. convert hard-wrapped paragraphs to a single line
# 2. add line break and indent to subcommand descriptions
# 3. find any option descriptions longer than a pre-set length and wrap between words
# with a hanging indent, without breaking any words that overflow
# 4. wrap any remaining description lines that need wrapping with the same indent
# 5. wrap all lines to the given width.
#
# Note that an option (e.g. `--foo`) may not be at the beginning of a line,
# so we always wrap one word before an option.
# @see https://github.com/Homebrew/brew/pull/12672
# @see https://macromates.com/blog/2006/wrapping-text-with-regular-expressions/
def self.format_help_text(string, width: 172)
desc = OPTION_DESC_WIDTH
indent = width - desc
string.gsub(/(?<=\S) *\n(?=\S)/, " ")
.gsub(/([`>)\]]:) /, "\\1\n ")
.gsub(/^( +-.+ +(?=\S.{#{desc}}))(.{1,#{desc}})( +|$)(?!-)\n?/, "\\1\\2\n#{" " * indent}")
.gsub(/^( {#{indent}}(?=\S.{#{desc}}))(.{1,#{desc}})( +|$)(?!-)\n?/, "\\1\\2\n#{" " * indent}")
.gsub(/(.{1,#{width}})( +|$)(?!-)\n?/, "\\1\n")
end
def self.url(string)
"#{Tty.underline}#{string}#{Tty.no_underline}"
end
def self.label(label, string, color)
label = "#{label}:" unless label.nil?
prefix(label, string, color)
end
private_class_method :label
def self.prefix(prefix, string, color)
if prefix.nil? && color.nil?
string
elsif prefix.nil?
"#{Tty.send(color)}#{string}#{Tty.reset}"
elsif color.nil?
"#{prefix} #{string}"
else
"#{Tty.send(color)}#{prefix}#{Tty.reset} #{string}"
end
end
private_class_method :prefix
# Layout objects in columns that fit the current terminal width.
#
# @api internal
def self.columns(*objects, gap_size: 2)
objects = objects.flatten.map(&:to_s)
fallback = proc do
return objects.join("\n").concat("\n")
end
fallback.call if objects.empty?
fallback.call if respond_to?(:tty?) ? !T.unsafe(self).tty? : !$stdout.tty?
console_width = Tty.width
object_lengths = objects.map { |obj| Tty.strip_ansi(obj).length }
cols = (console_width + gap_size) / (T.must(object_lengths.max) + gap_size)
fallback.call if cols < 2
rows = (objects.count + cols - 1) / cols
cols = (objects.count + rows - 1) / rows # avoid empty trailing columns
col_width = ((console_width + gap_size) / cols) - gap_size
gap_string = "".rjust(gap_size)
output = +""
rows.times do |row_index|
item_indices_for_row = T.cast(row_index.step(objects.size - 1, rows).to_a, T::Array[Integer])
first_n = T.must(item_indices_for_row[0...-1]).map do |index|
objects[index] + "".rjust(col_width - object_lengths.fetch(index))
end
# don't add trailing whitespace to last column
last = objects.values_at(item_indices_for_row.fetch(-1))
output.concat((first_n + last)
.join(gap_string))
.concat("\n")
end
output.freeze
end
end