From 0ae7acc6217b1a06c8a1628c7a6dc2487ab55736 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 1 Mar 2016 10:11:47 +0100 Subject: jobs --- tprint/src/Main.hs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'tprint/src/Main.hs') diff --git a/tprint/src/Main.hs b/tprint/src/Main.hs index a937181..dcdeb4f 100644 --- a/tprint/src/Main.hs +++ b/tprint/src/Main.hs @@ -9,8 +9,11 @@ import Data.Text (Text) import qualified Data.Text as T import qualified Data.ByteString.Lazy.Char8 as Lazy (ByteString) import qualified Data.ByteString.Lazy.Char8 as LCBS +import Data.Time -import Control.Monad +import Data.Foldable +import Data.List +import Data.Monoid import Control.Monad @@ -36,6 +39,7 @@ withOutput TPrint{..} a | otherwise = a stdout tprint :: TPrint -> Client IO -> Handle -> IO () + tprint TPrint{ operation = Printers, ..} Client{..} out = printers >>= format where format ps | (Human, _) <- output = mapM_ (\(PrinterId n, st) -> hPutStrLn out $ show n ++ "\t" ++ humanStatus st) $ Map.toAscList ps @@ -43,4 +47,24 @@ tprint TPrint{ operation = Printers, ..} Client{..} out = printers >>= format | otherwise = hPutStrLn out . dumpStr $ Map.toAscList ps humanStatus (Busy (JobId n)) = "busy printing job #" ++ show n humanStatus (Available) = "available" + +tprint TPrint{ operation = Jobs{..}, ..} Client{..} out = jobs printer jobRange timeRange >>= format + where format js + | (Human, _) <- output = mapM_ (\((JobId n), created, status) -> hPutStrLn out $ show n ++ "\t" ++ formatTime defaultTimeLocale "%Y-%m-%d %H:%M:%S %Z" created ++ "\t" ++ humanStatus status printer) . sortBy jSort $ toList js + | (JSON, _) <- output = LCBS.hPutStrLn out $ encodePretty js + | otherwise = hPutStrLn out . dumpStr $ toList js + jSort (id, time, status) (id', time', status') = queueSort status status' <> compare time' time <> compare id id' + where + compare' :: Ord a => a -> a -> Ordering + compare' + | (Queued _) <- status = compare + | otherwise = flip compare + humanStatus (Queued (PrinterId n)) Nothing = "queued at printer #" ++ show n + humanStatus (Queued _) _ = "queued" + humanStatus (Printing (PrinterId n)) Nothing = "printing on printer #" ++ show n + humanStatus (Printing _) _ = "printing" + humanStatus (Done) _ = "finished successfully" + humanStatus (Failed err) _ = "failed: " ++ show err + + tprint _ _ _ = undefined -- cgit v1.2.3