From a6d63bdae8ca93f07724a9b100a5bd2094ad6971 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 1 Mar 2016 10:34:13 +0100 Subject: cleanup anticipating jobStatus --- tprint/src/Main.hs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'tprint/src/Main.hs') diff --git a/tprint/src/Main.hs b/tprint/src/Main.hs index f0f0186..971e655 100644 --- a/tprint/src/Main.hs +++ b/tprint/src/Main.hs @@ -39,9 +39,21 @@ main = withArgs (main' <=< dumpOpts) main' config@(TPrint{..}) = withOutput config $ tprint config (mkClient' baseUrl) withOutput :: TPrint -> (Handle -> IO a) -> IO a -withOutput TPrint{..} a - | (_, WriteFile f) <- output = withFile f WriteMode a - | otherwise = a stdout +withOutput TPrint{..} + | (_, WriteFile f) <- output = withFile f WriteMode + | otherwise = ($ stdout) + +withInput :: Input -> (Handle -> IO a) -> IO a +withInput (_, Stdin) = ($ stdin) +withInput (_, ReadFile f) = withFile f ReadMode + +humanJobStatus :: JobStatus -> Maybe (Range PrinterId) -> String +humanJobStatus (Queued (PrinterId n)) Nothing = "queued at printer #" ++ show n +humanJobStatus (Queued _) _ = "queued" +humanJobStatus (Printing (PrinterId n)) Nothing = "printing on printer #" ++ show n +humanJobStatus (Printing _) _ = "printing" +humanJobStatus (Done) _ = "finished successfully" +humanJobStatus (Failed err) _ = "failed: " ++ show err tprint :: TPrint -> Client IO -> Handle -> IO () @@ -55,16 +67,10 @@ tprint TPrint{ operation = Printers, ..} Client{..} out = printers >>= format 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 + | (Human, _) <- output = mapM_ (\((JobId n), created, status) -> hPutStrLn out $ show n ++ "\t" ++ formatTime defaultTimeLocale "%Y-%m-%d %H:%M:%S %Z" created ++ "\t" ++ humanJobStatus 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' - 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 TPrint{ operation = Drafts, ..} Client{..} out = drafts >>= format where format ds -- cgit v1.2.3