Merge pull request #12465 from XuehaiPan/bat-back-to-cat

dev-cmd/cat: auto-install `bat` when running `brew cat ...` with `HOMEBREW_BAT` set
This commit is contained in:
Mike McQuaid 2021-11-23 14:10:28 +00:00 committed by GitHub
commit 9be410b348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 10 deletions

View File

@ -31,8 +31,18 @@ module Homebrew
cd HOMEBREW_REPOSITORY
pager = if Homebrew::EnvConfig.bat?
require "formula"
unless Formula["bat"].any_version_installed?
# The user might want to capture the output of `brew cat ...`
# Redirect stdout to stderr
redirect_stdout($stderr) do
ohai "Installing `bat` for displaying <formula>/<cask> source..."
safe_system HOMEBREW_BREW_FILE, "install", "bat"
end
end
ENV["BAT_CONFIG_PATH"] = Homebrew::EnvConfig.bat_config_path
"#{HOMEBREW_PREFIX}/bin/bat"
Formula["bat"].opt_bin/"bat"
else
"cat"
end

View File

@ -437,21 +437,23 @@ module Kernel
$stderr = old
end
def nostdout
def nostdout(&block)
if verbose?
yield
else
begin
out = $stdout.dup
$stdout.reopen(File::NULL)
yield
ensure
$stdout.reopen(out)
out.close
end
redirect_stdout(File::NULL, &block)
end
end
def redirect_stdout(file)
out = $stdout.dup
$stdout.reopen(file)
yield
ensure
$stdout.reopen(out)
out.close
end
def paths
@paths ||= PATH.new(ENV["HOMEBREW_PATH"]).map do |p|
File.expand_path(p).chomp("/")