public
Last active

IRB Improvements

  • Download Gist
improved_irb.rb
Ruby
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
#!usr/bin/env ruby
 
require 'irb'
require 'irb/completion'
require 'rubygems'
require 'rawline'
require 'pathname'
require 'extlib'
 
# System extensions
 
class Object
def show
if self.is_a?(Enumerable) then
self.each do |e|
puts e
end
else
puts self.to_s
end
nil
end
end
 
class Pathname
alias old_to_s to_s
def to_s
case
when !self.exist? then
old_to_s.dark_red
when self.directory? then
old_to_s.dark_yellow
else
old_to_s.dark_green
end
end
end
 
class String
def path
Pathname.new self
end
def red; colorize(self, "\e[1;31m"); end
def dark_red; colorize(self, "\e[0;31m"); end
def green; colorize(self, "\e[1;32m"); end
def dark_green; colorize(self, "\e[0;32m"); end
def yellow; colorize(self, "\e[1;33m"); end
def dark_yellow; colorize(self, "\e[0;33m"); end
def blue; colorize(self, "\e[1;34m"); end
def dark_blue; colorize(self, "\e[0;34m"); end
def magenta; colorize(self, "\e[1;35m"); end
def dark_magenta; colorize(self, "\e[0;35m"); end
def cyan; colorize(self, "\e[1;36m"); end
def dark_cyan; colorize(self, "\e[0;36m"); end
def uncolorize; self.gsub!(/\e\[\d[;0-9]*m/, '') end
def colorize(text, color_code)
RawLine.ansi? ? "#{color_code}#{text}\e[0m" : text
end
end
 
# Rawline and Shell Configuration
 
Rawline.basic_word_break_characters= " \t\n\"\\'`><;|&{("
Rawline.completion_append_character = nil
Rawline.completion_proc = IRB::InputCompletor::CompletionProc
 
class RawlineInputMethod < IRB::ReadlineInputMethod
include Rawline
def gets
if l = readline(@prompt, false)
HISTORY.push(l) if !l.empty?
@line[@line_no += 1] = l + "\n"
else
@eof = true
l
end
end
end
 
IRB.conf[:SCRIPT] = RawlineInputMethod.new
 
# Predefined commands and variables
 
include FileUtils
 
@pwd = Pathname.new Dir.pwd
@home = ENV['HOME']
@root = Pathname.new '/'
 
def cd(string)
p = Pathname.new string
return "Path #{p} is invalid" unless p.exist?
Dir.chdir string
@pwd = p
end
 
def save
end
 
IRB.start

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.