public
Last active

.irbrc

  • Download Gist
.irbrc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
# 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

You forgot to define the colors (the ANSI constant)

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.