Sha256: a67f27d9ebf9b48e3e9b2a8f447f537ee83e1b6b1d99e4ebf0e80937bf99c1eb

Contents?: true

Size: 966 Bytes

Versions: 2

Compression:

Stored size: 966 Bytes

Contents

require 'readline'

module Ichabod
  class Repl
    Prompt = 'js> '
    AwaitingInput = '?> '
    Result = '=> '
    HistoryFile = File.join(File.expand_path('~'), '.ichabod_history')

    def self.start(dom = nil)
      load_history
      @parser = Ichabod::Runtime.new(:dom => dom)

      loop do
        input = Readline.readline(Prompt)
        quit if input.nil?

        begin
          puts Result + @parser.eval(input).inspect.to_s
        rescue => e
          save_history
          raise e
        else
          Readline::HISTORY.push(input)
        end
      end
    end

    def self.load_history
      return unless File.exists? HistoryFile
      File.readlines(HistoryFile).each do |line|
        Readline::HISTORY.push line.chomp
      end
    end

    def self.save_history
      File.open(HistoryFile, 'w') do |f|
        f.puts Readline::HISTORY.to_a.join("\n")
      end
    end

    def self.quit
      save_history
      exit
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ichabod-0.0.2 lib/ichabod/repl.rb
ichabod-0.0.1 lib/ichabod/repl.rb