Mike McQuaid c5dbd3ca24
Rearrange requires
This improves the load time of most brew commands. For an example of
one of the simplest commands this speeds up:

Without Bootsnap:
```
$ hyperfine 'git checkout master; brew help' 'git checkout optimise_requires; brew help'
Benchmark 1: git checkout master; brew help
  Time (mean ± σ):     525.0 ms ±  35.8 ms    [User: 229.9 ms, System: 113.1 ms]
  Range (min … max):   465.3 ms … 576.6 ms    10 runs

Benchmark 2: git checkout optimise_requires; brew help
  Time (mean ± σ):     383.3 ms ±  25.1 ms    [User: 133.0 ms, System: 72.1 ms]
  Range (min … max):   353.0 ms … 443.6 ms    10 runs

Summary
  git checkout optimise_requires; brew help ran
    1.37 ± 0.13 times faster than git checkout master; brew help
```

With Bootsnap:
```
$ hyperfine 'git checkout master; brew help' 'git checkout optimise_requires; brew help'
Benchmark 1: git checkout master; brew help
  Time (mean ± σ):     386.0 ms ±  30.9 ms    [User: 130.2 ms, System: 93.8 ms]
  Range (min … max):   359.5 ms … 469.3 ms    10 runs

Benchmark 2: git checkout optimise_requires; brew help
  Time (mean ± σ):     330.2 ms ±  32.4 ms    [User: 93.4 ms, System: 73.0 ms]
  Range (min … max):   302.9 ms … 413.9 ms    10 runs

Summary
  git checkout optimise_requires; brew help ran
    1.17 ± 0.15 times faster than git checkout master; brew help
```
2024-07-14 08:49:39 -04:00

119 lines
2.4 KiB
Ruby

# typed: true
# frozen_string_literal: true
# Various helper functions for interacting with TTYs.
module Tty
@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
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] || "")
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
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?
current_escape_sequence
ensure
reset_escape_sequence!
end
sig { returns(T::Boolean) }
def color?
require "env_config"
return false if Homebrew::EnvConfig.no_color?
return true if Homebrew::EnvConfig.color?
@stream.tty?
end
end
end