Created
June 12, 2011 18:25
-
-
Save Mon-Ouie/1021848 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
module PryDebug | |
class FileBreakpoint < Struct.new(:id, :file, :line) | |
def to_s | |
"breakpoint #{id} at #{file}:#{line}" | |
end | |
def is_at?(other_file, other_line) | |
other_file.end_with?(file) && other_line == line | |
end | |
end | |
Commands = Pry::CommandSet.new do | |
command "breakpoint", "adds a breakpoint" do |argument| | |
if argument =~ /(.+):(\d+)/ | |
file, line = $1, $2.to_i | |
if other = PryDebug.breakpoints.find { |bp| bp.is_at?(file, line) } | |
output.puts "breakpoint #{other.id} is already located at #{argument}" | |
else | |
bp = PryDebug.add_breakpoint(file, line) | |
output.puts "adedd #{bp}" | |
end | |
else | |
output.puts "usage: breakpoint FILE:LINE" | |
output.puts | |
output.puts "FILE can be foo.rb or /full/path/to/foo.rb" | |
end | |
end | |
command "file", "sets the file to start the debugger at" do |file| | |
PryDebug.file = file | |
output.puts "debugged file set to #{file}" | |
end | |
command "run", "starts the debugger" do |arg| | |
if arg | |
PryDebug.file = arg | |
end | |
if PryDebug.file and File.exist? PryDebug.file | |
throw :start_debugging!, :now! | |
else | |
output.puts "error: file does not exist: #{PryDebug.file}" | |
output.puts "create it or set a new file using the 'file' command." | |
end | |
end | |
end | |
ShortCommands = Pry::CommandSet.new Commands do | |
alias_command "b", "breakpoint" | |
alias_command "bp", "breakpoint" | |
alias_command "r", "run" | |
end | |
@breakpoints = [] | |
@file = nil | |
class << self | |
attr_reader :breakpoints | |
attr_accessor :file | |
end | |
module_function | |
def start | |
should_start = catch(:start_debugging!) do | |
Pry.start(TOPLEVEL_BINDING, :commands => ShortCommands) | |
end | |
if should_start == :now! | |
set_trace_func proc { |*args| PryDebug.trace_func(*args) } | |
load PryDebug.file | |
end | |
end | |
def trace_func(event, file, line, method, binding, class_name) | |
case event | |
when 'line' | |
if bp = PryDebug.breakpoints.find { |bp| bp.is_at?(file, line) } | |
puts "reached #{bp}" | |
Pry.start(binding, :commands => ShortCommands) | |
end | |
end | |
end | |
def add_breakpoint(file, line) | |
bp = FileBreakpoint.new(PryDebug.breakpoints.size, file, line) | |
PryDebug.breakpoints << bp | |
bp | |
end | |
end | |
if $PROGRAM_NAME == __FILE__ | |
PryDebug.start | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment