Sha256: b3f49908a1d83415ec7a94cb492c6bf361d680a29cfd326a85043aa30ed90953

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

require "fiber"
require "pry"

module BetterErrors
  module REPL
    class Pry
      class Input
        def readline
          Fiber.yield
        end
      end
      
      class Output
        def initialize
          @buffer = ""
        end
        
        def puts(*args)
          args.each do |arg|
            @buffer << "#{arg.chomp}\n"
          end
        end
        
        def tty?
          false
        end
        
        def read_buffer
          @buffer
        ensure
          @buffer = ""
        end
      end
      
      def initialize(binding)
        @fiber = Fiber.new do
          @pry.repl binding
        end
        @input = Input.new
        @output = Output.new
        @pry = ::Pry.new input: @input, output: @output
        @pry.hooks.clear_all
        @continued_expression = false
        @pry.hooks.add_hook :after_read, "better_errors hacky hook" do
          @continued_expression = false
        end
        @fiber.resume
      end
    
      def send_input(str)
        local ::Pry.config, color: false, pager: false do
          @continued_expression = true
          @fiber.resume "#{str}\n"
          [@output.read_buffer, @continued_expression ? ".." : ">>"]
        end
      end
      
    private
      def local(obj, attrs)
        old_attrs = {}
        attrs.each do |k, v|
          old_attrs[k] = obj.send k
          obj.send "#{k}=", v
        end
        yield
      ensure
        old_attrs.each do |k, v|
          obj.send "#{k}=", v
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
better_errors-0.9.0 lib/better_errors/repl/pry.rb
better_errors-0.8.0 lib/better_errors/repl/pry.rb
better_errors-0.7.2 lib/better_errors/repl/pry.rb
better_errors-0.7.0 lib/better_errors/repl/pry.rb
better_errors-0.6.0 lib/better_errors/repl/pry.rb
better_errors-0.5.0 lib/better_errors/repl/pry.rb