irbtools 1.7.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/irbtools.rb CHANGED
@@ -1,118 +1,66 @@
1
- # encoding: utf-8
2
-
3
- if defined?(IRB) || defined?(Ripl)
4
- # # # # #
5
- # require 'irbtools' in your .irbrc
6
- # see the README file for more information
7
- require_relative 'irbtools/configure' unless defined? Irbtools
8
-
9
- # # # # #
10
- # load extension packages
11
- Irbtools.packages.each{ |pkg|
12
- begin
13
- require "irbtools/#{pkg}"
14
-
15
- rescue LoadError => err
16
- warn "Couldn't load the extension package '#{pkg}' #{err.class}\n* " +
17
- err.message + "\n* " + err.backtrace[0] + "\n"
18
- end
19
- }
20
-
21
- # # # # #
22
- # loading helper proc
23
- load_libraries_proc = proc{ |libs|
24
- remember_verbose_and_debug = $VERBOSE, $DEBUG
25
- $VERBOSE = $DEBUG = false
26
-
27
- libs.each{ |lib|
28
- begin
29
- require lib.to_s
30
- Irbtools.send :library_loaded, lib
31
- rescue Exception => err
32
- warn "Couldn't load the irb library '#{lib}': #{err.class}\n* " +
33
- err.message + "\n* " + err.backtrace[0] + "\n"
34
- end
35
- }
36
- $VERBOSE, $DEBUG = remember_verbose_and_debug
37
- }
38
-
39
- # # # # #
40
- # load: start
41
- load_libraries_proc[ Irbtools.libraries[:start] ]
42
-
43
- # # # # #
44
- # load: autoload
45
- Irbtools.libraries[:autoload].each{ |constant, lib_name, gem_name|
46
- gem gem_name
47
- autoload constant, lib_name
48
- Irbtools.send :library_loaded, lib_name
49
- }
50
-
51
- # # # # #
52
- # irb options
53
- unless defined?(Ripl) && Ripl.respond_to?(:started?) && Ripl.started?
54
- IRB.conf[:AUTO_INDENT] = true # simple auto indent
55
- IRB.conf[:EVAL_HISTORY] = 42424242424242424242 # creates the special __ variable
56
- IRB.conf[:SAVE_HISTORY] = 2000 # how many lines will go to ~/.irb_history
57
-
58
- # prompt
59
- (IRB.conf[:PROMPT] ||= {} ).merge!( {:IRBTOOLS => {
60
- :PROMPT_I => ">> ", # normal
61
- :PROMPT_N => "| ", # indenting
62
- :PROMPT_C => " > ", # continuing a statement
63
- :PROMPT_S => "%l> ", # continuing a string
64
- :RETURN => "=> %s \n",
65
- :AUTO_INDENT => true,
66
- }})
67
-
68
- IRB.conf[:PROMPT_MODE] = :IRBTOOLS
1
+ require_relative 'irbtools/configure'
2
+
3
+ # # # # #
4
+ # Load irbtools extension packages
5
+ Irbtools.load_packages
6
+
7
+ # # # # #
8
+ # Load: start
9
+ Irbtools.load_libraries(Irbtools.libraries[:start])
10
+
11
+ # # # # #
12
+ # Load: autoload
13
+ Irbtools.libraries[:autoload].each{ |constant, lib_name, gem_name|
14
+ gem(gem_name)
15
+ autoload(constant, lib_name)
16
+ Irbtools.library_loaded(lib_name)
17
+ }
18
+
19
+ # # # # #
20
+ # Misc: Apply IRB options
21
+ Irbtools.configure_irb!
22
+
23
+ # # # # #
24
+ # Load: sub-session / after_rc
25
+ if Irbtools.ripl?
26
+ if defined? Ripl::AfterRc
27
+ Irbtools.libraries[:sub_session].each{ |r| Ripl.after_rcs << r }
28
+ elsif !Irbtools.libraries[:sub_session].empty?
29
+ warn "Couldn't load libraries in Irbtools.libraries[:sub_session]. Please install ripl-after_rc to use this feature in Ripl!"
69
30
  end
31
+ else
32
+ original_irbrc_proc = IRB.conf[:IRB_RC]
33
+ IRB.conf[:IRB_RC] = proc{
34
+ Irbtools.load_libraries(Irbtools.libraries[:sub_session])
35
+ original_irbrc_proc.call if original_irbrc_proc
36
+ }
37
+ end
70
38
 
71
- # # # # #
72
- # misc: add current directory to the load path
73
- $: << '.' if RUBY_VERSION >= '1.9.2'
74
-
75
- # # # # #
76
- # load: sub-session / after_rc
77
- if defined?(Ripl) && Ripl.respond_to?(:started?) && Ripl.started?
78
- if defined? Ripl::AfterRc
79
- Irbtools.libraries[:sub_session].each{ |r| Ripl.after_rcs << r }
80
- elsif !Irbtools.libraries[:sub_session].empty?
81
- warn "Couldn't load libraries in Irbtools.libraries[:sub_session]. Please install ripl-after_rc to use this feature in Ripl!"
82
- end
83
- else
84
- original_irbrc_proc = IRB.conf[:IRB_RC]
85
- IRB.conf[:IRB_RC] = proc{
86
- load_libraries_proc[ Irbtools.libraries[:sub_session] ]
87
- original_irbrc_proc[ ] if original_irbrc_proc
88
- }
39
+ # # # # #
40
+ # Load: threads
41
+ threads = []
42
+ Irbtools.libraries[:thread].each{ |_,libs|
43
+ threads << Thread.new do
44
+ Irbtools.load_libraries(libs)
89
45
  end
46
+ }
90
47
 
91
- # # # # #
92
- # load: threads
93
- Irbtools.libraries[:thread].each{ |_,libs|
94
- Thread.new do
95
- load_libraries_proc[ libs ]
96
- end
97
- }
98
-
99
- # # # # #
100
- # load: late
101
- load_libraries_proc[ Irbtools.libraries[:late] ]
48
+ threads.map(&:join)
102
49
 
103
- # # # # #
104
- # load: late_threads
105
- Irbtools.libraries[:late_thread].each{ |_,libs|
106
- Thread.new do
107
- load_libraries_proc[ libs ]
108
- end
109
- }
50
+ # # # # #
51
+ # Load: late
52
+ Irbtools.load_libraries(Irbtools.libraries[:late])
110
53
 
111
- # # # # #
112
- # done :)
113
- if msg = Irbtools.welcome_message
114
- puts msg
54
+ # # # # #
55
+ # Load: late_threads
56
+ Irbtools.libraries[:late_thread].each{ |_,libs|
57
+ Thread.new do
58
+ Irbtools.load_libraries(libs)
115
59
  end
116
- end
60
+ }
117
61
 
118
- # J-_-L
62
+ # # # # #
63
+ # Done
64
+ if msg = Irbtools.welcome_message
65
+ puts msg
66
+ end
@@ -0,0 +1,19 @@
1
+ require 'binding.repl'
2
+ BindingRepl.auto = %w[irb ripl ir rib pry]
3
+
4
+ class Binding
5
+ alias irb repl!
6
+ end
7
+
8
+ begin
9
+ require 'binding_of_caller'
10
+ rescue LoadError
11
+ end
12
+
13
+ if defined? BindingOfCaller
14
+ require 'debugging/repl'
15
+
16
+ module Debugging
17
+ alias irb repl
18
+ end
19
+ end
@@ -1,137 +1,4 @@
1
- # encoding: utf-8
2
-
3
- # # # # #
4
- # require 'irbtools' in your .irbrc
5
- # but you could also require 'irbtools/configure' and then call Irbtools.init to modify the loaded libraries
6
- # see the README file for more information
7
-
8
- # # # # #
9
- # define module methods
10
- module Irbtools
11
- VERSION = "1.7.1"
12
-
13
- @libraries = { :start => [], :sub_session => [], :autoload => [], :thread => {}, :late => [], :late_thread => {} }
14
- @lib_hooks = Hash.new{|h,k| h[k] = [] }
15
- @packages = []
16
- @shell_name = File.split($0)[-1].upcase
17
- @welcome_message = "Welcome to #{ @shell_name }. You are using #{ RUBY_DESCRIPTION }. Have fun ;)"
18
- @minimal ||= false
19
-
20
- class << self
21
- # message to display when starting. Set to nil to disable
22
- attr_accessor :welcome_message
23
-
24
- # shell name (usually irb), retrieved from $0
25
- attr_reader :shell_name
26
-
27
- # set this to true before loading this file to deactivate loading of default libraries
28
- attr_accessor :minimal
29
-
30
- # a hash of arrays of libraries that get loaded
31
- # keys determine if lib is required, required on sub-session or autoloaded
32
- attr_accessor :libraries
33
- alias libs libraries
34
- alias libs= libraries=
35
- alias gems libraries
36
- alias gems= libraries=
37
-
38
- # an array of extension packages that get loaded (e.g. irbtools-more)
39
- attr_accessor :packages
40
-
41
- # add a library. the block gets executed, when the library was loaded.
42
- # if the second param is true, it's hooked in into IRB.conf[:IRB_RC] instead of the start.
43
- def add_library(lib, options = {}, &block)
44
- lib = lib.to_s
45
-
46
- if constant = options[:autoload]
47
- lib_path = File.split( lib ); lib_path.delete('.')
48
- gem_name = lib_path[0] # assume that first dir in load dir is the gem name
49
- if constant.is_a?(Array)
50
- constant.each{ |single_constant|
51
- @libraries[:autoload] << [single_constant, lib, gem_name]
52
- }
53
- else
54
- @libraries[:autoload] << [constant, lib, gem_name]
55
- end
56
- elsif options[:after_rc]
57
- @libraries[:after_rc] << lib
58
- elsif which = options[:thread]
59
- @libraries[:thread][which] ||= []
60
- @libraries[:thread][which] << lib
61
- elsif options[:late]
62
- @libraries[:late] << lib
63
- elsif which = options[:late_thread]
64
- @libraries[:late_thread][which] ||= []
65
- @libraries[:late_thread][which] << lib
66
- else
67
- @libraries[:start] << lib
68
- end
69
-
70
- add_library_callback(lib, &block) if block_given?
71
- end
72
- alias add_lib add_library
73
- alias add_gem add_library
74
-
75
- # add a callback that gets (usually) executed after loading the library
76
- def add_library_callback(lib, &block)
77
- lib = lib.to_s
78
- @lib_hooks[lib] << block
79
- end
80
- alias add_lib_callback add_library_callback
81
- alias add_gem_callback add_library_callback
82
-
83
- # replace all callbacks with the new one given in the block
84
- # a callback that gets (usually) executed after loading the library
85
- def replace_library_callback(lib, &block)
86
- lib = lib.to_s
87
- @lib_hooks[lib].clear
88
- @lib_hooks[lib] << block
89
- end
90
- alias replace_lib_callback replace_library_callback
91
- alias replace_gem_callback replace_library_callback
92
-
93
- # don't load a specific library
94
- def remove_library(lib)
95
- lib = lib.to_s
96
-
97
- @libraries[:start].delete lib
98
- @libraries[:sub_session].delete lib
99
- @libraries[:autoload].reject!{|_,e,| e == lib }
100
- @libraries[:thread].each{ |_,libs| libs.delete lib }
101
- @libraries[:late].delete lib
102
- @libraries[:late_thread].each{ |_,libs| libs.delete lib }
103
-
104
- @lib_hooks.delete lib
105
- end
106
- alias remove_lib remove_library
107
- alias remove_gem remove_library
108
-
109
- # add extensions packages
110
- def add_package(pkg)
111
- @packages << pkg.to_s
112
- end
113
-
114
- # remove extension package
115
- def remove_package(pkg)
116
- @packages.delete pkg.to_s
117
- end
118
-
119
- def library_loaded(lib) #:nodoc:
120
- @lib_hooks[lib.to_s].each{ |hook| hook.call }
121
- end
122
- private :library_loaded
123
-
124
- # loads all the stuff ;)
125
- def init
126
- require File.expand_path( '../irbtools.rb', File.dirname(__FILE__) )
127
- end
128
- alias start init
129
- end
130
- end
131
-
132
- # # # # #
133
- # libraries
134
- require File.expand_path( 'libraries.rb', File.dirname(__FILE__) ) unless Irbtools.minimal
135
-
136
- # J-_-L
1
+ require_relative 'version'
137
2
 
3
+ require_relative 'implementation'
4
+ require_relative 'libraries' unless Irbtools.minimal
@@ -0,0 +1,46 @@
1
+ Irbtools.add_library :hirb, thread: :paint do
2
+ Hirb::View.enable output: { "Object" => { ancestor: true, options: { unicode: true }}},
3
+ pager_command: 'less -R'
4
+ extend Hirb::Console
5
+
6
+ def page(what, options = {})
7
+ Hirb::Pager.command_pager(what, options = {})
8
+ end
9
+
10
+ # page wirb output hacks
11
+ class Hirb::Pager
12
+ alias original_activated_by? activated_by?
13
+ def activated_by?(string_to_page, inspect_mode=false)
14
+ original_activated_by?(Paint.unpaint(string_to_page || ''), inspect_mode)
15
+ end
16
+ end
17
+
18
+ class << Hirb::View
19
+ def view_or_page_output(val)
20
+ if defined?(val.inspect)
21
+ unless view_output(val)
22
+ if defined?(Wirb) && Wirb.running?
23
+ page_output Wirb.colorize_result_with_timeout(val.inspect), true
24
+ else
25
+ page_output val.inspect, true
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ # colorful border
33
+ table_color = :yellow
34
+ Hirb::Helpers::UnicodeTable::CHARS.each do |place, group|
35
+ Hirb::Helpers::UnicodeTable::CHARS[place] =
36
+ group.each do |name, part|
37
+ if part.kind_of? String
38
+ Hirb::Helpers::UnicodeTable::CHARS[place][name] = Paint[part, *table_color]
39
+ elsif part.kind_of? Hash
40
+ part.each do |special, char|
41
+ Hirb::Helpers::UnicodeTable::CHARS[place][name][special] = Paint[char, *table_color]
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,156 @@
1
+ module Irbtools
2
+ @libraries = { :start => [], :sub_session => [], :autoload => [], :thread => {}, :late => [], :late_thread => {} }
3
+ @lib_hooks = Hash.new{|h,k| h[k] = [] }
4
+ @packages = []
5
+ @shell_name = File.split($0)[-1].upcase
6
+ @welcome_message = "Welcome to #{ @shell_name }. You are using #{ RUBY_DESCRIPTION }. Have fun ;)"
7
+ @minimal ||= false
8
+
9
+ class << self
10
+ # message to display when starting. Set to nil to disable
11
+ attr_accessor :welcome_message
12
+
13
+ # shell name (usually irb), retrieved from $0
14
+ attr_reader :shell_name
15
+
16
+ # set this to true before loading this file to deactivate loading of default libraries
17
+ attr_accessor :minimal
18
+
19
+ # a hash of arrays of libraries that get loaded
20
+ # keys determine if lib is required, required on sub-session or autoloaded
21
+ attr_accessor :libraries
22
+
23
+ # an array of extension packages that get loaded (e.g. irbtools-more)
24
+ attr_accessor :packages
25
+
26
+ # add a library. the block gets executed, when the library was loaded.
27
+ # if the second param is true, it's hooked in into IRB.conf[:IRB_RC] instead of the start.
28
+ def add_library(lib, options = {}, &block)
29
+ lib = lib.to_s
30
+
31
+ if constant = options[:autoload]
32
+ lib_path = File.split( lib ); lib_path.delete('.')
33
+ gem_name = lib_path[0] # assume that first dir in load dir is the gem name
34
+ if constant.is_a?(Array)
35
+ constant.each{ |single_constant|
36
+ @libraries[:autoload] << [single_constant, lib, gem_name]
37
+ }
38
+ else
39
+ @libraries[:autoload] << [constant, lib, gem_name]
40
+ end
41
+ elsif options[:after_rc]
42
+ @libraries[:after_rc] << lib
43
+ elsif which = options[:thread]
44
+ @libraries[:thread][which] ||= []
45
+ @libraries[:thread][which] << lib
46
+ elsif options[:late]
47
+ @libraries[:late] << lib
48
+ elsif which = options[:late_thread]
49
+ @libraries[:late_thread][which] ||= []
50
+ @libraries[:late_thread][which] << lib
51
+ else
52
+ @libraries[:start] << lib
53
+ end
54
+
55
+ add_library_callback(lib, &block) if block_given?
56
+ end
57
+
58
+ # add a callback that gets (usually) executed after loading the library
59
+ def add_library_callback(lib, &block)
60
+ lib = lib.to_s
61
+ @lib_hooks[lib] << block
62
+ end
63
+
64
+ # replace all callbacks with the new one given in the block
65
+ # a callback that gets (usually) executed after loading the library
66
+ def replace_library_callback(lib, &block)
67
+ lib = lib.to_s
68
+ @lib_hooks[lib].clear
69
+ @lib_hooks[lib] << block
70
+ end
71
+
72
+ # don't load a specific library
73
+ def remove_library(lib)
74
+ lib = lib.to_s
75
+
76
+ @libraries[:start].delete lib
77
+ @libraries[:sub_session].delete lib
78
+ @libraries[:autoload].reject!{|_,e,| e == lib }
79
+ @libraries[:thread].each{ |_,libs| libs.delete lib }
80
+ @libraries[:late].delete lib
81
+ @libraries[:late_thread].each{ |_,libs| libs.delete lib }
82
+
83
+ @lib_hooks.delete lib
84
+ end
85
+
86
+ # add extensions packages
87
+ def add_package(pkg)
88
+ @packages << pkg.to_s
89
+ end
90
+
91
+ # remove extension package
92
+ def remove_package(pkg)
93
+ @packages.delete pkg.to_s
94
+ end
95
+
96
+ # actually require registered packages
97
+ def load_packages
98
+ @packages.each{ |pkg|
99
+ require "irbtools/#{pkg}"
100
+ }
101
+ end
102
+
103
+ # te be triggered when a library has loaded
104
+ def library_loaded(lib)
105
+ @lib_hooks[lib.to_s].each{ |hook| hook.call }
106
+ end
107
+
108
+ # actually load libraries
109
+ def load_libraries(libs)
110
+ remember_verbose_and_debug = $VERBOSE, $DEBUG
111
+ $VERBOSE = $DEBUG = false
112
+
113
+ libs.each{ |lib|
114
+ begin
115
+ require lib.to_s
116
+ library_loaded(lib)
117
+ rescue Exception => err
118
+ warn "Error while loading a library into IRB:\n\n### #{err.class}\n" +
119
+ err.message + "\n\n### STACKTRACE\n " + err.backtrace*"\n " + "\n\n\n"
120
+ end
121
+ }
122
+ $VERBOSE, $DEBUG = remember_verbose_and_debug
123
+ end
124
+
125
+ # configure irb
126
+ def configure_irb!
127
+ if defined?(IRB)
128
+ IRB.conf[:AUTO_INDENT] = true # simple auto indent
129
+ IRB.conf[:EVAL_HISTORY] = 42424242424242424242 # creates the special __ variable
130
+ IRB.conf[:SAVE_HISTORY] = 2000 # how many lines will go to ~/.irb_history
131
+
132
+ # prompt
133
+ (IRB.conf[:PROMPT] ||= {} ).merge!( {:IRBTOOLS => {
134
+ :PROMPT_I => ">> ", # normal
135
+ :PROMPT_N => "| ", # indenting
136
+ :PROMPT_C => " > ", # continuing a statement
137
+ :PROMPT_S => "%l> ", # continuing a string
138
+ :RETURN => "=> %s \n",
139
+ :AUTO_INDENT => true,
140
+ }})
141
+
142
+ IRB.conf[:PROMPT_MODE] = :IRBTOOLS
143
+ end
144
+ end
145
+
146
+ # check if we are in a RIPL session
147
+ def ripl?
148
+ defined?(Ripl) && Ripl.started?
149
+ end
150
+
151
+ # loads all the stuff
152
+ def start
153
+ require 'irbtools'
154
+ end
155
+ end
156
+ end