master/lib/guard/interactor.rb

60 lines
1.2 KiB
Ruby
Raw Normal View History

2010-10-03 21:00:33 +00:00
module Guard
2011-09-20 11:10:16 +00:00
# The interactor reads user input and triggers
# specific action upon them unless its locked.
#
# Currently the following actions are implemented:
# - stop, quit, exit, s, q, e => Exit Guard
# - reload, r, z => Reload Guard
# - pause, p => Pause Guard
# - Everything else => Run all
#
class Interactor
2011-09-01 21:24:45 +00:00
attr_reader :locked
2011-09-20 11:10:16 +00:00
# Initialize the interactor in unlocked state.
#
def initialize
@locked = false
end
2011-08-17 08:07:30 +00:00
2011-09-20 11:10:16 +00:00
# Start the interactor in a own thread.
#
def start
return if ENV["GUARD_ENV"] == 'test'
2011-09-20 11:10:16 +00:00
2011-08-17 08:07:30 +00:00
Thread.new do
loop do
if (entry = $stdin.gets) && !@locked
entry.gsub! /\n/, ''
case entry
when 'stop', 'quit', 'exit', 's', 'q', 'e'
::Guard.stop
when 'reload', 'r', 'z'
::Guard.reload
when 'pause', 'p'
::Guard.pause
else
::Guard.run_all
2011-08-17 08:07:30 +00:00
end
end
2010-10-03 21:00:33 +00:00
end
end
end
2011-09-20 11:10:16 +00:00
# Lock the interactor.
#
def lock
@locked = true
end
2011-09-20 11:10:16 +00:00
# Unlock the interactor.
#
def unlock
@locked = false
2011-08-17 08:07:30 +00:00
end
2010-10-03 21:00:33 +00:00
end
end