apainintheneck 9f7ab25af5 irb: improve pry config
The idea here is that the pry session history
should be separate for homebrew than the global
pry history.

We also ignore any .pryrc files so that they
don't interfere with this config.
2023-03-06 22:37:09 -08:00

89 lines
2.0 KiB
Ruby

# typed: false
# frozen_string_literal: true
require "formulary"
require "cli/parser"
class Symbol
def f(*args)
Formulary.factory(to_s, *args)
end
end
class String
def f(*args)
Formulary.factory(self, *args)
end
end
module Homebrew
extend T::Sig
module_function
sig { returns(CLI::Parser) }
def irb_args
Homebrew::CLI::Parser.new do
description <<~EOS
Enter the interactive Homebrew Ruby shell.
EOS
switch "--examples",
description: "Show several examples."
switch "--pry",
env: :pry,
description: "Use Pry instead of IRB. Implied if `HOMEBREW_PRY` is set."
end
end
def irb
# work around IRB modifying ARGV.
args = irb_args.parse(ARGV.dup.freeze)
clean_argv
if args.examples?
puts <<~EOS
'v8'.f # => instance of the v8 formula
:hub.f.latest_version_installed?
:lua.f.methods - 1.methods
:mpd.f.recursive_dependencies.reject(&:installed?)
EOS
return
end
if args.pry?
Homebrew.install_gem_setup_path! "pry"
require "pry"
else
require "irb"
end
require "formula"
require "keg"
require "cask"
ohai "Interactive Homebrew Shell", "Example commands available with: `brew irb --examples`"
if args.pry?
Pry.config.should_load_rc = false # skip loading .pryrc
Pry.config.history_file = (HOMEBREW_REPOSITORY/".pry_history").to_s
Pry.config.memory_size = 100 # max lines to save to history file
Pry.config.prompt_name = "brew"
Pry.start
else
ENV["IRBRC"] = (HOMEBREW_REPOSITORY/".irb_config").to_s
IRB.start
end
end
# Remove the `--debug`, `--verbose` and `--quiet` options which cause problems
# for IRB and have already been parsed by the CLI::Parser.
def clean_argv
global_options = Homebrew::CLI::Parser
.global_options
.flat_map { |options| options[0..1] }
ARGV.reject! { |arg| global_options.include?(arg) }
end
end