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/Instances.hs | 6 +++++- tprint/src/Main.hs | 27 +++++++++++++++++++++++---- tprint/src/Options.hs | 4 ++-- 3 files changed, 30 insertions(+), 7 deletions(-) (limited to 'tprint/src') diff --git a/tprint/src/Instances.hs b/tprint/src/Instances.hs index cffb8b0..7814bbc 100644 --- a/tprint/src/Instances.hs +++ b/tprint/src/Instances.hs @@ -8,7 +8,7 @@ import Data.Time (UTCTime, formatTime, defaultTimeLocale) import Text.Show.Pretty (Value, PrettyVal(..), dumpStr) import qualified Text.Show.Pretty as PShow (Value(..)) -import Thermoprint.Client (Scheme(..), BaseUrl(..), PrinterId(..), JobId(..), DraftId(..), Range(..)) +import Thermoprint.Client (Scheme(..), BaseUrl(..), PrinterId(..), JobId(..), DraftId(..), Range(..), PrinterStatus(..), JobStatus(..), PrintingError(..), EncodingException(..)) instance PrettyVal Scheme instance PrettyVal BaseUrl @@ -23,3 +23,7 @@ instance PrettyVal UTCTime where instance PrettyVal Text where prettyVal = prettyVal . T.unpack +instance PrettyVal PrinterStatus +instance PrettyVal JobStatus +instance PrettyVal PrintingError +instance PrettyVal EncodingException 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 diff --git a/tprint/src/Options.hs b/tprint/src/Options.hs index 4b61d1c..30350cb 100644 --- a/tprint/src/Options.hs +++ b/tprint/src/Options.hs @@ -80,8 +80,8 @@ data Sink = Stdout | WriteFile FilePath deriving (Show, Generic, PrettyVal) supportedInputs, supportedOutputs :: [Format] -supportedInputs = [BBCode] -supportedOutputs = [Human] +supportedInputs = [BBCode, JSON] +supportedOutputs = [Human, Internal, JSON] cmdPrinters, cmdJobs, cmdJobCreate, cmdJob, cmdJobStatus, cmdJobDelete, cmdDrafts, cmdDraftCreate, cmdDraftReplace, cmdDraft, cmdDraftDelete, cmdDraftPrint :: ParserInfo Operation cmdPrinters = info cmdPrinters' $ progDesc "List all available printers" -- cgit v1.2.3