The twitch package

[Tags:library, mit, test]

Twitch is a monadic DSL and library for file watching. It conveniently utilizes do notation in the style of Shake and clay to expose the functionality of the fsnotify cross-platform file system watcher.

Here is an example that converts Markdown files to HTML and reloads Safari whenever the input files change.

import Twitch
import System.Process ( system )

main = defaultMain $ do
*.md   |> \filePath -> system $ pandoc -t html  ++ filePath
*.html |> \_ -> system $ osascript refreshSafari.AppleScript

[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.1.0, 0.1.2.0, 0.1.2.1, 0.1.2.2, 0.1.3.0, 0.1.4.0, 0.1.5.0, 0.1.5.1, 0.1.6.1, 0.1.7.0, 0.1.7.1
Dependencies base (>=4.5 && <5.0), data-default, directory, filepath, fsnotify, Glob, optparse-applicative, time, transformers [details]
License MIT
Copyright (c) Jonathan Fischoff 2015
Author Jonathan Fischoff, Andreas Schacker
Maintainer jonathangfischoff@gmail.com
Category System
Home page https://github.com/jfischoff/twitch
Source repository head: git clone https://github.com/jfischoff/twitch.git
Uploaded Wed Aug 17 07:13:37 UTC 2016 by AndreasSchacker
Distributions NixOS:0.1.7.1
Downloads 1599 total (25 in the last 30 days)
Votes
1 []
Status Docs available [build log]
Last success reported on 2016-08-17 [all 1 reports]
Hackage Matrix CI

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees

Readme for twitch

Readme for twitch-0.1.7.1

Hackage travis build status

Twitch is monadic DSL and library for file watching. It conveniently utilizes 'do' notation in the style of Shake and clay to expose the functionality of the fsnotify cross-platform file system watcher.

Here is an example that converts Markdown files to Html and reloads Safari whenever the input files change.

{-# LANGUAGE OverloadedStrings #-}
import Twitch
import System.Process ( system )

main = defaultMain $ do
   "*.md"   |> \filePath -> system ("pandoc -t html " ++ filePath)
   "*.html" |> \_ -> system "osascript refreshSafari.AppleScript"

Rules are specified in the Dep (for Dependency) monad. The library takes advantage of the OverloadedStrings extension to create a Dep value from a glob pattern.

After creating a Dep value using a glob, event callbacks are added using prefix or infix API.

There are three types of events, add, modify and delete. In many cases, the add and modify responses are the same, so an 'add and modify' API is provided

In the example above an 'add and modify' callback was added to both the ".md" and ".html" globs using the |> operator.

All this is the common case, differing callbacks can be added with |+ (or add) and |% (or modify) functions. Finally, delete callbacks are added with |- (of delete).

Here is a more complex usage example, handling all three events seperately.

handleHaskellFiles :: Dep
handleHaskellFiles = "src/**/*.hs" |+ addToCabalFile |% reloadFile |- removeFromCabalFile

The glob above is also more complicated and incorporates a recursive wildcard. For complete documentation on the glob syntax, consult the Glob library's documentation.

Since a command pattern is calling system commands with a file path, a useful addition to twitch is the file-command-qq quasiquoter, which is the package of the same name.

Here is a slightly more complicated version the example from earlier, using the file-command-qq quasiquoter.

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
import Twitch
import FileCommand

main = defaultMain $ do
   "*.md"    |> [s|pandoc -t html -o$directory$basename-test.html $path|]
   "*.html"  |> [s|osascript refreshSafari.AppleScript|]