From cd545d5207113ebf6be2c5f635e477a8fc0e1957 Mon Sep 17 00:00:00 2001 From: Brian John Date: Sun, 2 Oct 2011 16:46:20 -0500 Subject: [PATCH] fix issue where interator thread was continuing to capture input from stdin while a guard is being executed --- lib/guard.rb | 7 +++++-- lib/guard/interactor.rb | 34 ++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/guard.rb b/lib/guard.rb index f150371..ae29bd2 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -29,6 +29,8 @@ module Guard # @option options [Boolean] watch_all_modifications watches all file modifications if true # def setup(options = {}) + @lock = Mutex.new + @options = options @guards = [] @groups = [Group.new(:default)] @@ -41,8 +43,6 @@ module Guard debug_command_execution if @options[:debug] - @lock = Mutex.new - self end @@ -180,9 +180,12 @@ module Guard @lock.synchronize do begin + @interactor.stop_if_not_current yield rescue Interrupt end + + @interactor.start end end diff --git a/lib/guard/interactor.rb b/lib/guard/interactor.rb index b648125..da335cf 100644 --- a/lib/guard/interactor.rb +++ b/lib/guard/interactor.rb @@ -11,28 +11,34 @@ module Guard # - Everything else => Run all # class Interactor - # Start the interactor in its own thread. # def start return if ENV["GUARD_ENV"] == 'test' - @thread = Thread.new do - while entry = $stdin.gets.chomp - entry.gsub! /\n/, '' - case entry - when 'stop', 'quit', 'exit', 's', 'q', 'e' - ::Guard.stop - when 'reload', 'r', 'z' - ::Guard::Dsl.reevaluate_guardfile - ::Guard.reload - when 'pause', 'p' - ::Guard.pause - else - ::Guard.run_all + if !@thread || @thread.stop? + @thread = Thread.new do + while entry = $stdin.gets.chomp + case entry + when 'stop', 'quit', 'exit', 's', 'q', 'e' + ::Guard.stop + when 'reload', 'r', 'z' + ::Guard::Dsl.reevaluate_guardfile + ::Guard.reload + when 'pause', 'p' + ::Guard.pause + else + ::Guard.run_all + end end end end end + + def stop_if_not_current + unless Thread.current == @thread + @thread.kill + end + end end end