Skip to content

Instantly share code, notes, and snippets.

@karmatr0n
Last active January 18, 2025 15:04
Show Gist options
  • Save karmatr0n/5882f43b17bff1d6e7eb30c0c1da960c to your computer and use it in GitHub Desktop.
Save karmatr0n/5882f43b17bff1d6e7eb30c0c1da960c to your computer and use it in GitHub Desktop.
irbrc
def load_gems!
%w[irb/completion irb/history irb/color rubygems].each do |gem|
require gem
end
return unless Gem::Version.new(RUBY_VERSION) > Gem::Version.new('2.7.0')
require 'irb/easter-egg'
end
def install_unbundled_gem!(gem)
Gem::Specification.find_by_name(gem)
rescue Gem::MissingSpecError
puts "Installing #{gem} gem"
Gem.install(gem)
end
def require_unbundled_gem!(gem)
spec = Gem::Specification.find_by_name(gem)
$LOAD_PATH.unshift(spec.lib_dirs_glob) unless spec.nil?
require gem
end
def install_unbundled_gems!(gems)
gems.each do |gem|
next if Gem.loaded_specs.key?(gem)
install_unbundled_gem!(gem)
require_unbundled_gem!(gem)
end
end
def gem_sources
if Gem.sources.any?
Gem.sources.map(&:to_s)
else
%w[https://rubygems.org]
end
end
def install_bundled_gems!(gems)
require 'bundler/inline'
gemfile do
gem_sources.each do |gem_source|
source gem_source
end
gems.each do |gem_name|
next if Gem.loaded_specs.key?(gem_name)
gem gem_name
end
end
end
def install_gems!
gem_list = %w[pastel neovim os amazing_print solargraph hexdump irbtools] # repl_type_completor
if defined?(Bundler)
install_bundled_gems!(gem_list)
else
install_unbundled_gems!(gem_list)
end
end
def app_env
app_env = ENV.values_at('RAILS_ENV', 'APP_ENV', 'RACK_ENV').compact.first
return app_env unless app_env.nil?
Rails.env if defined?(Rails)
end
def prompt_prefix
prefix = "\u{F219} %N(#{app_env || '%m'})"
pastel = Pastel.new
if app_env == 'production'
pastel.bright_red.bold(prefix)
else
pastel.bright_green.bold(prefix)
end
end
def custom_prompt
prefix = prompt_prefix
{
PROMPT_I: "#{prefix}:%03n:%i> ",
PROMPT_N: "#{prefix}:%03n:%i> ",
PROMPT_S: "#{prefix}:%03n:%i%l ",
PROMPT_C: "#{prefix}:%03n:%i* ",
RETURN: " => %s\n",
AUTO_INDENT: true
}
end
def configure_irb!
IRB.conf[:AUTO_INDENT] = true
IRB.conf[:PROMPT][:CUSTOM] = custom_prompt
IRB.conf[:PROMPT_MODE] = :CUSTOM
IRB.conf[:HISTORY_FILE] = File.join(ENV['HOME'], '.irb_history')
IRB.conf[:SAVE_HISTORY] = 10_000
IRB.conf[:EVAL_HISTORY] = 20
IRB.conf[:USE_PAGER] = true
return unless app_env != :production
IRB.conf[:COMPLETOR] = :type
end
def load_rbs!
return unless File.exist?('./rbs_collection.yaml') || Dir.exist?('./sig')
ReplTypeCompletor.preload_rbs # load_rbs
end
def copy(str)
if OS.mac?
IO.open('pbcopy', 'w') { |f| f << str.to_s }
elsif OS.posix?
IO.open('xsel --clipboard --input', 'w') { |f| f << str.to_s }
else
warn 'Not supported platform to copy content to the clipboard'
end
end
def paste
if OS.mac?
system('pbpaste')
elsif OS.posix?
system('xsel --clipboard --output')
else
warn 'Not supported platform to paste from the clipboard'
end
end
def copy_history
history = Readline::HISTORY.entries
index = history.rindex('exit') || - 1
content = history[index.next..-2].join("\n")
puts(content)
copy(content)
end
def run_easter_egg
IRB.send(:easter_egg, %i[logo dancing].sample)
end
def split_long_string(string, length = 70)
string.each_char.each_slice(length).map(&:join).map { |s| "'#{s}'" }.join(" \\ \n")
end
load_gems!
install_gems!
configure_irb!
# load_rbs!
AmazingPrint.pry!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment