Sha256: 1e7fdcd7c00b742c10b4d8a9ab8809a3f05180da35c2575e7ecb608ed4a829d8

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

# encoding: UTF-8

# require 'watchr'

module Spontaneous::Cli
  class Console < ::Spontaneous::Cli::Thor
    Spontaneous = ::Spontaneous
    include Thor::Actions
    namespace :console

    default_task :open

    desc "#{namespace}:open", "Gives you console access to the current site"
    def open
      # script = Watchr::Script.new
      # script.watch('(lib|schema)/.*\\.rb') { puts 'reload!' } # doesn't block
      # Thread.new do
      #   Watchr::Controller.new(script, Watchr.handler.new).run
      # end


      ENV["SPOT_MODE"] = "console"
      prepare :console
      ARGV.clear
      ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
      require 'irb'
      boot!

      require 'irb/completion'
      require 'irb/ext/save-history'

      history_file = Spontaneous.root / ".irb_history"

      IRB.setup(nil)
      IRB.conf[:SAVE_HISTORY] = 100
      IRB.conf[:HISTORY_FILE] = history_file

      irb = IRB::Irb.new
      IRB.conf[:MAIN_CONTEXT] = irb.context

      irb.context.evaluate((<<-INIT), __LINE__)

        module Readline
          module History
            def self.write_log(line)
              File.open('#{history_file}', 'ab') {|f| f << "\#{line}\n"}
            end
          end

          alias :old_readline :readline
          def readline(*args)
            ln = old_readline(*args)
            begin
              History.write_log(ln) unless ln.strip.empty?
            rescue
            end
            ln
          end
        end


      INIT

      trap("SIGINT") do
        irb.signal_handle
      end
      catch(:IRB_EXIT) do
        irb.eval_input
      end
    end
  end # Console
end # Spontaneous::Cli

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spontaneous-0.2.0.alpha4 lib/spontaneous/cli/console.rb
spontaneous-0.2.0.alpha3 lib/spontaneous/cli/console.rb