Created
May 9, 2014 22:17
-
-
Save YoEight/d4137b99d899228a2a6d to your computer and use it in GitHub Desktop.
Unix tail using pipes library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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