Created
February 10, 2009 22:55
-
-
Save radarek/61665 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim: ft=ruby | |
# This file is execute by IRB - Iteractive Ruby | |
# author: Radosław Bułat | |
begin | |
# CORE (should work for all ruby implementation and platforms) | |
require "rbconfig" | |
# FEATURE: Color output support | |
module StringColors | |
COLORS = { | |
:gray => "\e[1m\e[30m", | |
:dark_gray => "\e[30m", | |
:red => "\e[1m\e[31m", | |
:dark_red => "\e[31m", | |
:green => "\e[1m\e[32m", | |
:dark_green => "\e[32m", | |
:yellow => "\e[1m\e[33m", | |
:dark_yellow => "\e[33m", | |
:blue => "\e[1m\e[34m", | |
:dark_blue => "\e[34m", | |
:pink => "\e[1m\e[35m", | |
:dark_pink => "\e[35m", | |
:cyan => "\e[1m\e[36m", | |
:dark_cyan => "\e[36m", | |
:white => "\e[1m\e[37m", | |
:silver => "\e[37m", | |
} | |
colors_supported = true | |
if RbConfig::CONFIG['host_os'] =~ /mswin|windows/ | |
begin | |
require "win32console" | |
rescue LoadError | |
colors_supported = false | |
puts "[inf] run gem install win32console to have colorful messages" | |
end | |
end | |
if colors_supported | |
COLORS.each_pair do |color_name, color_value| | |
define_method(color_name) do | |
return "#{color_value}#{self}\e[0m" | |
end | |
end | |
else | |
COLORS.each_pair do |color_name, color_value| | |
define_method(color_name) do | |
return self | |
end | |
end | |
end | |
end | |
String.send(:include, StringColors) | |
IRB_FEATURES = {} | |
def irb_feature(feature_name, &block) | |
yield | |
IRB_FEATURES[feature_name] = true | |
rescue LoadError => e | |
gem_name = e.message[/no such file to load -- (.*)/, 1] | |
puts "[err] ".red + "loading feature #{feature_name.to_s.dark_blue} (err: #{e})" | |
puts "[inf] ".blue + "sudo gem install #{gem_name} ".blue + "will probably help" | |
rescue Exception => e | |
puts "[err] ".red + "loading feature #{feature_name.to_s.blue} (err: #{e})" | |
end | |
system("clear") | |
puts "[info] .irbrc is beeing loaded...".green | |
if $PROGRAM_NAME && $PROGRAM_NAME !~ /irb/ | |
$stderr.puts "[error] .irbrc is probably being read from outside irb ($PROGRAM_NAME = #{$PROGRAM_NAME})".red | |
end | |
irb_feature :core_features do | |
#ARGV.concat ["--readline", "--prompt-mode", "simple" ] | |
#ARGV.concat ["--prompt-mode", "simple" ] | |
require 'pp' | |
require 'irb' | |
require 'irb/completion' | |
require 'irb/ext/save-history' | |
IRB.conf[:IRB_RC] = nil | |
IRB.conf[:SAVE_HISTORY] = 1000 | |
IRB.conf[:AUTO_INDENT] = true | |
IRB.conf[:USE_READLINE] = true | |
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history" | |
IRB.conf[:PROMPT_MODE] = :SIMPLE | |
puts "ruby => #{RUBY_VERSION}" | |
end | |
irb_feature :minigems do | |
require 'minigems' | |
end | |
irb_feature :rubygems do | |
unless IRB_FEATURES[:minigems] | |
require "rubygems" | |
end | |
end | |
# After quit from IRB it prints how much time passed since start. | |
irb_feature :irb_access_time do | |
require "duration" | |
irb_start_time = Time.now | |
at_exit do | |
puts "Goodbye!" | |
puts "Your session time: " + Duration.new(Time.now - irb_start_time).to_s.green | |
end | |
end | |
# what? for Ruby | |
# for background see: http://redhanded.hobix.com/inspect/stickItInYourIrbrcMethodfinder.html | |
# and the original MethodFinder: http://www.nobugs.org/developer/ruby/method_finder.html | |
# Example: | |
# >> "ewq".what?(3) | |
# "ewq".length == 3 | |
# "ewq".size == 3 | |
# "ewq".bytesize == 3 | |
# => ["length", "size", "bytesize"] | |
irb_feature :what_methods do | |
require "what_methods" | |
class WhatMethods::MethodFinder | |
@@blacklist.concat %w(breakpoint debugger) | |
end | |
end | |
# Try to find source location of a method. It should be called with a block, where | |
# given method is called. | |
# Example: | |
# where { | |
irb_feature :where do | |
module Kernel | |
def where(&block) | |
raise ArgumentError, "no block given" unless block_given? | |
result = "Not found, probably native method" | |
found = false | |
trace_func = lambda do |event, file, line, id, binding, classname| | |
if event == "call" && !found | |
found = true | |
result = "#{file}:#{line}" | |
end | |
end | |
begin | |
Kernel.set_trace_func(trace_func) | |
yield | |
ensure | |
Kernel.set_trace_func(nil) | |
end | |
return result | |
end | |
end | |
end | |
# Colorize irb output | |
irb_feature :wirble do | |
require "wirble" | |
Wirble.init | |
Wirble.colorize | |
end | |
# NOTICE: It should be loaded AFTER wirble, otherwise it doesn't work (wirble override some IRB method) | |
irb_feature :irb_callbacks do | |
require "irb_callbacks" | |
require "benchmark" | |
module IRB | |
class << self | |
def around_eval(&block) | |
@timing = Benchmark.realtime do | |
block.call | |
end | |
end | |
def after_output | |
puts "=> #{'%.3f' % @timing} seconds :(" if @timing > 1 | |
end | |
end | |
end | |
end | |
irb_feature :extra_features do | |
unless Symbol.method_defined?(:to_proc) | |
class Symbol | |
def to_proc | |
return Proc.new {|*args| args.shift.send(self, *args) } | |
end | |
end | |
end | |
def ri(*names) | |
system("ri %s" % names.map(&:to_s).join(" ")) | |
end | |
def qri(*names) | |
system("qri %s" % names.map(&:to_s).join(" ")) | |
end | |
class Object | |
def metaclass | |
class<<self;self;end | |
end | |
end | |
end | |
# Load .railsrc when script/console is running. | |
irb_feature :railsrc do | |
if ENV["RAILS_ENV"] | |
load File.dirname(__FILE__) + "/.railsrc" | |
end | |
end | |
irb_feature :ascii_logo do | |
Dir.chdir("/home/radarek/work/bundle/xterm-256colors/") do | |
#system("ruby image2ascii.rb images/ruby-logo-R-crop.png 30") | |
#system("ruby1.9 image2ascii.rb images/ruby.png 80") | |
end | |
#text = "Interactive Ruby" | |
#system("figlet -f bigmono9 -F metal -t -s '#{INTRO_TEXT}'") | |
end | |
# Extended IRB history. | |
# Adds shell-style history display and replay to irb. The magic happens in | |
# the h, h!, and hw methods. | |
# http://blog.bleything.net/pages/irb_history | |
irb_feature :extended_irb_history do | |
# Lists the last how_many lines of history (defaults to 50). Aliased to h. | |
def history( how_many = 50 ) | |
history_size = Readline::HISTORY.size | |
# no lines, get out of here | |
puts "No history" and return if history_size == 0 | |
start_index = 0 | |
# not enough lines, only show what we have | |
if history_size <= how_many | |
how_many = history_size - 1 | |
end_index = how_many | |
else | |
end_index = history_size - 1 # -1 to adjust for array offset | |
start_index = end_index - how_many | |
end | |
start_index.upto( end_index ) {|i| print_line i} | |
nil | |
end | |
alias :h :history | |
# replay lines from history. Aliased to h! | |
# | |
# h! by itself will replay the most recent line. You can also pass in a | |
# range, array, or any mixture of the three. | |
# | |
# We subtract 2 from HISTORY.size because -1 is the command we just issued. | |
def history_do( *lines ) | |
lines = [Readline::HISTORY.size - 2] if lines.empty? | |
to_eval = get_lines( lines ) | |
to_eval.each {|l| Readline::HISTORY << l} | |
IRB.CurrentContext.workspace.evaluate self, to_eval.join(';') | |
end | |
alias :h! :history_do | |
# writes history to a named file. This is useful if you want to show somebody | |
# something you did in irb, or for rapid prototyping. Aliased to hw. | |
# | |
# Uses similar calling semantics to h!, that is, you can pass in fixnums, | |
# ranges, arrays, or any combination thereof. | |
def history_write( filename, *lines ) | |
File.open( filename, 'w' ) do |f| | |
get_lines( lines ).each do |l| | |
f.puts l | |
end | |
end | |
end | |
alias :hw :history_write | |
private | |
# simple getter to fetch from Readline | |
def get_line(line_number) | |
Readline::HISTORY[line_number] | |
end | |
# the code what powers the line fetcherating. Accepts an array and iterates | |
# over each entry, fetching that line from the history and placing it into a | |
# temporary array which is ultimately returned. | |
def get_lines( lines ) | |
out = [] | |
lines.each do |line| | |
case line | |
when Fixnum | |
out << get_line( line ) | |
when Range | |
line.to_a.each do |l| | |
out << get_line( l ) | |
end | |
end | |
end | |
return out | |
end | |
# prints out the contents of the line from history, along with a line number, | |
# if desired. | |
def print_line(line_number, show_line_numbers = true) | |
print "[%04d] " % line_number if show_line_numbers | |
puts get_line(line_number) | |
end | |
end | |
puts "#{ENV['USER']}, welcome!" | |
rescue Exception => e | |
require "pp" | |
puts "Error: #{e.message}" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim: ft=ruby | |
require 'logger' | |
# http://errtheblog.com/posts/41-real-console-helpers | |
def Object.method_added(method) | |
return super(method) unless method == :helper | |
(class<<self;self;end).send(:remove_method, :method_added) | |
def helper(*helper_names) | |
returning $helper_proxy ||= Object.new do |helper| | |
helper_names.each { |h| helper.extend "#{h}_helper".classify.constantize } | |
end | |
end | |
helper.instance_variable_set("@controller", ActionController::Integration::Session.new) | |
def helper.method_missing(method, *args, &block) | |
@controller.send(method, *args, &block) if @controller && method.to_s =~ /_path$|_url$/ | |
end | |
helper :application rescue nil | |
end | |
Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment