c4f94236bde4

Add --smart-interval.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 17 Nov 2012 12:08:20 -0500
parents e5b500cb033e
children 4ee5a2152416
branches/tags (none)
files peat

Changes

--- a/peat	Wed Nov 14 13:45:38 2012 -0500
+++ b/peat	Sat Nov 17 12:08:20 2012 -0500
@@ -55,9 +55,11 @@
                      )
 
     # Main options
-    p.add_option('-i', '--interval', default='1000',
-                 help='interval between checks in milliseconds (default 1000)',
+    p.add_option('-i', '--interval', default=None,
+                 help='interval between checks in milliseconds',
                  metavar='N')
+    p.add_option('-I', '--smart-interval', dest='interval',
+                 help='determine the interval based on number of files watched (default)')
     p.add_option('-c', '--clear', default=True,
                  action='store_true', dest='clear',
                  help='clear screen before runs (default)')
@@ -91,6 +93,8 @@
     for p in paths:
         log("  " + p)
     log('')
+    log('Checking for changes every %d milliseconds.' % int(interval * 1000))
+    log('')
 
     run()
 
@@ -101,6 +105,14 @@
                 subprocess.check_call('clear')
             run()
 
+def smart_interval(count):
+    """Return the smart interval to use in milliseconds."""
+    if count >= 50:
+        return 1000
+    else:
+        sq = lambda n: n * n
+        return int(1000 * (1 - (sq(50.0 - count) / sq(50))))
+
 def main():
     global interval, command, clear, paths, verbose
 
@@ -110,7 +122,6 @@
         die("exactly one command must be given")
 
     command = args[0]
-    interval = int(options.interval) / 1000.0
     clear = options.clear
     verbose = options.verbose
     sep = options.sep
@@ -125,6 +136,12 @@
     paths = map(os.path.abspath, paths)
     paths = set(paths)
 
+    if options.interval:
+        interval = int(options.interval)
+    else:
+        interval = smart_interval(len(paths))
+    interval = interval / 1000.0
+
     for path in paths:
         if not os.path.exists(path):
             die('path to watch does not exist: ' + repr(path))