pry 0.6.7pre4 → 0.6.7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +83 -75
- data/README.markdown +312 -309
- data/Rakefile +103 -103
- data/bin/pry +82 -82
- data/lib/pry.rb +32 -32
- data/lib/pry/commands.rb +577 -577
- data/lib/pry/print.rb +19 -19
- data/lib/pry/prompts.rb +26 -26
- data/lib/pry/pry_class.rb +186 -186
- data/lib/pry/pry_instance.rb +316 -316
- data/lib/pry/version.rb +3 -3
- metadata +13 -13
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,186 @@
|
|
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
|
+
# 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
|