# HG changeset patch
# User Steve Losh <steve@stevelosh.com>
# Date 1456234910 0
# Node ID b045294f3cd16d084abac96be63fe1320391f20e
# Parent  7a725f724e089507f1fd11f47248cd2d13895e8c
Fix cutoff thrashing/skipping

This fixes two main bugs I've been seeing sporadically for a while:

1. `peat` would sometimes "thrash" and run the command a bunch of times in a row
   even though nothing was changing between runs.
2. Other times `peat` would miss a change and I'd have to manually save a file
   again to trigger another run.

Neither of these were too annoying to work around, so I haven't gotten around to
fixing them until now.

diff -r 7a725f724e08 -r b045294f3cd1 peat
--- a/peat	Tue Feb 23 13:36:05 2016 +0000
+++ b/peat	Tue Feb 23 13:41:50 2016 +0000
@@ -24,6 +24,8 @@
 get_paths = lambda: set()
 verbose = True
 dynamic = None
+last_run = None
+
 
 USAGE = r"""usage: %prog [options] COMMAND
 
@@ -54,10 +56,9 @@
     sys.exit(1)
 
 def check(paths):
-    cutoff = int(time.time() - interval)
     for p in paths:
         try:
-            if os.stat(p).st_mtime >= cutoff:
+            if os.stat(p).st_mtime >= last_run:
                 return True
         except OSError as e:
             # If the file has been deleted since we started watching, don't
@@ -69,6 +70,8 @@
     return False
 
 def run():
+    global last_run
+    last_run = time.time()
     log("running: " + command)
     subprocess.call(command, shell=True)