Sha256: 0e77dd6d392fb4db2a30aceeb9e8fc4ab0eec90bed0a202dc60b409d8e55940a
Contents?: true
Size: 1.93 KB
Versions: 1
Compression:
Stored size: 1.93 KB
Contents
# ========================================================================== # Project: Spade - CommonJS Runtime # Copyright: ©2010 Strobe Inc. All rights reserved. # License: Licened under MIT license (see LICENSE) # ========================================================================== require 'readline' require 'fileutils' # Global object used for the shell. module Spade class Shell METHODS = [:to_s, :exit, :quit, :help, :evalrb, :inspectjs] def initialize @history_path = File.expand_path("~/.spadehistory") load_history end def inject(ctx) @ctx = ctx METHODS.each{|meth| @ctx[meth.to_s] = method(meth) } true end def to_s "[object Shell]" end def exit(status=0) save_history ensure @ctx.reactor.exit(status) end alias_method :quit, :exit def help(*args) <<-HELP print(msg) print msg to STDOUT exit(status = 0) exit the shell also: quit() evalrb(source) evaluate some ruby source HELP end def evalrb(str) Kernel.eval str, binding end def inspectjs(obj) # check for exact class match so we don't match things like arrays spacer = (obj.class == V8::Object) ? 2 : nil # the self[] uses the JS version of JSON json = @ctx['JSON'].stringify(obj, nil, spacer) # Some things can't be converted into json json || obj.inspect end private def load_history return unless File.exist?(@history_path) File.readlines(@history_path).each{|l| Readline::HISTORY << l.chomp } end def save_history FileUtils.mkdir_p(File.dirname(@history_path)) File.open(@history_path, 'w') do |f| Readline::HISTORY.to_a.last(100).each do |l| l.chomp! next if l.empty? next if l =~ /^(exit|quit)\(/ f.puts l end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
spade-0.1.1.1 | lib/spade/shell.rb |