Skip to content

Instantly share code, notes, and snippets.

@kfish
Created February 15, 2011 02:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kfish/826968 to your computer and use it in GitHub Desktop.
Save kfish/826968 to your computer and use it in GitHub Desktop.
Haskell inotify example: monitor file modifications
{-# 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