Skip to content

Instantly share code, notes, and snippets.

@joshrotenberg
Forked from kfish/watch-read.hs
Created May 2, 2014 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshrotenberg/d36e0e65a79e75ab3e03 to your computer and use it in GitHub Desktop.
Save joshrotenberg/d36e0e65a79e75ab3e03 to your computer and use it in GitHub Desktop.
{-# OPTIONS -Wall #-}
module Main where
import Control.Concurrent (threadDelay)
import System.Environment (getArgs)
import System.INotify
main :: IO ()
main = do
args <- getArgs
if (null args)
then usage
else watch args
usage :: IO ()
usage = putStrLn "Usage: watch-read file ..."
watch :: [FilePath] -> IO ()
watch fs = withINotify $ \inotify -> do
print fs
mapM_ (\f -> addWatch inotify [Modify, CloseWrite] f (handleEvent f)) fs
threadDelay (10 * microsecsPerSec)
where
handleEvent :: FilePath -> Event -> IO ()
handleEvent f e = putStrLn (f ++ ": " ++ show e)
microsecsPerSec = 1000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment