Module: Irbtools

Defined in:
lib/irbtools/minimal.rb,
lib/irbtools/version.rb,
lib/irbtools/implementation.rb

Overview

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

Constant Summary collapse

VERSION =
"4.0.9"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.librariesObject

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



20
21
22
# File 'lib/irbtools/implementation.rb', line 20

def libraries
  @libraries
end

.minimalObject

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



16
17
18
# File 'lib/irbtools/implementation.rb', line 16

def minimal
  @minimal
end

.shell_nameObject (readonly)

shell name (usually irb), retrieved from $0



13
14
15
# File 'lib/irbtools/implementation.rb', line 13

def shell_name
  @shell_name
end

.welcome_messageObject

message to display when starting. Set to nil to disable



10
11
12
# File 'lib/irbtools/implementation.rb', line 10

def welcome_message
  @welcome_message
end

Class Method Details

.add_command_aliasesObject



144
145
146
147
148
149
150
151
# File 'lib/irbtools/implementation.rb', line 144

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) ⇒ Object

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.



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
# 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) ⇒ Object

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



55
56
57
58
# File 'lib/irbtools/implementation.rb', line 55

def add_library_callback(lib, &block)
  lib = lib.to_s
  @lib_hooks[lib] << block
end

.configure_irb!Object



104
105
106
107
108
109
110
111
112
113
114
# 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) ⇒ Object

te be triggered when a library has loaded



83
84
85
# File 'lib/irbtools/implementation.rb', line 83

def library_loaded(lib)
  @lib_hooks[lib.to_s].each{ |hook| hook.call }
end

.load_commandsObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/irbtools/implementation.rb', line 129

def load_commands
  ec = IRB::ExtendCommandBundle.instance_variable_get(:@EXTEND_COMMANDS)

  [
    [:code, :Code, nil, [:code, IRB::ExtendCommandBundle::OVERRIDE_ALL]],
    [:howtocall, :Howtocall, nil, [:howtocall, IRB::ExtendCommandBundle::OVERRIDE_ALL]],
    [:look, :Look, nil, [:look, IRB::ExtendCommandBundle::OVERRIDE_ALL]],
    [:shadow, :Shadow, nil, [:shadow, IRB::ExtendCommandBundle::OVERRIDE_ALL]],
    [:sys, :Sys, nil, [:sys, IRB::ExtendCommandBundle::OVERRIDE_ALL]],
  ].each{ |ecconfig|
    ec.push(ecconfig)
    IRB::ExtendCommandBundle.def_extend_command(*ecconfig)
  }
end

.load_libraries(libs) ⇒ Object

actually load libraries



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ Object

don’t load a specific library



69
70
71
72
73
74
75
76
77
78
79
80
# 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_ilsObject

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



154
155
156
157
158
159
160
# File 'lib/irbtools/implementation.rb', line 154

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) ⇒ Object

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



62
63
64
65
66
# 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_propmtObject



116
117
118
119
120
121
122
123
124
125
126
127
# 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

.startObject

loads all the stuff



163
164
165
# File 'lib/irbtools/implementation.rb', line 163

def start
  require 'irbtools'
end