Sha256: 050efe3d64c316ddbf8f88fe945f15b33a7e71e0becf86dbd156afb0b9541289

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 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 pry_indent
        @pry.instance_variable_get(:@indent)
      end
    
      def send_input(str)
        old_pry_config_color = ::Pry.config.color
        ::Pry.config.color = false
        @continued_expression = true
        @fiber.resume "#{str}\n"
        # TODO - indent with `pry_indent.current_prefix`
        # TODO - use proper pry prompt
        [@output.read_buffer, @continued_expression ? ".." : ">>"]
      ensure
        ::Pry.config.color = old_pry_config_color
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
better_errors-0.2.0 lib/better_errors/repl/pry.rb
better_errors-0.1.1 lib/better_errors/repl/pry.rb
better_errors-0.1.0 lib/better_errors/repl/pry.rb