Skip to content

Instantly share code, notes, and snippets.

@myme
Last active January 1, 2016 13:49
Show Gist options
  • Save myme/8154065 to your computer and use it in GitHub Desktop.
Save myme/8154065 to your computer and use it in GitHub Desktop.
import Control.Exception (tryJust)
import Control.Monad (guard)
import Data.Time
import GHC.IO.Handle
import System.Console.GetOpt
import System.Environment
import System.IO.Error
import System.Process
import System.Locale
annotateLine :: FormatTime t => t -> String -> String
annotateLine time line = timeString ++ ": " ++ line
where timeString = formatTime defaultTimeLocale "%F %T" time
annotateIO :: Handle -> IO ()
annotateIO handle = do
input <- tryJust (guard . isEOFError) (hGetLine handle)
case input of
Left _ -> return ()
Right line -> do
time <- getCurrentTime
let output = annotateLine time line
putStrLn output
annotateIO handle
spawn :: String -> [String] -> IO Handle
spawn cmd args = do
(_, out, _, _) <- runInteractiveProcess cmd args Nothing Nothing
return out
main :: IO ()
main = do
let options = []
cliArgs <- getArgs
case getOpt Permute options cliArgs of
(_, cmd:args, []) -> do
out <- spawn cmd args
hSetBinaryMode out False
hSetBuffering out NoBuffering
annotateIO out
_ -> do
putStrLn "usage: annodate <COMMAND> [OPTIONS...]"
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment