Last active
August 29, 2015 14:03
-
-
Save rabbitt/7a80de03a80e339e3a7a 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
# sublime: x_syntax Packages/Ruby/Ruby.tmLanguage | |
# sublime: translate_tabs_to_spaces false; tab_size 2 | |
IRB.conf[:IGNORE_SIGINT] = true | |
IRB.conf[:USE_READLINE] = true | |
IRB.conf[:PROMPT_MODE] = :CLASSIC | |
def inject_path(path, where = :before) | |
require 'pathname' | |
path = Pathname.new(path).realpath.to_s | |
case where | |
when :before then $:.unshift(path) | |
when :after then $:.push(path) | |
end unless $:.include?(path); | |
end | |
def current_context | |
IRB.CurrentContext | |
end | |
def trace!() | |
current_context.use_tracer = !tracing? | |
end | |
def tracing?() | |
current_context.use_tracer rescue false | |
end | |
def require_lib(lib, install_name = nil) | |
install_name ||= lib | |
begin | |
require lib | |
rescue LoadError => e | |
require 'rubygems/dependency_installer' | |
require 'rubygems/dependency' | |
require 'rubygems/commands/install_command' | |
options = Gem::DependencyInstaller::DEFAULT_OPTIONS.merge({ | |
:generate_rdoc => false, | |
:generate_ri => false, | |
:format_executable => false, | |
:version => Gem::Requirement.default, | |
}) | |
gem_name = install_name | |
begin | |
return if options[:conservative] and | |
not Gem::Dependency.new(gem_name, options[:version]).matching_specs.empty? | |
inst = Gem::DependencyInstaller.new options | |
inst.install gem_name, options[:version] | |
inst.installed_gems.each do |spec| | |
puts "Successfully installed #{spec.full_name}" | |
end | |
Gem.refresh | |
retry | |
rescue Gem::InstallError, Gem::GemNotFoundException => e | |
puts "Error auto-installing #{gem_name}:\n\t#{e.message}\n\nPlease try installing 'ruby-ip' manually: gem install ruby-ip\n" | |
exit 1 | |
end | |
end | |
end | |
def load_lib library | |
library = library.to_s | |
begin | |
require library | |
yield if block_given? | |
rescue LoadError => err | |
warn "Couldn't load library '#{library}'" | |
else | |
warn "Library '#{library}' loaded successfully." | |
end | |
end | |
load_lib :pathname do | |
class << $: | |
def prepend(path) | |
path = Pathname.new(path).realpath.to_s | |
unshift path unless include? path | |
end | |
def append(path) | |
path = Pathname.new(path).realpath.to_s | |
push path unless include? path | |
end | |
end | |
end | |
load_lib :awesome_print do | |
IRB::Irb.class_eval do | |
def output_value | |
ap(@context.last_value, :multiline => false) | |
end | |
end | |
end | |
load_lib :hirb do | |
self.extend Hirb::Console | |
Hirb::View.enable | |
end if ENV['SKIP_HIRB'].nil? | |
load_lib 'irb/completion' do | |
require 'irb/ext/save-history' | |
require 'irb/ext/history' | |
begin | |
require 'io/console' | |
def console_height() IO.console.winsize.first - 2; end | |
rescue LoadError | |
def console_height() 25; end | |
end | |
IRB.conf[:SAVE_HISTORY] = 5000 | |
def _history | |
Readline::HISTORY.to_a | |
end | |
def history(options = {}) | |
limit = options.delete(:limit) || -1 | |
reversed = case (order = options.delete(:order)).to_s | |
when /^asc/ then false | |
when /^desc/ then true | |
else true | |
end | |
indexed_history = _history.each_with_index.collect {|ln, idx| [idx, ln ] } | |
indexed_history = indexed_history.reverse[0..limit] | |
indexed_history.reverse! if reversed | |
max_idx_size = indexed_history.sort { |a,b| a[0] <=> b[0] }.last[0].to_s.size | |
indexed_history.reverse.each_slice(console_height).each do |screen_data| | |
puts "Showing entries #{screen_data.first[0]} to #{screen_data.last[0]}:" | |
screen_data.each do |idx, line| | |
puts " %#{max_idx_size}d: %s\n" % [ idx, line] | |
end | |
print "press any key to continue" | |
begin | |
raise Interrupt if $stdin.getch == "\u0003" | |
rescue Interrupt, IRB::Abort | |
puts | |
break | |
end | |
end | |
nil | |
end | |
module Readline | |
alias_method :original_readline, :readline unless respond_to? :original_readline | |
def readline(*args) | |
begin | |
# bash style, basic history expansion | |
history_line = case current_line = original_readline(*args) | |
when '!!' then | |
_history[-2] | |
when /^\!(-?[0-9]+)$/ then | |
_history[$1.to_i.abs] | |
when /^\!\?(.+)/ then | |
regexp = Regexp.new(Regexp.escape($1)).freeze | |
_history.select{ |line| line =~ regexp }.last | |
when /^\!\^([^\^]+)\^([^\^]+)\^/ then | |
search, replace = $1, $2 | |
search = Regexp.new(Regexp.escape($1)).freeze | |
line = _history.select{|line| line =~ search}.last | |
line.gsub(search, replace) if line | |
else | |
current_line | |
end | |
puts "> #{history_line}" unless history_line == current_line | |
history_line | |
rescue RubyLex::TerminateLineInput | |
raise | |
rescue StandardError => e | |
puts "Exception caught: #{e.class.name}: #{e.message}:\n\t#{e.backtrace.join("\n\t")}" | |
end | |
end | |
end | |
end | |
load_lib :utility_belt do | |
UtilityBelt.equip :all, :except => %w( | |
rails_finder_shortcut | |
rails_verbosity_control | |
amazon_upload_shortcut | |
wirble | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment