Skip to content

Instantly share code, notes, and snippets.

@danahern
Created March 9, 2011 20:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danahern/862902 to your computer and use it in GitHub Desktop.
Save danahern/862902 to your computer and use it in GitHub Desktop.
# IRBRC file by Iain Hecker, http://iain.nl
# put all this in your ~/.irbrc
require 'rubygems'
require 'yaml'
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[0;90m"
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[:BLUE]}>>#{ANSI[:RESET]} ",
:PROMPT_N => "#{ANSI[:BLUE]}>>#{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
# 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[:GRAY]}#{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
# extend_console 'wirb' do
# Wirb.start
# end
# Hirb makes tables easy.
# extend_console 'hirb' do
# Hirb.enable
# extend Hirb::Console
# end
# awesome_print is prints prettier than pretty_print
extend_console 'ap' do
alias pp ap
end
extend_console 'net-http-spy' do
#
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('sql.active_record') do |*args|
$odd_or_even_queries = !$odd_or_even_queries
color = $odd_or_even_queries ? ANSI[:CYAN] : ANSI[:MAGENTA]
event = ActiveSupport::Notifications::Event.new(*args)
time = "%.1fms" % event.duration
name = event.payload[:name]
sql = event.payload[: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
# extend_console 'interactive_editor' do
# # no configuration needed
# end
# Show results of all extension-loading
puts "#{ANSI[:GRAY]}~> Console extensions:#{ANSI[:RESET]} #{$console_extensions.join(' ')}#{ANSI[:RESET]}"
# Save History between irb sessions
# Setup permanent history.
HISTFILE = "~/.irb_history"
MAXHISTSIZE = 100
begin
histfile = File::expand_path(HISTFILE)
if File::exists?(histfile)
lines = IO::readlines(histfile).collect { |line| line.chomp }
puts "Read #{lines.nitems} saved history commands from '#{histfile}'." if $VERBOSE
Readline::HISTORY.push(* lines)
else
puts "History file '#{histfile}' was empty or non-existant." if $VERBOSE
end
Kernel::at_exit do
lines = Readline::HISTORY.to_a.reverse.uniq.reverse
lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.nitems > MAXHISTSIZE
puts "Saving #{lines.length} history lines to '#{histfile}'." if $VERBOSE
File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
end
rescue => e
puts "Error when configuring permanent history: #{e}" if $VERBOSE
end
def clear
system 'clear'
if ENV['RAILS_ENV']
return "Rails environment: " + ENV['RAILS_ENV']
else
return "No rails environment - happy hacking!";
end
end
# Tab Completion
require 'irb/completion'
# Quick way to run just a few specific lines from a file
def eval_lines(fn, lines)
eval( File.readlines(fn)[lines].join)
end
# # require 'irb/completion'
# # require 'irb/ext/save-history'
# # #
# # # ARGV.concat [ "--readline",
# # # "--prompt-mode",
# # # "simple" ]
# #
# # require 'rubygems'
# # require 'wirble'
# # Wirble.init
# # Wirble.colorize
# #
# # class Object
# # def local_methods
# # (methods - Object.instance_methods).sort
# # end
# # end
# #
# # # Log to STDOUT if in Rails
# # if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
# # require 'logger'
# # RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
# # end
# #
# # if defined?(Rails) && Rails.version > "2.9"
# # ActiveRecord::Base.logger = Logger.new STDOUT
# # end
#
#
#
# # Make gems available
# require 'rubygems'
#
#
# # Pretty Print method
# require 'pp'
#
# # Awesome Print gem (gem install awesome_print)
# require 'ap'
#
# # Print information about any HTTP requests being made
# require 'net-http-spy'
#
# # Draw ASCII tables
# require 'hirb'
# require 'hirb/import_object'
# Hirb.enable
# extend Hirb::Console
#
#
# # Load the readline module.
# IRB.conf[:USE_READLINE] = true
#
# # Remove the annoying irb(main):001:0 and replace with >>
# # IRB.conf[:PROMPT_MODE] = :SIMPLE
#
# # Tab Completion
# require 'irb/completion'
#
# # Automatic Indentation
# IRB.conf[:AUTO_INDENT]=true
#
# # Save History between irb sessions
# # Setup permanent history.
# HISTFILE = "~/.irb_history"
# MAXHISTSIZE = 100
# begin
# histfile = File::expand_path(HISTFILE)
# if File::exists?(histfile)
# lines = IO::readlines(histfile).collect { |line| line.chomp }
# puts "Read #{lines.nitems} saved history commands from '#{histfile}'." if $VERBOSE
# Readline::HISTORY.push(* lines)
# else
# puts "History file '#{histfile}' was empty or non-existant." if $VERBOSE
# end
# Kernel::at_exit do
# lines = Readline::HISTORY.to_a.reverse.uniq.reverse
# lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.nitems > MAXHISTSIZE
# puts "Saving #{lines.length} history lines to '#{histfile}'." if $VERBOSE
# File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
# end
# rescue => e
# puts "Error when configuring permanent history: #{e}" if $VERBOSE
# end
# # Wirble is a set of enhancements for irb
# # http://pablotron.org/software/wirble/README
# # Implies require 'pp', 'irb/completion', and 'rubygems'
# require 'wirble'
# Wirble.init
#
# # Enable colored output
# Wirble.colorize
#
# # Clear the screen
# def clear
# system 'clear'
# if ENV['RAILS_ENV']
# return "Rails environment: " + ENV['RAILS_ENV']
# else
# return "No rails environment - happy hacking!";
# end
# end
#
# # Shortcuts
# alias c clear
#
# # Load / reload files faster
# # http://www.themomorohoax.com/2009/03/27/irb-tip-load-files-faster
# def fl(file_name)
# file_name += '.rb' unless file_name =~ /\.rb/
# @@recent = file_name
# load "#{file_name}"
# end
#
# def rl
# fl(@@recent)
# end
#
# # Reload the file and try the last command again
# # http://www.themomorohoax.com/2009/04/07/ruby-irb-tip-try-again-faster
# def rt
# rl
# eval(choose_last_command)
# end
#
# # prevent 'rt' itself from recursing.
# def choose_last_command
# real_last = Readline::HISTORY.to_a[-2]
# real_last == 'rt' ? @@saved_last : (@@saved_last = real_last)
# end
#
# # Method to pretty-print object methods
# # Coded by sebastian delmont
# # http://snippets.dzone.com/posts/show/2916
# class Object
# ANSI_BOLD = "\033[1m"
# ANSI_RESET = "\033[0m"
# ANSI_LGRAY = "\033[0;37m"
# ANSI_GRAY = "\033[1;30m"
#
# # Print object's methods
# def pm(*options)
# methods = self.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 = self.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, 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_BOLD}#{item[0].to_s.rjust(max_name)}#{ANSI_RESET}"
# print "#{ANSI_GRAY}#{item[1].ljust(max_args)}#{ANSI_RESET}"
# print " #{ANSI_LGRAY}#{item[2]}#{ANSI_RESET}\n"
# end
# data.size
# end
# end
#
# # Log to STDOUT if in Rails
# if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
# require 'logger'
# RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
# end
#
# if defined?(Rails) && Rails.version > "2.9"
# ActiveRecord::Base.logger = Logger.new STDOUT
# end
#
# # Quick way to run just a few specific lines from a file
# def eval_lines(fn, lines)
# eval( File.readlines(fn)[lines].join)
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment