pry 0.9.8pre2 → 0.9.8pre3

Sign up to get free protection for your applications and to get access to all the features.
@@ -60,8 +60,32 @@ they are lost for hours.
60
60
  text
61
61
  end
62
62
 
63
+ command "test-ansi", "" do
64
+ prev_color = Pry.color
65
+ Pry.color = true
63
66
 
64
- end
67
+ picture = unindent <<-'EOS'.gsub(/[[:alpha:]!]/) { |s| text.red(s) }
68
+ ____ _______________________
69
+ / \ | A W G |
70
+ / O O \ | N I O N ! |
71
+ | | | S S R I ! |
72
+ \ \__/ / __| I K ! |
73
+ \____/ \________________________|
74
+ EOS
75
+
76
+ if defined?(Win32::Console)
77
+ move_up = proc { |n| "\e[#{n}F" }
78
+ else
79
+ move_up = proc { |n| "\e[#{n}A\e[0G" }
80
+ end
81
+
82
+ output.puts "\n" * 6
83
+ output.puts picture.lines.map(&:chomp).reverse.join(move_up[1])
84
+ output.puts "\n" * 6
85
+ output.puts "** ENV['TERM'] is #{ENV['TERM']} **\n\n"
65
86
 
87
+ Pry.color = prev_color
88
+ end
89
+ end
66
90
  end
67
91
  end
@@ -28,7 +28,7 @@ class Pry
28
28
  USAGE
29
29
 
30
30
  opt.on :h, :help, "This message." do
31
- output.puts opt
31
+ output.puts opt.help
32
32
  end
33
33
  end
34
34
 
@@ -74,7 +74,7 @@ class Pry
74
74
  opt.on :f, "file", 'The file to replay in context.', true
75
75
  opt.on :o, "open", 'When used with the -m switch, it plays the entire method except the last line, leaving the method definition "open". `amend-line` can then be used to modify the method.'
76
76
  opt.on :h, :help, "This message." do
77
- output.puts opt
77
+ output.puts opt.help
78
78
  end
79
79
  end
80
80
 
@@ -214,7 +214,7 @@ class Pry
214
214
 
215
215
  if opts.present?(:exclude)
216
216
  history.map!.with_index do |element, index|
217
- unless command_processor.valid_command? element
217
+ unless command_set.valid_command? element
218
218
  if opts.present?(:'no-numbers')
219
219
  element
220
220
  else
@@ -262,7 +262,7 @@ class Pry
262
262
  output.puts "Saving history in #{file_name} ..."
263
263
  # exclude pry commands
264
264
  hist_array.reject! do |element|
265
- command_processor.valid_command?(element)
265
+ command_set.valid_command?(element)
266
266
  end
267
267
 
268
268
  File.open(file_name, 'w') do |f|
@@ -283,32 +283,6 @@ class Pry
283
283
 
284
284
  alias_command "history", "hist"
285
285
 
286
- helpers do
287
- def one_index_number(line_number)
288
- if line_number > 0
289
- line_number - 1
290
- elsif line_number < 0
291
- line_number
292
- else
293
- line_number
294
- end
295
- end
296
-
297
- def one_index_range(range)
298
- Range.new(one_index_number(range.begin), one_index_number(range.end))
299
- end
300
-
301
- def one_index_range_or_number(range_or_number)
302
- case range_or_number
303
- when Range
304
- one_index_range(range_or_number)
305
- else
306
- one_index_number(range_or_number)
307
- end
308
- end
309
-
310
- end
311
-
312
286
  end
313
287
 
314
288
  end
@@ -5,40 +5,44 @@ class Pry
5
5
 
6
6
  Introspection = Pry::CommandSet.new do
7
7
 
8
- command "show-method", "Show the source for METH. Type `show-method --help` for more info. Aliases: $, show-source", :shellwords => false do |*args|
9
- opts, meth = parse_options!(args, :method_object) do |opt|
10
- opt.banner unindent <<-USAGE
11
- Usage: show-method [OPTIONS] [METH]
12
- Show the source for method METH. Tries instance methods first and then methods by default.
13
- e.g: show-method hello_method
14
- USAGE
15
-
8
+ command_class "show-method", "Show the source for METH. Type `show-method --help` for more info. Aliases: $, show-source", :shellwords => false do |*args|
9
+ banner <<-BANNER
10
+ Usage: show-method [OPTIONS] [METH]
11
+ Show the source for method METH. Tries instance methods first and then methods by default.
12
+ e.g: show-method hello_method
13
+ BANNER
14
+
15
+ def options(opt)
16
+ method_options(opt)
16
17
  opt.on :l, "line-numbers", "Show line numbers."
17
18
  opt.on :b, "base-one", "Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)."
18
19
  opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
19
20
  end
20
21
 
21
- raise CommandError, "Could not find method source" unless meth.source
22
+ def process
23
+ meth = method_object
24
+ raise CommandError, "Could not find method source" unless meth.source
22
25
 
23
- output.puts make_header(meth)
24
- output.puts "#{text.bold("Owner:")} #{meth.owner || "N/A"}"
25
- output.puts "#{text.bold("Visibility:")} #{meth.visibility}"
26
- output.puts
26
+ output.puts make_header(meth)
27
+ output.puts "#{text.bold("Owner:")} #{meth.owner || "N/A"}"
28
+ output.puts "#{text.bold("Visibility:")} #{meth.visibility}"
29
+ output.puts
27
30
 
28
- if Pry.color
29
- code = CodeRay.scan(meth.source, meth.source_type).term
30
- else
31
- code = meth.source
32
- end
31
+ if Pry.color
32
+ code = CodeRay.scan(meth.source, meth.source_type).term
33
+ else
34
+ code = meth.source
35
+ end
33
36
 
34
- start_line = false
35
- if opts.present?(:'base-one')
36
- start_line = 1
37
- elsif opts.present?(:'line-numbers')
38
- start_line = meth.source_line || 1
39
- end
37
+ start_line = false
38
+ if opts.present?(:'base-one')
39
+ start_line = 1
40
+ elsif opts.present?(:'line-numbers')
41
+ start_line = meth.source_line || 1
42
+ end
40
43
 
41
- render_output(opts.present?(:flood), start_line, code)
44
+ render_output(opts.present?(:flood), start_line, code)
45
+ end
42
46
  end
43
47
 
44
48
  alias_command "show-source", "show-method"
@@ -57,7 +61,7 @@ class Pry
57
61
  opt.on :l, "line-numbers", "Show line numbers."
58
62
  opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
59
63
  opt.on :h, :help, "This message." do
60
- output.puts opt
64
+ output.puts opt.help
61
65
  end
62
66
  end
63
67
 
@@ -111,7 +115,7 @@ class Pry
111
115
  opt.on :n, :"no-reload", "Don't automatically reload the edited code"
112
116
  opt.on :r, :reload, "Reload the edited code immediately (default for ruby files)"
113
117
  opt.on :h, :help, "This message." do
114
- output.puts opt
118
+ output.puts opt.help
115
119
  end
116
120
  end
117
121
  next if opts.present?(:help)
@@ -199,64 +203,63 @@ class Pry
199
203
  end
200
204
  end
201
205
 
202
- command "edit-method", "Edit a method. Type `edit-method --help` for more info.", :shellwords => false do |*args|
203
- target = target()
206
+ command_class "edit-method", "Edit a method. Type `edit-method --help` for more info.", :shellwords => false do |*args|
204
207
 
205
- opts, meth = parse_options!(args, :method_object) do |opt|
206
- opt.banner unindent <<-USAGE
208
+ def options(opt)
209
+ opt.banner unindent <<-BANNER
207
210
  Usage: edit-method [OPTIONS] [METH]
208
211
  Edit the method METH in an editor.
209
212
  Ensure #{text.bold("Pry.config.editor")} is set to your editor of choice.
210
213
  e.g: edit-method hello_method
211
- USAGE
212
-
214
+ BANNER
215
+ method_options(opt)
213
216
  opt.on :n, "no-reload", "Do not automatically reload the method's file after editing."
214
217
  opt.on "no-jump", "Do not fast forward editor to first line of method."
215
218
  opt.on :p, :patch, "Instead of editing the method's file, try to edit in a tempfile and apply as a monkey patch."
216
- opt.on :h, :help, "This message." do
217
- output.puts opt
218
- end
219
219
  end
220
220
 
221
- if !Pry.config.editor
222
- raise CommandError, "No editor set!\nEnsure that #{text.bold("Pry.config.editor")} is set to your editor of choice."
223
- end
224
-
225
- if opts.present?(:patch) || meth.dynamically_defined?
226
- lines = meth.source.lines.to_a
227
-
228
- if ((original_name = meth.original_name) &&
229
- lines[0] =~ /^def (?:.*?\.)?#{original_name}(?=[\(\s;]|$)/)
230
- lines[0] = "def #{original_name}#{$'}"
231
- else
232
- raise CommandError, "Pry can only patch methods created with the `def` keyword."
221
+ def process
222
+ meth = method_object
223
+ if !Pry.config.editor
224
+ raise CommandError, "No editor set!\nEnsure that #{text.bold("Pry.config.editor")} is set to your editor of choice."
233
225
  end
234
226
 
235
- temp_file do |f|
236
- f.puts lines.join
237
- f.flush
238
- invoke_editor(f.path, 0)
227
+ if opts.present?(:patch) || meth.dynamically_defined?
228
+ lines = meth.source.lines.to_a
239
229
 
240
- if meth.alias?
241
- with_method_transaction(original_name, meth.owner) do
230
+ if ((original_name = meth.original_name) &&
231
+ lines[0] =~ /^def (?:.*?\.)?#{original_name}(?=[\(\s;]|$)/)
232
+ lines[0] = "def #{original_name}#{$'}"
233
+ else
234
+ raise CommandError, "Pry can only patch methods created with the `def` keyword."
235
+ end
236
+
237
+ temp_file do |f|
238
+ f.puts lines.join
239
+ f.flush
240
+ invoke_editor(f.path, 0)
241
+
242
+ if meth.alias?
243
+ with_method_transaction(original_name, meth.owner) do
244
+ Pry.new(:input => StringIO.new(File.read(f.path))).rep(meth.owner)
245
+ Pry.binding_for(meth.owner).eval("alias #{meth.name} #{original_name}")
246
+ end
247
+ else
242
248
  Pry.new(:input => StringIO.new(File.read(f.path))).rep(meth.owner)
243
- Pry.binding_for(meth.owner).eval("alias #{meth.name} #{original_name}")
244
249
  end
245
- else
246
- Pry.new(:input => StringIO.new(File.read(f.path))).rep(meth.owner)
247
250
  end
251
+ return
248
252
  end
249
- next
250
- end
251
253
 
252
- if meth.source_type == :c
253
- raise CommandError, "Can't edit a C method."
254
- else
255
- file, line = meth.source_file, meth.source_line
254
+ if meth.source_type == :c
255
+ raise CommandError, "Can't edit a C method."
256
+ else
257
+ file, line = meth.source_file, meth.source_line
256
258
 
257
- invoke_editor(file, opts["no-jump"] ? 0 : line)
258
- silence_warnings do
259
- load file if !opts.present?(:'no-jump') && !Pry.config.disable_auto_reload
259
+ invoke_editor(file, opts["no-jump"] ? 0 : line)
260
+ silence_warnings do
261
+ load file if !opts.present?(:'no-jump') && !Pry.config.disable_auto_reload
262
+ end
260
263
  end
261
264
  end
262
265
  end
@@ -3,7 +3,94 @@ class Pry
3
3
 
4
4
  Ls = Pry::CommandSet.new do
5
5
 
6
- helpers do
6
+ command_class "ls","Show the list of vars and methods in the current scope. Type `ls --help` for more info.",
7
+ :shellwords => false, :interpolate => false do
8
+
9
+ def options(opt)
10
+ opt.banner unindent <<-USAGE
11
+ Usage: ls [-m|-M|-p|-pM] [-q|-v] [-c|-i] [Object]
12
+ ls [-g] [-l]
13
+
14
+ ls shows you which methods, constants and variables are accessible to Pry. By default it shows you the local variables defined in the current shell, and any public methods or instance variables defined on the current object.
15
+
16
+ The colours used are configurable using Pry.config.ls.*_color, and the separator is Pry.config.ls.separator.
17
+
18
+ Pry.config.ls.ceiling is used to hide methods defined higher up in the inheritance chain, this is by default set to [Object, Module, Class] so that methods defined on all Objects are omitted. The -v flag can be used to ignore this setting and show all methods, while the -q can be used to set the ceiling much lower and show only methods defined on the object or its direct class.
19
+ USAGE
20
+
21
+ opt.on :m, "methods", "Show public methods defined on the Object (default)"
22
+ opt.on :M, "instance-methods", "Show methods defined in a Module or Class"
23
+
24
+ opt.on :p, "ppp", "Show public, protected (in yellow) and private (in green) methods"
25
+ opt.on :q, "quiet", "Show only methods defined on object.singleton_class and object.class"
26
+ opt.on :v, "verbose", "Show methods and constants on all super-classes (ignores Pry.config.ls.ceiling)"
27
+
28
+ opt.on :g, "globals", "Show global variables, including those builtin to Ruby (in cyan)"
29
+ opt.on :l, "locals", "Show locals, including those provided by Pry (in red)"
30
+
31
+ opt.on :c, "constants", "Show constants, highlighting classes (in blue), and exceptions (in purple)"
32
+
33
+ opt.on :i, "ivars", "Show instance variables (in blue) and class variables (in bright blue)"
34
+
35
+ opt.on :G, "grep", "Filter output by regular expression", :optional => false
36
+ end
37
+
38
+ def process
39
+ obj = args.empty? ? target_self : target.eval(args.join(" "))
40
+
41
+ # exclude -q, -v and --grep because they don't specify what the user wants to see.
42
+ has_opts = (opts.present?(:methods) || opts.present?(:'instance-methods') || opts.present?(:ppp) ||
43
+ opts.present?(:globals) || opts.present?(:locals) || opts.present?(:constants) ||
44
+ opts.present?(:ivars))
45
+
46
+ show_methods = opts.present?(:methods) || opts.present?(:'instance-methods') || opts.present?(:ppp) || !has_opts
47
+ show_constants = opts.present?(:constants) || (!has_opts && Module === obj)
48
+ show_ivars = opts.present?(:ivars) || !has_opts
49
+ show_locals = opts.present?(:locals) || (!has_opts && args.empty?)
50
+
51
+ grep_regex, grep = [Regexp.new(opts[:G] || "."), lambda{ |x| x.grep(grep_regex) }]
52
+
53
+ raise Pry::CommandError, "-l does not make sense with a specified Object" if opts.present?(:locals) && !args.empty?
54
+ raise Pry::CommandError, "-g does not make sense with a specified Object" if opts.present?(:globals) && !args.empty?
55
+ raise Pry::CommandError, "-q does not make sense with -v" if opts.present?(:quiet) && opts.present?(:verbose)
56
+ raise Pry::CommandError, "-M only makes sense with a Module or a Class" if opts.present?(:'instance-methods') && !(Module === obj)
57
+ raise Pry::CommandError, "-c only makes sense with a Module or a Class" if opts.present?(:constants) && !args.empty? && !(Module === obj)
58
+
59
+
60
+ if opts.present?(:globals)
61
+ output_section("global variables", grep[format_globals(target.eval("global_variables"))])
62
+ end
63
+
64
+ if show_constants
65
+ mod = Module === obj ? obj : Object
66
+ constants = mod.constants
67
+ constants -= (mod.ancestors - [mod]).map(&:constants).flatten unless opts.present?(:verbose)
68
+ output_section("constants", grep[format_constants(mod, constants)])
69
+ end
70
+
71
+ if show_methods
72
+ # methods is a hash {Module/Class => [Pry::Methods]}
73
+ methods = all_methods(obj).select{ |method| opts.present?(:ppp) || method.visibility == :public }.group_by(&:owner)
74
+
75
+ # reverse the resolution order so that the most useful information appears right by the prompt
76
+ resolution_order(obj).take_while(&below_ceiling(obj)).reverse.each do |klass|
77
+ methods_here = format_methods((methods[klass] || []).select{ |m| m.name =~ grep_regex })
78
+ output_section "#{Pry::WrappedModule.new(klass).method_prefix}methods", methods_here
79
+ end
80
+ end
81
+
82
+ if show_ivars
83
+ klass = (Module === obj ? obj : obj.class)
84
+ output_section("instance variables", format_variables(:instance_var, grep[obj.__send__(:instance_variables)]))
85
+ output_section("class variables", format_variables(:class_var, grep[klass.__send__(:class_variables)]))
86
+ end
87
+
88
+ if show_locals
89
+ output_section("locals", format_locals(grep[target.eval("local_variables")]))
90
+ end
91
+ end
92
+
93
+ private
7
94
 
8
95
  # http://ruby.runpaint.org/globals, and running "puts global_variables.inspect".
9
96
  BUILTIN_GLOBALS = %w($" $$ $* $, $-0 $-F $-I $-K $-W $-a $-d $-i $-l $-p $-v $-w $. $/ $\\
@@ -20,17 +107,17 @@ class Pry
20
107
  $LAST_PAREN_MATCH $LAST_READ_LINE $MATCH $POSTMATCH $PREMATCH)
21
108
 
22
109
  # Get all the methods that we'll want to output
23
- def all_methods(obj, opts)
110
+ def all_methods(obj)
24
111
  opts.present?(:'instance-methods') ? Pry::Method.all_from_class(obj) : Pry::Method.all_from_obj(obj)
25
112
  end
26
113
 
27
- def resolution_order(obj, opts)
114
+ def resolution_order(obj)
28
115
  opts.present?(:'instance-methods') ? Pry::Method.instance_resolution_order(obj) : Pry::Method.resolution_order(obj)
29
116
  end
30
117
 
31
118
  # Get a lambda that can be used with .take_while to prevent over-eager
32
119
  # traversal of the Object's ancestry graph.
33
- def below_ceiling(obj, opts)
120
+ def below_ceiling(obj)
34
121
  ceiling = if opts.present?(:quiet)
35
122
  [opts.present?(:'instance-methods') ? obj.ancestors[1] : obj.class.ancestors[1]] + Pry.config.ls.ceiling
36
123
  elsif opts.present?(:verbose)
@@ -108,96 +195,6 @@ class Pry
108
195
  text.send(Pry.config.ls.send(:"#{type}_color"), str)
109
196
  end
110
197
  end
111
-
112
- command "ls", "Show the list of vars and methods in the current scope. Type `ls --help` for more info.",
113
- :shellwords => false, :interpolate => false do |*args|
114
-
115
- opts = Slop.parse!(args, :strict => true) do |opt|
116
- opt.banner unindent <<-USAGE
117
- Usage: ls [-m|-M|-p|-pM] [-q|-v] [-c|-i] [Object]
118
- ls [-g] [-l]
119
-
120
- ls shows you which methods, constants and variables are accessible to Pry. By default it shows you the local variables defined in the current shell, and any public methods or instance variables defined on the current object.
121
-
122
- The colours used are configurable using Pry.config.ls.*_color, and the separator is Pry.config.ls.separator.
123
-
124
- Pry.config.ls.ceiling is used to hide methods defined higher up in the inheritance chain, this is by default set to [Object, Module, Class] so that methods defined on all Objects are omitted. The -v flag can be used to ignore this setting and show all methods, while the -q can be used to set the ceiling much lower and show only methods defined on the object or its direct class.
125
- USAGE
126
-
127
- opt.on :m, "methods", "Show public methods defined on the Object (default)"
128
- opt.on :M, "instance-methods", "Show methods defined in a Module or Class"
129
-
130
- opt.on :p, "ppp", "Show public, protected (in yellow) and private (in green) methods"
131
- opt.on :q, "quiet", "Show only methods defined on object.singleton_class and object.class"
132
- opt.on :v, "verbose", "Show methods and constants on all super-classes (ignores Pry.config.ls.ceiling)"
133
-
134
- opt.on :g, "globals", "Show global variables, including those builtin to Ruby (in cyan)"
135
- opt.on :l, "locals", "Show locals, including those provided by Pry (in red)"
136
-
137
- opt.on :c, "constants", "Show constants, highlighting classes (in blue), and exceptions (in purple)"
138
-
139
- opt.on :i, "ivars", "Show instance variables (in blue) and class variables (in bright blue)"
140
-
141
- opt.on :G, "grep", "Filter output by regular expression", :optional => false
142
-
143
- opt.on :h, "help", "Show help"
144
- end
145
-
146
- next output.puts(opts.to_s) if opts.present?(:help)
147
-
148
- obj = args.empty? ? target_self : target.eval(args.join(" "))
149
-
150
- # exclude -q, -v and --grep because they don't specify what the user wants to see.
151
- has_opts = (opts.present?(:methods) || opts.present?(:'instance-methods') || opts.present?(:ppp) ||
152
- opts.present?(:globals) || opts.present?(:locals) || opts.present?(:constants) ||
153
- opts.present?(:ivars))
154
-
155
- show_methods = opts.present?(:methods) || opts.present?(:'instance-methods') || opts.present?(:ppp) || !has_opts
156
- show_constants = opts.present?(:constants) || (!has_opts && Module === obj)
157
- show_ivars = opts.present?(:ivars) || !has_opts
158
- show_locals = opts.present?(:locals) || (!has_opts && args.empty?)
159
-
160
- grep_regex, grep = [Regexp.new(opts[:G] || "."), lambda{ |x| x.grep(grep_regex) }]
161
-
162
- raise Pry::CommandError, "-l does not make sense with a specified Object" if opts.present?(:locals) && !args.empty?
163
- raise Pry::CommandError, "-g does not make sense with a specified Object" if opts.present?(:globals) && !args.empty?
164
- raise Pry::CommandError, "-q does not make sense with -v" if opts.present?(:quiet) && opts.present?(:verbose)
165
- raise Pry::CommandError, "-M only makes sense with a Module or a Class" if opts.present?(:'instance-methods') && !(Module === obj)
166
- raise Pry::CommandError, "-c only makes sense with a Module or a Class" if opts.present?(:constants) && !args.empty? && !(Module === obj)
167
-
168
-
169
- if opts.present?(:globals)
170
- output_section("global variables", grep[format_globals(target.eval("global_variables"))])
171
- end
172
-
173
- if show_constants
174
- mod = Module === obj ? obj : Object
175
- constants = mod.constants
176
- constants -= (mod.ancestors - [mod]).map(&:constants).flatten unless opts.present?(:verbose)
177
- output_section("constants", grep[format_constants(mod, constants)])
178
- end
179
-
180
- if show_methods
181
- # methods is a hash {Module/Class => [Pry::Methods]}
182
- methods = all_methods(obj, opts).select{ |method| opts.present?(:ppp) || method.visibility == :public }.group_by(&:owner)
183
-
184
- # reverse the resolution order so that the most useful information appears right by the prompt
185
- resolution_order(obj, opts).take_while(&below_ceiling(obj, opts)).reverse.each do |klass|
186
- methods_here = format_methods((methods[klass] || []).select{ |m| m.name =~ grep_regex })
187
- output_section "#{Pry::WrappedModule.new(klass).method_prefix}methods", methods_here
188
- end
189
- end
190
-
191
- if show_ivars
192
- klass = (Module === obj ? obj : obj.class)
193
- output_section("instance variables", format_variables(:instance_var, grep[obj.__send__(:instance_variables)]))
194
- output_section("class variables", format_variables(:class_var, grep[klass.__send__(:class_variables)]))
195
- end
196
-
197
- if show_locals
198
- output_section("locals", format_locals(grep[target.eval("local_variables")]))
199
- end
200
- end
201
198
  end
202
199
  end
203
200
  end