Skip to content

Instantly share code, notes, and snippets.

@YoEight
Created May 9, 2014 22:17
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 YoEight/d4137b99d899228a2a6d to your computer and use it in GitHub Desktop.
Save YoEight/d4137b99d899228a2a6d to your computer and use it in GitHub Desktop.
Unix tail using pipes library
module Unix where
import Data.Foldable (traverse_)
import System.IO
import Control.Monad.Trans
import Pipes
import Pipes.Parse
import qualified Pipes.Prelude as P
produceFile :: FilePath -> Producer' String IO ()
produceFile path = do
h <- liftIO $ openFile path ReadMode
P.fromHandle h
tailParser :: Parser String IO ()
tailParser = go [] (0 :: Int) where
go xs len = do
iopt <- draw
case iopt of
Nothing -> liftIO $ traverse_ print $ reverse xs
Just l
| len == 10 -> go (l:init xs) len
| otherwise -> go (l:xs) (len+1)
unixTail :: FilePath -> IO ()
unixTail path = evalStateT tailParser (produceFile path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment