From c2153f196c64593a252bc0fbbc3d503628fe896f Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 1 Mar 2016 09:57:25 +0100 Subject: printers --- tprint/src/Main.hs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'tprint/src/Main.hs') diff --git a/tprint/src/Main.hs b/tprint/src/Main.hs index cd6e68b..a937181 100644 --- a/tprint/src/Main.hs +++ b/tprint/src/Main.hs @@ -1,4 +1,5 @@ {-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE ViewPatterns #-} import Data.Map (Map) import qualified Data.Map as Map @@ -6,10 +7,15 @@ import Data.Sequence (Seq) import qualified Data.Sequence as Seq 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 Control.Monad import Control.Monad import Text.Show.Pretty (dumpStr) +import Data.Aeson.Encode.Pretty (encodePretty) import System.IO @@ -19,9 +25,22 @@ import Options import Debug.Trace main :: IO () -main = withArgs (tprint <=< dumpOpts) +main = withArgs (main' <=< dumpOpts) where dumpOpts c@(TPrint{..}) = c <$ when (dumpOptions) (hPutStrLn stderr $ dumpStr c) - -tprint :: TPrint -> IO () -tprint = undefined + 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 + +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 + | (JSON , _) <- output = LCBS.hPutStrLn out $ encodePretty ps + | otherwise = hPutStrLn out . dumpStr $ Map.toAscList ps + humanStatus (Busy (JobId n)) = "busy printing job #" ++ show n + humanStatus (Available) = "available" +tprint _ _ _ = undefined -- cgit v1.2.3