Skip to content

Instantly share code, notes, and snippets.

@banister
Created June 17, 2011 17:27
Show Gist options
  • Save banister/1031846 to your computer and use it in GitHub Desktop.
Save banister/1031846 to your computer and use it in GitHub Desktop.
pry(main)> blame Pry#repl
John Mair def repl(target=TOPLEVEL_BINDING)
John Mair target = Pry.binding_for(target)
John Mair target_self = target.eval('self')
John Mair
John Mair repl_prologue(target)
Mon ouïe
John Mair # cannot rely on nesting.level as
John Mair # nesting.level changes with new sessions
John Mair nesting_level = nesting.size
John Mair
John Mair break_data = catch(:breakout) do
John Mair nesting.push [nesting.size, target_self, self]
John Mair loop do
John Mair rep(target)
John Mair end
John Mair end
John Mair
John Mair return_value = repl_epilogue(target, nesting_level, break_data)
Lee Jarvis return_value || target_self
John Mair end
pry(main)> show-command blame
From: ./lib/pry/extended_commands/experimental.rb @ line 15:
Number of lines: 20
command "blame", "Show blame for a method", :requires_gem => "grit" do |meth_name|
require 'grit'
if (meth = get_method_object(meth_name, target, {})).nil?
output.puts "Invalid method name: #{meth_name}."
next
end
repo ||= Grit::Repo.new(".")
start_line = meth.source_location.last
num_lines = meth.source.lines.count
authors = repo.blame(meth.source_location.first).lines.select do |v|
v.lineno >= start_line && v.lineno <= start_line + num_lines
end.map do |v|
v.commit.author.output(Time.new).split(/</).first.strip
end
lines_with_blame = []
meth.source.lines.zip(authors) { |line, author| lines_with_blame << ("#{author}".ljust(10) + colorize_code(line)) }
output.puts lines_with_blame.join
end
pry(main)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment