Skip to content

Instantly share code, notes, and snippets.

@rogerleite
Created January 13, 2010 15:53
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 rogerleite/276299 to your computer and use it in GitHub Desktop.
Save rogerleite/276299 to your computer and use it in GitHub Desktop.
.irbrc
# This irb script enables:
# - irb completion. (Ex: "".[tab] show available methods)
# - commands history. Up and Down arrows to navigate at commands.
# - requires pretty print.
# - if "awesome print" gem is available, replace pp command for ap.
# - if RAILS_ENV exists, set irb console as logger. (I can see queries from ActiveRecord)
require 'irb/completion'
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
module Readline
module History
LOG = "#{ENV['HOME']}/.irb-history"
def self.write_log(line)
File.open(LOG, 'ab') {|f| f << "#{line}\n"}
end
def self.load_history
lines = File.read(LOG)
lines.to_a.each do |line|
line.strip!
HISTORY << line unless (line.empty? or line.start_with?("#"))
end
end
def self.start_session_log
load_history
write_log("\n# session start: #{Time.now}\n#\n")
at_exit do
write_log("\n# session stop: #{Time.now}\n")
end
end
end
alias :old_readline :readline
def readline(*args)
ln = old_readline(*args)
begin
History.write_log(ln)
rescue
end
ln
end
end
Readline::History.start_session_log
require "pp" #pretty print
begin
require "rubygems"
require "ap"
puts "Loaded awesome print!"
alias old_pp pp
alias pp ap
rescue LoadError => ex
puts "#{ex.message}\n"
puts "you can do \"gem install awesome_print\" if want! ;)"
end
#show Rails log
require "logger"
if ENV.include?('RAILS_ENV')&& !Object.const_defined?('RAILS_DEFAULT_LOGGER')
Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT))
end
def ansi_values
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"
ansi
end
# When you're using Rails 3 console, show queries in the console
# Thanks Iain Hecker! http://gist.github.com/368237
if defined?(ActiveSupport::Notifications)
ansi = ansi_values
$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
@iain
Copy link

iain commented May 5, 2010

You forgot to define the colors (the ANSI constant)

@rogerleite
Copy link
Author

Thanks again!
I didn't have time to test Rails 3! :X

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment