pry 0.6.7pre4-i386-mingw32 → 0.6.8-i386-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/lib/pry/hooks.rb CHANGED
@@ -1,8 +1,19 @@
1
- class Pry
2
-
3
- # The default hooks - display messages when beginning and ending Pry sessions.
4
- DEFAULT_HOOKS = {
5
- :before_session => proc { |out, obj| out.puts "Beginning Pry session for #{Pry.view_clip(obj)}" },
6
- :after_session => proc { |out, obj| out.puts "Ending Pry session for #{Pry.view_clip(obj)}" }
7
- }
8
- end
1
+ class Pry
2
+
3
+ # The default hooks - display messages when beginning and ending Pry sessions.
4
+ DEFAULT_HOOKS = {
5
+
6
+ :before_session => proc do |out, target|
7
+ out.puts "Beginning Pry session for #{Pry.view_clip(target.eval('self'))}"
8
+
9
+ # ensure we're actually in a method
10
+ meth_name = target.eval('__method__')
11
+ file = target.eval('__FILE__')
12
+ if ![nil, :__binding__, :__binding_impl__].include?(meth_name) && file !~ /(\(.*\))|<.*>/
13
+ Pry.run_command "whereami", :output => out, :show_output => true, :context => target, :commands => Pry::Commands
14
+ end
15
+ end,
16
+
17
+ :after_session => proc { |out, target| out.puts "Ending Pry session for #{Pry.view_clip(target.eval('self'))}" }
18
+ }
19
+ end
data/lib/pry/print.rb CHANGED
@@ -1,19 +1,19 @@
1
- class Pry
2
-
3
- # The default print object - only show first line of backtrace and
4
- # prepend output with `=>`
5
- DEFAULT_PRINT = proc do |output, value|
6
- case value
7
- when Exception
8
- output.puts "#{value.class}: #{value.message}"
9
- output.puts "from #{value.backtrace.first}"
10
- else
11
- if Pry.color
12
- output.puts "=> #{CodeRay.scan(Pry.view(value), :ruby).term}"
13
- else
14
- output.puts "=> #{Pry.view(value)}"
15
- end
16
- end
17
- end
18
- end
19
-
1
+ class Pry
2
+
3
+ # The default print object - only show first line of backtrace and
4
+ # prepend output with `=>`
5
+ DEFAULT_PRINT = proc do |output, value|
6
+ case value
7
+ when Exception
8
+ output.puts "#{value.class}: #{value.message}"
9
+ output.puts "from #{value.backtrace.first}"
10
+ else
11
+ if Pry.color
12
+ output.puts "=> #{CodeRay.scan(Pry.view(value), :ruby).term}"
13
+ else
14
+ output.puts "=> #{Pry.view(value)}"
15
+ end
16
+ end
17
+ end
18
+ end
19
+
data/lib/pry/prompts.rb CHANGED
@@ -1,26 +1,26 @@
1
- class Pry
2
-
3
-
4
- # The default prompt; includes the target and nesting level
5
- DEFAULT_PROMPT = [
6
- proc do |target_self, nest_level|
7
-
8
- if nest_level == 0
9
- "pry(#{Pry.view_clip(target_self)})> "
10
- else
11
- "pry(#{Pry.view_clip(target_self)}):#{Pry.view_clip(nest_level)}> "
12
- end
13
- end,
14
-
15
- proc do |target_self, nest_level|
16
- if nest_level == 0
17
- "pry(#{Pry.view_clip(target_self)})* "
18
- else
19
- "pry(#{Pry.view_clip(target_self)}):#{Pry.view_clip(nest_level)}* "
20
- end
21
- end
22
- ]
23
-
24
- # A simple prompt - doesn't display target or nesting level
25
- SIMPLE_PROMPT = [proc { ">> " }, proc { ">* " }]
26
- end
1
+ class Pry
2
+
3
+
4
+ # The default prompt; includes the target and nesting level
5
+ DEFAULT_PROMPT = [
6
+ proc do |target_self, nest_level|
7
+
8
+ if nest_level == 0
9
+ "pry(#{Pry.view_clip(target_self)})> "
10
+ else
11
+ "pry(#{Pry.view_clip(target_self)}):#{Pry.view_clip(nest_level)}> "
12
+ end
13
+ end,
14
+
15
+ proc do |target_self, nest_level|
16
+ if nest_level == 0
17
+ "pry(#{Pry.view_clip(target_self)})* "
18
+ else
19
+ "pry(#{Pry.view_clip(target_self)}):#{Pry.view_clip(nest_level)}* "
20
+ end
21
+ end
22
+ ]
23
+
24
+ # A simple prompt - doesn't display target or nesting level
25
+ SIMPLE_PROMPT = [proc { ">> " }, proc { ">* " }]
26
+ end
data/lib/pry/pry_class.rb CHANGED
@@ -1,186 +1,219 @@
1
- # @author John Mair (banisterfiend)
2
- class Pry
3
-
4
- # class accessors
5
- class << self
6
-
7
- # Get nesting data.
8
- # This method should not need to be accessed directly.
9
- # @return [Array] The unparsed nesting information.
10
- attr_reader :nesting
11
-
12
- # Get last value evaluated by Pry.
13
- # This method should not need to be accessed directly.
14
- # @return [Object] The last result.
15
- attr_accessor :last_result
16
-
17
- # Get the active Pry instance that manages the active Pry session.
18
- # This method should not need to be accessed directly.
19
- # @return [Pry] The active Pry instance.
20
- attr_accessor :active_instance
21
-
22
- # Get/Set the object to use for input by default by all Pry instances.
23
- # @return [#readline] The object to use for input by default by all
24
- # Pry instances.
25
- attr_accessor :input
26
-
27
- # Get/Set the object to use for output by default by all Pry instances.
28
- # @return [#puts] The object to use for output by default by all
29
- # Pry instances.
30
- attr_accessor :output
31
-
32
- # Get/Set the object to use for commands by default by all Pry instances.
33
- # @return [Pry::CommandBase] The object to use for commands by default by all
34
- # Pry instances.
35
- attr_accessor :commands
36
-
37
- # Get/Set the Proc to use for printing by default by all Pry
38
- # instances.
39
- # This is the 'print' component of the REPL.
40
- # @return [Proc] The Proc to use for printing by default by all
41
- # Pry instances.
42
- attr_accessor :print
43
-
44
- # Get/Set the Hash that defines Pry hooks used by default by all Pry
45
- # instances.
46
- # @return [Hash] The hooks used by default by all Pry instances.
47
- # @example
48
- # Pry.hooks :before_session => proc { puts "hello" },
49
- # :after_session => proc { puts "goodbye" }
50
- attr_accessor :hooks
51
-
52
- # Get the array of Procs to be used for the prompts by default by
53
- # all Pry instances.
54
- # @return [Array<Proc>] The array of Procs to be used for the
55
- # prompts by default by all Pry instances.
56
- attr_accessor :prompt
57
-
58
- # Value returned by last executed Pry command.
59
- # @return [Object] The command value
60
- attr_accessor :cmd_ret_value
61
-
62
- # Determines whether colored output is enabled.
63
- # @return [Boolean]
64
- attr_accessor :color
65
- end
66
-
67
- # Start a Pry REPL.
68
- # @param [Object, Binding] target The receiver of the Pry session
69
- # @param [Hash] options
70
- # @option options (see Pry#initialize)
71
- # @example
72
- # Pry.start(Object.new, :input => MyInput.new)
73
- def self.start(target=TOPLEVEL_BINDING, options={})
74
- new(options).repl(target)
75
- end
76
-
77
- # A custom version of `Kernel#inspect`.
78
- # This method should not need to be accessed directly.
79
- # @param obj The object to view.
80
- # @return [String] The string representation of `obj`.
81
- def self.view(obj)
82
- case obj
83
- when String, Hash, Array, Symbol, nil
84
- obj.inspect
85
- else
86
- obj.to_s
87
- end
88
- end
89
-
90
- # A version of `Pry.view` that clips the output to `max_size` chars.
91
- # In case of > `max_size` chars the `#<Object...> notation is used.
92
- # @param obj The object to view.
93
- # @param max_size The maximum number of chars before clipping occurs.
94
- # @return [String] The string representation of `obj`.
95
- def self.view_clip(obj, max_size=60)
96
- if Pry.view(obj).size < max_size
97
- Pry.view(obj)
98
- else
99
- "#<#{obj.class}:%#x>" % (obj.object_id << 1)
100
- end
101
- end
102
-
103
- # Run a Pry command from outside a session. The commands available are
104
- # those referenced by `Pry.commands` (the default command set).
105
- # Command output is suppresed by default, this is because the return
106
- # value (if there is one) is likely to be more useful.
107
- # @param [String] arg_string The Pry command (including arguments,
108
- # if any).
109
- # @param [Hash] options Optional named parameters.
110
- # @return [Object] The return value of the Pry command.
111
- # @option options [Object, Binding] :context The object context to run the
112
- # command under. Defaults to `TOPLEVEL_BINDING` (main).
113
- # @option options [Boolean] :show_output Whether to show command
114
- # output. Defaults to false.
115
- # @example Run at top-level with no output.
116
- # Pry.run_command "ls"
117
- # @example Run under Pry class, returning only public methods.
118
- # Pry.run_command "ls -m", :context => Pry
119
- # @example Display command output.
120
- # Pry.run_command "ls -av", :show_output => true
121
- def self.run_command(arg_string, options={})
122
- name, arg_string = arg_string.split(/\s+/, 2)
123
- arg_string = "" if !arg_string
124
-
125
- options = {
126
- :context => TOPLEVEL_BINDING,
127
- :show_output => false
128
- }.merge!(options)
129
-
130
- null_output = Object.new.tap { |v| v.instance_eval { def puts(*) end } }
131
-
132
- commands = Pry.commands.dup
133
- commands.output = options[:show_output] ? Pry.output : null_output
134
- commands.target = Pry.binding_for(options[:context])
135
-
136
- cmd = commands.commands[name]
137
- if cmd
138
- action = cmd[:action]
139
- commands.instance_exec(*Shellwords.shellwords(arg_string), &action)
140
- else
141
- raise "No such Pry command: #{name}"
142
- end
143
- end
144
-
145
- # Set all the configurable options back to their default values
146
- def self.reset_defaults
147
- @input = Readline
148
- @output = $stdout
149
- @commands = Pry::Commands
150
- @prompt = DEFAULT_PROMPT
151
- @print = DEFAULT_PRINT
152
- @hooks = DEFAULT_HOOKS
153
- @color = false
154
- end
155
-
156
- self.reset_defaults
157
-
158
- @nesting = []
159
- def @nesting.level
160
- last.is_a?(Array) ? last.first : nil
161
- end
162
-
163
- # Return all active Pry sessions.
164
- # @return [Array<Pry>] Active Pry sessions.
165
- def self.sessions
166
- # last element in nesting array is the pry instance
167
- nesting.map(&:last)
168
- end
169
-
170
- # Return a `Binding` object for `target` or return `target` if it is
171
- # already a `Binding`.
172
- # In the case where `target` is top-level then return `TOPLEVEL_BINDING`
173
- # @param [Object] target The object to get a `Binding` object for.
174
- # @return [Binding] The `Binding` object.
175
- def self.binding_for(target)
176
- if target.is_a?(Binding)
177
- target
178
- else
179
- if target == TOPLEVEL_BINDING.eval('self')
180
- TOPLEVEL_BINDING
181
- else
182
- target.__binding__
183
- end
184
- end
185
- end
186
- end
1
+ # @author John Mair (banisterfiend)
2
+ class Pry
3
+
4
+ # The RC Files to load.
5
+ RC_FILES = ["~/.pryrc"]
6
+
7
+ # class accessors
8
+ class << self
9
+
10
+ # Get nesting data.
11
+ # This method should not need to be accessed directly.
12
+ # @return [Array] The unparsed nesting information.
13
+ attr_reader :nesting
14
+
15
+ # Get last value evaluated by Pry.
16
+ # This method should not need to be accessed directly.
17
+ # @return [Object] The last result.
18
+ attr_accessor :last_result
19
+
20
+ # Get the active Pry instance that manages the active Pry session.
21
+ # This method should not need to be accessed directly.
22
+ # @return [Pry] The active Pry instance.
23
+ attr_accessor :active_instance
24
+
25
+ # Get/Set the object to use for input by default by all Pry instances.
26
+ # @return [#readline] The object to use for input by default by all
27
+ # Pry instances.
28
+ attr_accessor :input
29
+
30
+ # Get/Set the object to use for output by default by all Pry instances.
31
+ # @return [#puts] The object to use for output by default by all
32
+ # Pry instances.
33
+ attr_accessor :output
34
+
35
+ # Get/Set the object to use for commands by default by all Pry instances.
36
+ # @return [Pry::CommandBase] The object to use for commands by default by all
37
+ # Pry instances.
38
+ attr_accessor :commands
39
+
40
+ # Get/Set the Proc to use for printing by default by all Pry
41
+ # instances.
42
+ # This is the 'print' component of the REPL.
43
+ # @return [Proc] The Proc to use for printing by default by all
44
+ # Pry instances.
45
+ attr_accessor :print
46
+
47
+ # Get/Set the Hash that defines Pry hooks used by default by all Pry
48
+ # instances.
49
+ # @return [Hash] The hooks used by default by all Pry instances.
50
+ # @example
51
+ # Pry.hooks :before_session => proc { puts "hello" },
52
+ # :after_session => proc { puts "goodbye" }
53
+ attr_accessor :hooks
54
+
55
+ # Get the array of Procs to be used for the prompts by default by
56
+ # all Pry instances.
57
+ # @return [Array<Proc>] The array of Procs to be used for the
58
+ # prompts by default by all Pry instances.
59
+ attr_accessor :prompt
60
+
61
+ # Value returned by last executed Pry command.
62
+ # @return [Object] The command value
63
+ attr_accessor :cmd_ret_value
64
+
65
+ # Determines whether colored output is enabled.
66
+ # @return [Boolean]
67
+ attr_accessor :color
68
+
69
+ # Determines whether the rc file (~/.pryrc) should be loaded.
70
+ # @return [Boolean]
71
+ attr_accessor :should_load_rc
72
+
73
+ # Set to true if Pry is invoked from command line using `pry` executable
74
+ # @return [Boolean]
75
+ attr_accessor :cli
76
+ end
77
+
78
+ # Load the rc files given in the `Pry::RC_FILES` array.
79
+ # Defaults to loading just `~/.pryrc`. This method can also
80
+ # be used to reload the files if they have changed.
81
+ def self.load_rc
82
+ RC_FILES.each do |file_name|
83
+ file_name = File.expand_path(file_name)
84
+ load(file_name) if File.exists?(file_name)
85
+ end
86
+ end
87
+
88
+ # Start a Pry REPL.
89
+ # This method also loads the files specified in `Pry::RC_FILES` the
90
+ # first time it is invoked.
91
+ # @param [Object, Binding] target The receiver of the Pry session
92
+ # @param [Hash] options
93
+ # @option options (see Pry#initialize)
94
+ # @example
95
+ # Pry.start(Object.new, :input => MyInput.new)
96
+ def self.start(target=TOPLEVEL_BINDING, options={})
97
+ if should_load_rc && !@rc_loaded
98
+ load_rc
99
+ @rc_loaded = true
100
+ end
101
+
102
+ new(options).repl(target)
103
+ end
104
+
105
+ # A custom version of `Kernel#inspect`.
106
+ # This method should not need to be accessed directly.
107
+ # @param obj The object to view.
108
+ # @return [String] The string representation of `obj`.
109
+ def self.view(obj)
110
+ case obj
111
+ when String, Hash, Array, Symbol, nil
112
+ obj.inspect
113
+ else
114
+ obj.to_s
115
+ end
116
+ end
117
+
118
+ # A version of `Pry.view` that clips the output to `max_size` chars.
119
+ # In case of > `max_size` chars the `#<Object...> notation is used.
120
+ # @param obj The object to view.
121
+ # @param max_size The maximum number of chars before clipping occurs.
122
+ # @return [String] The string representation of `obj`.
123
+ def self.view_clip(obj, max_size=60)
124
+ if Pry.view(obj).size < max_size
125
+ Pry.view(obj)
126
+ else
127
+ "#<#{obj.class}:%#x>" % (obj.object_id << 1)
128
+ end
129
+ end
130
+
131
+ # Run a Pry command from outside a session. The commands available are
132
+ # those referenced by `Pry.commands` (the default command set).
133
+ # Command output is suppresed by default, this is because the return
134
+ # value (if there is one) is likely to be more useful.
135
+ # @param [String] arg_string The Pry command (including arguments,
136
+ # if any).
137
+ # @param [Hash] options Optional named parameters.
138
+ # @return [Object] The return value of the Pry command.
139
+ # @option options [Object, Binding] :context The object context to run the
140
+ # command under. Defaults to `TOPLEVEL_BINDING` (main).
141
+ # @option options [Boolean] :show_output Whether to show command
142
+ # output. Defaults to false.
143
+ # @example Run at top-level with no output.
144
+ # Pry.run_command "ls"
145
+ # @example Run under Pry class, returning only public methods.
146
+ # Pry.run_command "ls -m", :context => Pry
147
+ # @example Display command output.
148
+ # Pry.run_command "ls -av", :show_output => true
149
+ def self.run_command(arg_string, options={})
150
+ name, arg_string = arg_string.split(/\s+/, 2)
151
+ arg_string = "" if !arg_string
152
+
153
+ options = {
154
+ :context => TOPLEVEL_BINDING,
155
+ :show_output => false,
156
+ :output => Pry.output,
157
+ :commands => Pry.commands
158
+ }.merge!(options)
159
+
160
+ null_output = Object.new.tap { |v| v.instance_eval { def puts(*) end } }
161
+
162
+ commands = options[:commands].clone
163
+ commands.output = options[:show_output] ? options[:output] : null_output
164
+ commands.target = Pry.binding_for(options[:context])
165
+
166
+ cmd = commands.commands[name]
167
+ if cmd
168
+ action = cmd[:action]
169
+ commands.instance_exec(*Shellwords.shellwords(arg_string), &action)
170
+ else
171
+ raise "No such Pry command: #{name}"
172
+ end
173
+ end
174
+
175
+ # Set all the configurable options back to their default values
176
+ def self.reset_defaults
177
+ @input = Readline
178
+ @output = $stdout
179
+ @commands = Pry::Commands
180
+ @prompt = DEFAULT_PROMPT
181
+ @print = DEFAULT_PRINT
182
+ @hooks = DEFAULT_HOOKS
183
+ @color = true
184
+ @should_load_rc = true
185
+ @rc_loaded = false
186
+ @cli = false
187
+ end
188
+
189
+ self.reset_defaults
190
+
191
+ @nesting = []
192
+ def @nesting.level
193
+ last.is_a?(Array) ? last.first : nil
194
+ end
195
+
196
+ # Return all active Pry sessions.
197
+ # @return [Array<Pry>] Active Pry sessions.
198
+ def self.sessions
199
+ # last element in nesting array is the pry instance
200
+ nesting.map(&:last)
201
+ end
202
+
203
+ # Return a `Binding` object for `target` or return `target` if it is
204
+ # already a `Binding`.
205
+ # In the case where `target` is top-level then return `TOPLEVEL_BINDING`
206
+ # @param [Object] target The object to get a `Binding` object for.
207
+ # @return [Binding] The `Binding` object.
208
+ def self.binding_for(target)
209
+ if target.is_a?(Binding)
210
+ target
211
+ else
212
+ if target == TOPLEVEL_BINDING.eval('self')
213
+ TOPLEVEL_BINDING
214
+ else
215
+ target.__binding__
216
+ end
217
+ end
218
+ end
219
+ end