Skip to content

Instantly share code, notes, and snippets.

@h3rald
Created April 26, 2009 10:00
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 h3rald/101987 to your computer and use it in GitHub Desktop.
Save h3rald/101987 to your computer and use it in GitHub Desktop.
IRB Improvements
#!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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment