1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

implement whereami -n (exclude line numbers)

This commit is contained in:
John Mair 2013-01-09 01:38:12 +01:00
parent 0608c0effa
commit 32eb6ca573
2 changed files with 26 additions and 2 deletions

View file

@ -5,7 +5,7 @@ class Pry
group 'Context' group 'Context'
banner <<-BANNER banner <<-BANNER
Usage: whereami [-q] [N] Usage: whereami [-qn] [N]
Describe the current location. If you use `binding.pry` inside a Describe the current location. If you use `binding.pry` inside a
method then whereami will print out the source for that method. method then whereami will print out the source for that method.
@ -17,6 +17,9 @@ class Pry
there's no code to show. This is used by pry in the default there's no code to show. This is used by pry in the default
before_session hook to show you when you arrive at a `binding.pry`. before_session hook to show you when you arrive at a `binding.pry`.
The `-n` flag can be used to hide line numbers so that code can be copy/pasted
effectively.
When pry was started on an Object and there is no associated method, When pry was started on an Object and there is no associated method,
whereami will instead output a brief description of the current whereami will instead output a brief description of the current
object. object.
@ -30,6 +33,7 @@ class Pry
def options(opt) def options(opt)
opt.on :q, :quiet, "Don't display anything in case of an error" opt.on :q, :quiet, "Don't display anything in case of an error"
opt.on :n, :"no-line-numbers", "Do not display line numbers."
end end
def code def code
@ -55,7 +59,7 @@ class Pry
set_file_and_dir_locals(@file) set_file_and_dir_locals(@file)
output.puts "\n#{text.bold('From:')} #{location}:\n\n" output.puts "\n#{text.bold('From:')} #{location}:\n\n"
output.puts code.with_line_numbers.with_marker(@line) output.puts code.with_line_numbers(use_line_numbers?).with_marker(marker)
output.puts output.puts
end end
@ -65,6 +69,14 @@ class Pry
opts.quiet? && (internal_binding?(target) || !code?) opts.quiet? && (internal_binding?(target) || !code?)
end end
def use_line_numbers?
!opts.present?(:n)
end
def marker
!opts.present?(:n) && @line
end
def top_level? def top_level?
target_self == TOPLEVEL_BINDING.eval("self") target_self == TOPLEVEL_BINDING.eval("self")
end end

View file

@ -111,6 +111,18 @@ describe "whereami" do
Object.remove_const(:Cor) Object.remove_const(:Cor)
end end
it 'should not show line numbers or marker when -n switch is used' do
class Cor
def blimey!
out = pry_eval(binding, 'whereami -n')
out.should =~ /^def/
out.should.not =~ /\=\>/
end
end
Cor.new.blimey!
Object.remove_const(:Cor)
end
it 'should use Pry.config.default_window_size for window size when outside a method context' do it 'should use Pry.config.default_window_size for window size when outside a method context' do
old_size, Pry.config.default_window_size = Pry.config.default_window_size, 1 old_size, Pry.config.default_window_size = Pry.config.default_window_size, 1
:litella :litella