module Irbtools

require this file (instead of configure) to deactivate loading of default set of libraries

Constants

VERSION

Attributes

libraries[RW]

a hash of arrays of libraries that get loaded keys determine if lib is required, required on sub-session or autoloaded

minimal[RW]

set this to true before loading this file to deactivate loading of default libraries

shell_name[R]

shell name (usually irb), retrieved from $0

welcome_message[RW]

message to display when starting. Set to nil to disable

Public Class Methods

add_command_aliases() click to toggle source
# File lib/irbtools/implementation.rb, line 137
def add_command_aliases
  IRB.conf[:COMMAND_ALIASES] = (IRB.conf[:COMMAND_ALIASES] || {}).merge({
    :ri => :show_doc,
    :co => :chws,
    :'$' => :sys,
    :'+' => :shadow,
  })
end
add_library(lib, options = {}, &block) click to toggle source

add a library. the block gets executed, when the library was loaded. if the second param is true, it’s hooked in into IRB.conf instead of the start.

# File lib/irbtools/implementation.rb, line 24
def add_library(lib, options = {}, &block)
  lib = lib.to_s

  if constant = options[:autoload]
    lib_path = File.split( lib ); lib_path.delete('.')
    gem_name = lib_path[0] # assume that first dir in load dir is the gem name
    if constant.is_a?(Array)
      constant.each{ |single_constant|
        @libraries[:autoload] << [single_constant, lib, gem_name]
      }
    else
      @libraries[:autoload] << [constant, lib, gem_name]
    end
  elsif options[:after_rc]
    @libraries[:after_rc] << lib
  elsif which = options[:thread]
    @libraries[:thread][which] ||= []
    @libraries[:thread][which] << lib
  elsif options[:late]
    @libraries[:late] << lib
  elsif which = options[:late_thread]
    @libraries[:late_thread][which] ||= []
    @libraries[:late_thread][which] << lib
  else
    @libraries[:start] << lib
  end

  add_library_callback(lib, &block) if block_given?
end
add_library_callback(lib, &block) click to toggle source

add a callback that gets (usually) executed after loading the library

# File lib/irbtools/implementation.rb, line 55
def add_library_callback(lib, &block)
  lib = lib.to_s
  @lib_hooks[lib] << block
end
configure_irb!() click to toggle source
# File lib/irbtools/implementation.rb, line 104
def configure_irb!
  if defined?(IRB)
    IRB.conf[:AUTO_INDENT]  = true                 # simple auto indent
    IRB.conf[:EVAL_HISTORY] = 42424242424242424242 # creates the special __ variable
    IRB.conf[:SAVE_HISTORY] = 2000                 # how many lines will go to ~/.irb_history
    set_propmt
    load_commands
    add_command_aliases
    rename_ls_to_ils
  end
end
library_loaded(lib) click to toggle source

te be triggered when a library has loaded

# File lib/irbtools/implementation.rb, line 83
def library_loaded(lib)
  @lib_hooks[lib.to_s].each{ |hook| hook.call }
end
load_commands() click to toggle source
# File lib/irbtools/implementation.rb, line 129
def load_commands
  IRB::Command.register(:code, Irbtools::Command::Code)
  IRB::Command.register(:howtocall, Irbtools::Command::Howtocall)
  IRB::Command.register(:look, Irbtools::Command::Look)
  IRB::Command.register(:shadow, Irbtools::Command::Shadow)
  IRB::Command.register(:sys, Irbtools::Command::Sys)
end
load_libraries(libs) click to toggle source

actually load libraries

# File lib/irbtools/implementation.rb, line 88
def load_libraries(libs)
  remember_verbose_and_debug = $VERBOSE, $DEBUG
  $VERBOSE = $DEBUG = false

  libs.each{ |lib|
    begin
      require lib.to_s
      library_loaded(lib)
    rescue Exception => err
      warn "Error while loading a library into IRB:\n\n### #{err.class}\n" +
           err.message + "\n\n### STACKTRACE\n  " + err.backtrace*"\n  " + "\n\n\n"
    end
  }
  $VERBOSE, $DEBUG = remember_verbose_and_debug
end
remove_library(lib) click to toggle source

don’t load a specific library

# File lib/irbtools/implementation.rb, line 69
def remove_library(lib)
  lib = lib.to_s

  @libraries[:start].delete lib
  @libraries[:sub_session].delete lib
  @libraries[:autoload].reject!{|_,e,| e == lib }
  @libraries[:thread].each{ |_,libs| libs.delete lib }
  @libraries[:late].delete lib
  @libraries[:late_thread].each{ |_,libs| libs.delete lib }

  @lib_hooks.delete lib
end
rename_ls_to_ils() click to toggle source

prevent clash between IRB’s ls and FileUtil’s ls

# File lib/irbtools/implementation.rb, line 147
def rename_ls_to_ils
  if aliases = IRB::ExtendCommandBundle.instance_variable_get(:@ALIASES)
    if irb_ls = aliases.find{|a,*| a == :ls}
      irb_ls[0] = :ils
    end
  end
end
replace_library_callback(lib, &block) click to toggle source

replace all callbacks with the new one given in the block a callback that gets (usually) executed after loading the library

# File lib/irbtools/implementation.rb, line 62
def replace_library_callback(lib, &block)
  lib = lib.to_s
  @lib_hooks[lib].clear
  @lib_hooks[lib] << block
end
set_propmt() click to toggle source
# File lib/irbtools/implementation.rb, line 116
def set_propmt
  (IRB.conf[:PROMPT] ||= {} ).merge!( {:IRBTOOLS => {
    :PROMPT_I => ">> ",    # normal
    :PROMPT_N => "|  ",    # indenting
    :PROMPT_C => " > ",    # continuing a statement
    :PROMPT_S => "%l> ",   # continuing a string
    :RETURN   => "=> %s \n",
    :AUTO_INDENT => true,
  }})

  IRB.conf[:PROMPT_MODE] = :IRBTOOLS
end
start() click to toggle source

loads all the stuff

# File lib/irbtools/implementation.rb, line 156
def start
  require 'irbtools'
end