Skip to content

Instantly share code, notes, and snippets.

@neocsr
Forked from rgo/.irbrc.rb
Created January 20, 2011 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neocsr/788622 to your computer and use it in GitHub Desktop.
Save neocsr/788622 to your computer and use it in GitHub Desktop.
# Forked from Iain Hecker, http://iain.nl
# put all this in your ~/.irbrc
# gem install wirble hirb ap interactive_editor
# TODO: Can fail silently if gems are missing.
puts "Loading '#{File.expand_path(__FILE__)}'..."
require 'rubygems'
require 'yaml'
if ENV['RAILS_ENV']
load File.dirname(__FILE__) + '/.railsrc'
else
require 'interactive_editor'
end
require 'pp'
alias q exit
class Object
def local_methods
(methods - Object.instance_methods).sort
end
end
ANSI = {}
ANSI[:RESET] = "\e[0m"
ANSI[:BOLD] = "\e[1m"
ANSI[:UNDERLINE] = "\e[4m"
ANSI[:LGRAY] = "\e[0;37m"
ANSI[:GRAY] = "\e[1;30m"
ANSI[:RED] = "\e[31m"
ANSI[:GREEN] = "\e[32m"
ANSI[:YELLOW] = "\e[33m"
ANSI[:BLUE] = "\e[34m"
ANSI[:MAGENTA] = "\e[35m"
ANSI[:CYAN] = "\e[36m"
ANSI[:WHITE] = "\e[37m"
# Build a simple colorful IRB prompt
IRB.conf[:PROMPT][:SIMPLE_COLOR] = {
:PROMPT_I => "#{ANSI[:MAGENTA]}>>#{ANSI[:RESET]} ",
:PROMPT_N => "#{ANSI[:MAGENTA]}>>#{ANSI[:RESET]} ",
:PROMPT_C => "#{ANSI[:RED]}?>#{ANSI[:RESET]} ",
:PROMPT_S => "#{ANSI[:YELLOW]}?>#{ANSI[:RESET]} ",
:RETURN => "#{ANSI[:GREEN]}=>#{ANSI[:RESET]} %s\n",
:AUTO_INDENT => true }
IRB.conf[:PROMPT_MODE] = :SIMPLE_COLOR
# Loading extensions of the console. This is wrapped
# because some might not be included in your Gemfile
# and errors will be raised
def extend_console(name, care = true, required = true)
if care
require name if required
yield if block_given?
$console_extensions << "#{ANSI[:GREEN]}#{name}#{ANSI[:RESET]}"
else
$console_extensions << "#{ANSI[:MAGENTA]}#{name}#{ANSI[:RESET]}"
end
rescue LoadError
$console_extensions << "#{ANSI[:RED]}#{name}#{ANSI[:RESET]}"
end
$console_extensions = []
# Wirble is a gem that handles coloring the IRB
# output and history
extend_console 'wirble' do
Wirble.init
Wirble.colorize
end
# Hirb makes tables easy.
extend_console 'hirb' do
# command out the following options to get tables for everything
Hirb.enable # :output=>{'Object'=>{:class=>:auto_table, :ancestor=>true}}
extend Hirb::Console
end
# awesome_print is prints prettier than pretty_print
extend_console 'ap' do
alias pp ap
end
# When you're using Rails 2 console, show queries in the console
extend_console 'rails2', (ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')), false do
require 'logger'
RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
end
# When you're using Rails 3 console, show queries in the console
extend_console 'rails3', defined?(ActiveSupport::Notifications), false do
$odd_or_even_queries = false
ActiveSupport::Notifications.subscribe('active_record.sql') do |*args|
$odd_or_even_queries = !$odd_or_even_queries
color = $odd_or_even_queries ? ANSI[:CYAN] : ANSI[:MAGENTA]
time = "%.1fms" % ((args[2] - args[1]) * 1000)
name = args.last[:name]
sql = args.last[:sql].gsub("\n", " ").squeeze(" ")
puts " #{ANSI[:UNDERLINE]}#{color}#{name} (#{time})#{ANSI[:RESET]} #{sql}"
end
end
# Add a method pm that shows every method on an object
# Pass a regex to filter these
extend_console 'pm', true, false do
def pm(obj, *options) # Print methods
methods = obj.methods
methods -= Object.methods unless options.include? :more
filter = options.select {|opt| opt.kind_of? Regexp}.first
methods = methods.select {|name| name =~ filter} if filter
data = methods.sort.collect do |name|
method = obj.method(name)
if method.arity == 0
args = "()"
elsif method.arity > 0
n = method.arity
args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")})"
elsif method.arity < 0
n = -method.arity
args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")}, ...)"
end
klass = $1 if method.inspect =~ /Method: (.*?)#/
[name.to_s, args, klass]
end
max_name = data.collect {|item| item[0].size}.max
max_args = data.collect {|item| item[1].size}.max
data.each do |item|
print " #{ANSI[:YELLOW]}#{item[0].to_s.rjust(max_name)}#{ANSI[:RESET]}"
print "#{ANSI[:BLUE]}#{item[1].ljust(max_args)}#{ANSI[:RESET]}"
print " #{ANSI[:GRAY]}#{item[2]}#{ANSI[:RESET]}\n"
end
data.size
end
end
# HISTFILE = "~/.irb.hist"
# MAXHISTSIZE = 100
#
# begin # Persistent command history
# if defined? Readline::HISTORY
# histfile = File::expand_path( HISTFILE )
# if File::exists?( histfile )
# lines = IO::readlines( histfile ).collect {|line| line.chomp}
# puts "Read %d saved history commands from %s." %
# [ lines.nitems, histfile ] if $DEBUG || $VERBOSE
# Readline::HISTORY.push( *lines )
# else
# puts "History file '%s' was empty or non-existant." %
# histfile if $DEBUG || $VERBOSE
# end
#
# Kernel::at_exit {
# lines = Readline::HISTORY.to_a.reverse.uniq.reverse
# lines = lines[ -MAXHISTSIZE, MAXHISTSIZE ] if lines.nitems > MAXHISTSIZE
# $stderr.puts "Saving %d history lines to %s." % [ lines.length, histfile ] if $VERBOSE || $DEBUG
# File::open( histfile, File::WRONLY|File::CREAT|File::TRUNC ) {|ofh|
# lines.each {|line| ofh.puts line }
# }
# }
# end
# end
# Show results of all extension-loading
puts "#{ANSI[:MAGENTA]}~> Console extensions:#{ANSI[:RESET]} #{$console_extensions.join(' ')}#{ANSI[:RESET]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment