66 lines
1.6 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
2019-04-17 18:25:08 +09:00
require "cli/parser"
2019-01-18 22:17:46 +05:30
module Homebrew
2020-10-20 12:03:48 +02:00
extend T::Sig
2016-09-26 01:44:51 +02:00
module_function
2020-10-20 12:03:48 +02:00
sig { returns(CLI::Parser) }
2019-01-18 22:17:46 +05:30
def cat_args
Homebrew::CLI::Parser.new do
description <<~EOS
2020-11-19 16:01:04 +01:00
Display the source of a <formula> or <cask>.
2019-01-18 22:17:46 +05:30
EOS
2020-11-12 10:40:41 -05:00
2020-11-19 16:01:04 +01:00
switch "--formula", "--formulae",
description: "Treat all named arguments as formulae."
switch "--cask", "--casks",
description: "Treat all named arguments as casks."
2020-11-27 11:41:08 -05:00
2020-11-19 16:01:04 +01:00
conflicts "--formula", "--cask"
named_args [:formula, :cask], min: 1
2019-01-18 22:17:46 +05:30
end
end
def cat
2020-07-30 18:40:10 +02:00
args = cat_args.parse
cd HOMEBREW_REPOSITORY
2020-04-05 15:44:50 +01:00
pager = if Homebrew::EnvConfig.bat?
ENV["BAT_CONFIG_PATH"] = Homebrew::EnvConfig.bat_config_path
ENV["BAT_THEME"] = Homebrew::EnvConfig.bat_theme
ensure_formula_installed!(
"bat",
reason: "displaying <formula>/<cask> source",
# The user might want to capture the output of `brew cat ...`
# Redirect stdout to stderr
output_to_stderr: true,
).opt_bin/"bat"
2020-04-05 15:44:50 +01:00
else
"cat"
2019-09-30 15:40:20 +02:00
end
2020-11-19 16:01:04 +01:00
args.named.to_paths.each do |path|
next path if path.exist?
path = path.basename(".rb") if args.cask?
ofail "#{path}'s source doesn't exist on disk."
end
if Homebrew.failed?
$stderr.puts "The name may be wrong, or the tap hasn't been tapped. Instead try:"
treat_as = "--cask " if args.cask?
treat_as = "--formula " if args.formula?
$stderr.puts " brew info --github #{treat_as}#{args.named.join(" ")}"
return
end
safe_system pager, *args.named.to_paths
end
end