aboutsummaryrefslogtreecommitdiff
path: root/tprint/src/Main.hs
blob: f0f01869f34c4c50c2242363c30ade48dcd85eab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-}

import Data.Map (Map)
import qualified Data.Map as Map
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.ByteString.Lazy.Char8 as Lazy (ByteString)
import qualified Data.ByteString.Lazy.Char8 as LCBS
import Data.Time

import Data.Foldable
import Data.List
import Data.Monoid
import Data.Maybe
import Data.Either

import Control.Monad
import Control.Monad.Catch

import Text.Show.Pretty (dumpStr)
import Data.Aeson.Encode.Pretty (encodePretty)

import System.IO

import Thermoprint.Printout.BBCode
import Thermoprint.Client
import Options

import Debug.Trace

main :: IO ()
main = withArgs (main' <=< dumpOpts)
  where
    dumpOpts c@(TPrint{..}) = c <$ when (dumpOptions) (hPutStrLn stderr $ dumpStr c)
    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 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'
        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
          | (Human, _) <- output = mapM_ (\(DraftId n, fromMaybe "" . fmap T.unpack -> t) -> hPutStrLn out $ show n ++ "\t" ++ t) $ Map.toAscList ds
          | (JSON, _) <- output = LCBS.hPutStrLn out $ encodePretty ds
          | otherwise = hPutStrLn out . dumpStr $ Map.toAscList ds
        
tprint TPrint{ operation = Job{..}, ..} Client{..} out = job jobId >>= format
  where format p
          | (JSON, _) <- output = LCBS.hPutStrLn out $ encodePretty p
          | otherwise <- output = T.hPutStrLn out =<< either throwM return (cobbcode p)


tprint _ _ _ = undefined