lib/ego/runner.rb in ego-0.1.0 vs lib/ego/runner.rb in ego-0.2.0
- old
+ new
@@ -1,15 +1,14 @@
-require_relative 'version'
-require_relative 'options'
-require_relative 'filesystem'
-require_relative 'robot'
-require_relative 'handler'
+require_relative '../ego'
module Ego
# The Ego::Runner class, given an array of arguments, initializes the
# required objects and executes the request.
class Runner
+ PROMPT = 'ego, '.green.freeze
+ QUIT = /^q(uit)?|exit|(good)?bye$/.freeze
+
# Takes an array of arguments and parses them into options:
#
# runner = Ego::Runner.new(ARGV)
#
def initialize(argv)
@@ -29,14 +28,46 @@
@formatter.puts @options.usage
exit(-1) if @options.usage_error
when :version
@formatter.puts "ego v#{Ego::VERSION}"
+ when :shell
+ init_robot
+ start_repl
else
- robot = Ego::Robot.new(@options, @formatter)
- Ego::Handler.load Ego::Filesystem.user_handlers
- Ego::Handler.load Ego::Filesystem.builtin_handlers
- Ego::Handler.dispatch robot, @options.query
+ init_robot
+ handle_query @options.query
end
+ end
+
+ protected
+
+ def init_robot
+ @robot = Ego::Robot.new(@options, @formatter)
+ Ego::Handler.load Ego::Filesystem.user_handlers
+ Ego::Handler.load Ego::Filesystem.builtin_handlers
+ end
+
+ def handle_query(query)
+ Ego::Handler.dispatch @robot, query
+ end
+
+ def prompt
+ Readline.readline(PROMPT, true)
+ end
+
+ def start_repl
+ require 'readline'
+
+ # Store the state of the terminal
+ stty_save = `stty -g`.chomp
+
+ loop do
+ query = prompt
+ break if query.nil? || query.strip =~ QUIT
+ handle_query query.strip
+ end
+ rescue Interrupt => e
+ system('stty', stty_save) # Restore state
end
end
end