Last active
January 1, 2016 13:49
-
-
Save myme/8154065 to your computer and use it in GitHub Desktop.
This file contains 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
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