Skip to content

Commit

Permalink
dev-cmd/cat: auto-install bat when running brew cat ... with `HOM…
Browse files Browse the repository at this point in the history
…EBREW_BAT` set
  • Loading branch information
XuehaiPan committed Nov 23, 2021
1 parent 354718c commit 687dbe5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
11 changes: 10 additions & 1 deletion Library/Homebrew/dev-cmd/cat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# frozen_string_literal: true

require "cli/parser"
require "formula"

module Homebrew
extend T::Sig
Expand Down Expand Up @@ -31,8 +32,16 @@ def cat

cd HOMEBREW_REPOSITORY
pager = if Homebrew::EnvConfig.bat?
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
Expand Down
20 changes: 11 additions & 9 deletions Library/Homebrew/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,21 +437,23 @@ def capture_stderr
$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("/")
Expand Down

0 comments on commit 687dbe5

Please sign in to comment.