Fix interacting with tools like ruby-debug. #137

Merged
merged 2 commits into from Sep 20, 2011
@@ -1,6 +1,9 @@
module Guard
class Interactor
+ class LockException < Exception; end
+ class UnlockException < Exception; end
+
attr_reader :locked
def initialize
@@ -9,31 +12,45 @@ def initialize
def start
return if ENV["GUARD_ENV"] == 'test'
- Thread.new do
+ @thread = 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
+ begin
+ if !@locked && (entry = $stdin.gets)
+ 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
+ end
end
+ rescue LockException
+ lock
+ rescue UnlockException
+ unlock
end
end
end
end
def lock
- @locked = true
+ if @thread == Thread.current
+ @locked = true
+ else
+ @thread.raise(LockException)
+ end
end
def unlock
- @locked = false
+ if @thread == Thread.current
+ @locked = false
+ else
+ @thread.raise(UnlockException)
+ end
end
end