File system watching as Streamly streams.
Koz Ross 3177e716e7 Widen bounds, new version | 5 年之前 | |
---|---|---|
src | 5 年之前 | |
.gitignore | 5 年之前 | |
.vimrc | 5 年之前 | |
CHANGELOG.md | 5 年之前 | |
LICENSE.md | 5 年之前 | |
README.md | 5 年之前 | |
Setup.hs | 5 年之前 | |
cabal.project | 5 年之前 | |
streamly-fsnotify.cabal | 5 年之前 |
streamly-fsnotify
streamly
is an undoubtedly awesome library - fast, flexible, and
well-documented. File system watching is a natural fit for a streaming library,
and this is exactly what streamly-notify
provides you.
As an example, here is a program which watches /home/koz/c-project/
and any
of its subdirectories for added or modified C source files (which we take to be
anything with a .c
extension). This program then writes that the event
occurred, to what file, and when, forever.
{-# LANGUAGE LambdaCase #-}
import Streamly.FSNotify (EventPredicate,
hasExtension, is Directory, invert, isDeletion, conj,
watchTree)
import System.Path (FsPath, FileExt, fromFilePath)
import qualified Streamly.Prelude as SP
-- conj -> both must be true
-- invert -> true when the argument would be false and vice versa
isCSourceFile :: EventPredicate
isCSourceFile = hasExtension (FileExt "c") `conj` (invert isDirectory)
notDeletion :: EventPredicate
notDeletion = invert isDeletion
srcPath :: FsPath
srcPath = fromFilePath "/home/koz/c-project"
-- first value given by watchTree stops the watcher
-- we don't use it here, but if you want to, just call it
main :: IO ()
main = do (_, stream) <- watchTree srcPath (isCSourceFile `conj` notDeletion)
SP.drain . SP.mapM go $ stream
where go = \case (Added p t _) -> putStrLn ("Created: " ++ show p ++ " at " ++ show t)
(Modified p t _) -> putStrLn ("Modified: " ++ show p ++ " at " ++ show t)
_ -> pure ()
streamly
and fsnotify
do.We've test-built this library for GHCs 8.2.2 through 8.8.1 on GNU/Linux. In
theory, streamly-fsnotify
should work everywhere both streamly
and
fsnotify
will, which includes older GHCs (7.10) and other OSes (such as
Windows). However, we haven't tried it ourselves - let us know if you do!
This library is under the GNU General Public License, version 3 or later (SPDX
code GPL-3.0-or-later
). For more details, see the LICENSE.md
file.