pry 0.8.0pre2 → 0.8.0pre4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,9 @@ require 'forwardable'
2
2
 
3
3
  class Pry
4
4
  class CommandProcessor
5
+ SYSTEM_COMMAND_DELIMITER = "."
6
+ SYSTEM_COMMAND_REGEX = /^#{Regexp.escape(SYSTEM_COMMAND_DELIMITER)}(.*)/
7
+
5
8
  extend Forwardable
6
9
 
7
10
  attr_reader :pry_instance
@@ -12,23 +15,33 @@ class Pry
12
15
 
13
16
  def_delegators :@pry_instance, :commands, :nesting, :output
14
17
 
15
- def system_command(val)
16
- if val =~ /^\.(.*)/
17
- execute_system_command($1)
18
- val.clear
19
- else
20
- return false
21
- end
22
- true
18
+ def valid_command?(val)
19
+ system_command?(val) || pry_command?(val)
23
20
  end
24
21
 
25
- def execute_system_command(cmd)
22
+ def system_command?(val)
23
+ !!(SYSTEM_COMMAND_REGEX =~ val)
24
+ end
25
+
26
+ def pry_command?(val)
27
+ !!command_matched(val).first
28
+ end
29
+
30
+ def execute_system_command(val)
31
+ SYSTEM_COMMAND_REGEX =~ val
32
+ cmd = $1
33
+
26
34
  if cmd =~ /^cd\s+(.+)/i
27
- Dir.chdir(File.expand_path($1))
28
- system(cmd)
35
+ begin
36
+ Dir.chdir(File.expand_path($1))
37
+ rescue Errno::ENOENT
38
+ output.puts "No such directory: #{$1}"
39
+ end
29
40
  else
30
41
  system(cmd)
31
42
  end
43
+
44
+ val.clear
32
45
  end
33
46
 
34
47
  # Determine whether a Pry command was matched and return command data
@@ -57,7 +70,10 @@ class Pry
57
70
  def val.clear() replace("") end
58
71
  def eval_string.clear() replace("") end
59
72
 
60
- return if system_command(val)
73
+ if system_command?(val)
74
+ execute_system_command(val)
75
+ return
76
+ end
61
77
 
62
78
  cmd_data, args_string = command_matched(val)
63
79
 
@@ -120,6 +120,13 @@ class Pry
120
120
  alias_command "quit-program", "exit-program", ""
121
121
  alias_command "!!!", "exit-program", ""
122
122
 
123
+ command "gem-cd", "Change working directory to specified gem's directory." do |gem_name|
124
+ require 'rubygems'
125
+ gem_spec = Gem.source_index.find_name(gem_name).first
126
+ next output.put("Gem `#{gem_name}` not found.") if !gem_spec
127
+ Dir.chdir(File.expand_path(gem_spec.full_gem_path))
128
+ end
129
+
123
130
  command "toggle-color", "Toggle syntax highlighting." do
124
131
  Pry.color = !Pry.color
125
132
  output.puts "Syntax highlighting #{Pry.color ? "on" : "off"}"
@@ -134,7 +141,6 @@ class Pry
134
141
  end
135
142
  end
136
143
 
137
- # FIXME: when restoring backups does not restore descriptions
138
144
  command "file-mode", "Toggle file mode." do
139
145
  case Pry.active_instance.prompt
140
146
  when Pry::FILE_PROMPT
@@ -413,9 +419,10 @@ Shows local and instance variables by default.
413
419
  ".json" => :json
414
420
  }
415
421
 
416
- syntax_highlight_by_file_type = lambda do |contents, file_name|
422
+ syntax_highlight_by_file_type_or_specified = lambda do |contents, file_name, file_type|
417
423
  _, language_detected = file_map.find { |k, v| Array(k).include?(File.extname(file_name)) }
418
424
 
425
+ language_detected = file_type if file_type
419
426
  CodeRay.scan(contents, language_detected).term
420
427
  end
421
428
 
@@ -443,11 +450,12 @@ Shows local and instance variables by default.
443
450
  end
444
451
  end
445
452
 
446
- command "cat-file", "Show output of file FILE. Type `cat --help` for more information. Aliases: :cat" do |*args|
453
+ command "cat-file", "Show output of file FILE. Type `cat --help` for more information." do |*args|
447
454
  options= {}
448
455
  file_name = nil
449
456
  start_line = 0
450
457
  end_line = -1
458
+ file_type = nil
451
459
 
452
460
  OptionParser.new do |opts|
453
461
  opts.banner = %{Usage: cat-file [OPTIONS] FILE
@@ -467,6 +475,10 @@ e.g: cat-file hello.rb
467
475
  end_line = line.to_i - 1
468
476
  end
469
477
 
478
+ opts.on("-t", "--type TYPE", "The specific file type for syntax higlighting.") do |type|
479
+ file_type = type.to_sym
480
+ end
481
+
470
482
  opts.on_tail("-h", "--help", "This message.") do
471
483
  output.puts opts
472
484
  options[:h] = true
@@ -485,7 +497,7 @@ e.g: cat-file hello.rb
485
497
  contents = read_between_the_lines.call(file_name, start_line, end_line, false)
486
498
 
487
499
  if Pry.color
488
- contents = syntax_highlight_by_file_type.call(contents, file_name)
500
+ contents = syntax_highlight_by_file_type_or_specified.call(contents, file_name, file_type)
489
501
  end
490
502
 
491
503
  if options[:l]
@@ -496,8 +508,6 @@ e.g: cat-file hello.rb
496
508
  contents
497
509
  end
498
510
 
499
- alias_command ":cat", "cat-file", ""
500
-
501
511
  command "eval-file", "Eval a Ruby script. Type `eval-file --help` for more info." do |*args|
502
512
  options = {}
503
513
  target = target()
@@ -57,6 +57,12 @@ class Pry
57
57
  # :after_session => proc { puts "goodbye" }
58
58
  attr_accessor :hooks
59
59
 
60
+
61
+ # Get/Set the Proc that defines extra Readline completions (on top
62
+ # of the ones defined for IRB).
63
+ # @return [Proc] The Proc that defines extra Readline completions (on top
64
+ # @example Add file names to completion list
65
+ # Pry.custom_completions = proc { Dir.entries('.') }
60
66
  attr_accessor :custom_completions
61
67
 
62
68
  # Get the array of Procs to be used for the prompts by default by
@@ -141,7 +141,8 @@ class Pry
141
141
  # Pry.new.rep(Object.new)
142
142
  def rep(target=TOPLEVEL_BINDING)
143
143
  target = Pry.binding_for(target)
144
- print.call output, re(target)
144
+ result = re(target)
145
+ print.call output, result if !@suppress_output
145
146
  end
146
147
 
147
148
  # Perform a read-eval
@@ -185,13 +186,29 @@ class Pry
185
186
  # Pry.new.r(Object.new)
186
187
  def r(target=TOPLEVEL_BINDING)
187
188
  target = Pry.binding_for(target)
189
+ @suppress_output = false
188
190
  eval_string = ""
189
191
 
190
192
  loop do
191
193
  val = retrieve_line(eval_string, target)
192
194
  process_line(val, eval_string, target)
193
- break eval_string if valid_expression?(eval_string)
195
+ if valid_expression?(eval_string)
196
+ redo if null_input?(val)
197
+ break
198
+ end
194
199
  end
200
+
201
+ @suppress_output = true if eval_string =~ /;\Z/
202
+
203
+ eval_string
204
+ end
205
+
206
+ # Returns true if input is "" and a command is not returning a
207
+ # value.
208
+ # @param [String] val The input string.
209
+ # @return [Boolean] Whether the input is null.
210
+ def null_input?(val)
211
+ val.empty? && !Pry.cmd_ret_value
195
212
  end
196
213
 
197
214
  # Read a line of input and check for ^d, also determine prompt to use.
@@ -224,7 +241,7 @@ class Pry
224
241
  if Pry.cmd_ret_value
225
242
  eval_string << "Pry.cmd_ret_value\n"
226
243
  else
227
- eval_string << "#{val}\n"
244
+ eval_string << "#{val}\n" if !val.empty?
228
245
  end
229
246
  end
230
247
 
@@ -1,3 +1,3 @@
1
1
  class Pry
2
- VERSION = "0.8.0pre2"
2
+ VERSION = "0.8.0pre4"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 8
8
- - 0pre2
9
- version: 0.8.0pre2
8
+ - 0pre4
9
+ version: 0.8.0pre4
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Mair (banisterfiend)
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-04-07 00:00:00 +12:00
17
+ date: 2011-04-08 00:00:00 +12:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency