diff --git a/lib/pry.rb b/lib/pry.rb index 55dfa426..e1fadffa 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -13,10 +13,7 @@ class Pry # ensure we're actually in a method file = target.eval('__FILE__') - # /unknown/ for rbx - if file !~ /(\(.*\))|<.*>/ && file !~ /__unknown__/ && file != "" && file != "-e" || file == Pry.eval_path - _pry_.run_command("whereami", "", target) - end + _pry_.run_command("whereami --quiet", "", target) end # The default print diff --git a/lib/pry/code.rb b/lib/pry/code.rb index 16a96ddb..6d22e593 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -43,7 +43,7 @@ class Pry f = File.open(fn, 'r') code_type = type_from_filename(fn) else - raise CommandError, "Cannot open #{fn.inspect} for reading." + raise MethodSource::SourceNotFoundError, "Cannot open #{fn.inspect} for reading." end end new(f, 1, code_type || :ruby) diff --git a/lib/pry/default_commands/context.rb b/lib/pry/default_commands/context.rb index 1f1df7ff..e1e203b4 100644 --- a/lib/pry/default_commands/context.rb +++ b/lib/pry/default_commands/context.rb @@ -22,12 +22,16 @@ class Pry @line = target.eval('__LINE__') end + def options(opt) + opt.on :q, :quiet, "Don't display anything in case of an error" + end + def code - if show_method? - Pry::Code.from_method(@method) - else - Pry::Code.from_file(@file).around(@line, 5) - end + @code ||= if show_method? + Pry::Code.from_method(@method) + else + Pry::Code.from_file(@file).around(@line, 5) + end end def location @@ -35,6 +39,13 @@ class Pry end def process + if opts.quiet? && (internal_binding? || !code?) + return + elsif internal_binding? + output.puts "Could not find local context, did you use `binding.pry`?" + return + end + set_file_and_dir_locals(@file) output.puts "\n#{text.bold('From:')} #{location}:\n\n" @@ -44,6 +55,10 @@ class Pry private + def internal_binding? + @method && ['__binding__', '__binding_impl__'].include?(@method.name) + end + def show_method? args.empty? && @method && @method.source? && @method.source_range.count < 20 && # These checks are needed in case of an eval with a binding and file/line @@ -51,9 +66,10 @@ class Pry @method.source_file == @file && @method.source_range.include?(@line) end - def invalid_file?(file) - file != Pry.eval_path && - (file =~ /(\(.*\))|<.*>/ || file == "" || file == "-e") + def code? + !!code + rescue MethodSource::SourceNotFoundError + false end end diff --git a/test/test_code.rb b/test/test_code.rb index 8c7a7eb0..e9e4cfbb 100644 --- a/test/test_code.rb +++ b/test/test_code.rb @@ -26,7 +26,7 @@ describe Pry::Code do should 'raise an error if the file doesn\'t exist' do proc do Pry::Code.from_file('/knalkjsdnalsd/alkjdlkq') - end.should.raise(Pry::CommandError) + end.should.raise(MethodSource::SourceNotFoundError) end end