Skip to content

Instantly share code, notes, and snippets.

@softwaregravy
Created March 11, 2012 23:29
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 softwaregravy/2018672 to your computer and use it in GitHub Desktop.
Save softwaregravy/2018672 to your computer and use it in GitHub Desktop.
Override IRB Prompt
require 'irb'
module IRB
class << self
alias :orig_init_config :init_config
def init_config(ap_path)
begin
puts "loading init config: #{Rails.env}"
# Set up the prompt to be slightly more informative
rails_env = Rails.env
current_app = Rails.application.class.parent_name
# a string we can sub the 2nd to last character depending on the context
prompt_string_root = "#{current_app}(#{rails_env})%%3n%c> "
# calculate manually since we don't count the trailing '> ' and %100n will be 3 char
normal_prompt_string_length = current_app.length + 1 + rails_env.length + 1 + 3
empty_string_root = "#{' ' * normal_prompt_string_length}%c> "
# http://tagaholic.me/2009/05/29/exploring-how-to-configure-irb.html#prompt
prompt_config = {
# Normal prompt (%zn – Line number with optional number z for printf width)
:PROMPT_I => sprintf(prompt_string_root, '>'),
# indent prompt
:PROMPT_N => sprintf(empty_string_root, ' '),
# string continue prompt
:PROMPT_S => sprintf(empty_string_root, '"'),
# statement continue prompt
:PROMPT_C => sprintf(empty_string_root, '?'),
# prefix output (leaves a space between the side effect ouput and return val)
:RETURN => "\n=> %s\n"
}
#actaully do the init
orig_init_config(ap_path)
# and override with our preferred prompt
@CONF[:PROMPT].reverse_merge!(:RAILS_ENV => prompt_config)
@CONF[:PROMPT_MODE] = :RAILS_ENV
rescue Exception => e
puts "Error loading IRB PROMPT", e
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment