class << PryRescue
  def exit_callbacks
    @exit_callbacks ||= []
  end

  def run_exit_callbacks
    Pry::rescue do
      exit_callbacks.dup.each(&:call)
    end
    TOPLEVEL_BINDING.pry unless any_exception_captured
  end
end

Kernel.at_exit { PryRescue.run_exit_callbacks }

module Kernel
  def at_exit(&block)
    PryRescue.exit_callbacks.push block
  end
end

class << PryRescue
  def exit_callbacks
    @exit_callbacks ||= []
  end

  def run_exit_callbacks
    Pry::rescue do
      exit_callbacks.dup.each(&:call)
    end
    unless any_exception_captured
      print "\n"
      TOPLEVEL_BINDING.pry
    end
  end
end

Kernel.at_exit { PryRescue.run_exit_callbacks }

module Kernel
  def at_exit(&block)
    PryRescue.exit_callbacks.push block
  end
end

Print newline after exit hooks
Print newline after exit hooks

Prevents the Pry shell from overwriting content from stdout that was not written with a newline.