diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-14 21:43:23 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-14 21:43:23 +0000 |
commit | 0609858031013f9d95f8104739811d6413331e9b (patch) | |
tree | c47d097d2984a088d7793f2f25dcdd6cb592be7b /server | |
parent | d4c2170b56b94497e37c94e5e3c9ee6f18a2ed43 (diff) | |
download | thermoprint-0609858031013f9d95f8104739811d6413331e9b.tar thermoprint-0609858031013f9d95f8104739811d6413331e9b.tar.gz thermoprint-0609858031013f9d95f8104739811d6413331e9b.tar.bz2 thermoprint-0609858031013f9d95f8104739811d6413331e9b.tar.xz thermoprint-0609858031013f9d95f8104739811d6413331e9b.zip |
First work on driver for generic (cheap) printers
Diffstat (limited to 'server')
-rw-r--r-- | server/src/Thermoprint/Server/Printer/Generic.hs | 100 | ||||
-rw-r--r-- | server/thermoprint-server.cabal | 4 | ||||
-rw-r--r-- | server/thermoprint-server.nix | 18 |
3 files changed, 114 insertions, 8 deletions
diff --git a/server/src/Thermoprint/Server/Printer/Generic.hs b/server/src/Thermoprint/Server/Printer/Generic.hs new file mode 100644 index 0000000..bf6ce3b --- /dev/null +++ b/server/src/Thermoprint/Server/Printer/Generic.hs | |||
@@ -0,0 +1,100 @@ | |||
1 | {-# LANGUAGE FlexibleContexts #-} | ||
2 | {-# LANGUAGE ViewPatterns #-} | ||
3 | {-# LANGUAGE OverloadedStrings #-} | ||
4 | |||
5 | module Thermoprint.Server.Printer.Generic | ||
6 | ( genericPrint | ||
7 | ) where | ||
8 | |||
9 | import Thermoprint.Printout | ||
10 | import Thermoprint.API (PrintingError(IOError)) | ||
11 | import Thermoprint.Server.Printer | ||
12 | |||
13 | import System.FileLock | ||
14 | import System.IO | ||
15 | |||
16 | import Data.Sequence (Seq, (|>), (<|), ViewL(..), viewl) | ||
17 | import qualified Data.Sequence as Seq | ||
18 | |||
19 | import qualified Data.ByteString.Lazy as Lazy (ByteString) | ||
20 | import qualified Data.ByteString.Lazy as LBS | ||
21 | import qualified Data.ByteString.Lazy.Char8 as CLBS | ||
22 | |||
23 | import qualified Data.Text.Lazy as Lazy (Text) | ||
24 | import qualified Data.Text.Lazy as TL | ||
25 | |||
26 | import Data.Encoding | ||
27 | import Data.Encoding.CP437 | ||
28 | import Data.Word | ||
29 | |||
30 | import Control.Monad | ||
31 | import Control.Monad.Reader | ||
32 | import Control.Monad.Trans.State | ||
33 | import Control.Monad.IO.Class | ||
34 | |||
35 | import Control.Monad.Trans.Resource | ||
36 | import Data.Acquire | ||
37 | |||
38 | import Control.Exception.Base (IOException) | ||
39 | import Control.Monad.Catch | ||
40 | |||
41 | import Data.Foldable | ||
42 | |||
43 | import Data.List (intersperse, genericReplicate, intercalate) | ||
44 | |||
45 | import Data.Int (Int64) | ||
46 | |||
47 | import Prelude hiding (mapM_, sequence_) | ||
48 | |||
49 | lockedFile :: FilePath -> Acquire (FileLock, Handle) | ||
50 | lockedFile f = mkAcquire ((,) <$> lockFile f Exclusive <*> openFile f AppendMode) (\(lock, h) -> () <$ hClose h <* unlockFile lock) | ||
51 | |||
52 | withLockedFile :: (MonadIO m, MonadMask m) => FilePath -> (Handle -> m a) -> m a | ||
53 | withLockedFile path f = withEx (lockedFile path) (f . snd) | ||
54 | |||
55 | genericPrint :: FilePath -> PrinterMethod | ||
56 | genericPrint path = PM $ flip catches handlers . withLockedFile path . print | ||
57 | where | ||
58 | print printout handle = liftIO $ runReaderT (bracket_ initialize (render printout) finalize) handle >> return Nothing | ||
59 | handlers = [ Handler $ return . Just . IOError . (show :: IOException -> String) | ||
60 | , Handler $ return . Just . EncError | ||
61 | ] | ||
62 | |||
63 | type Render = ReaderT Handle IO () | ||
64 | |||
65 | encode' :: ByteSink m => String -> m () | ||
66 | encode' = encode CP437 | ||
67 | |||
68 | width :: Int64 | ||
69 | width = 32 | ||
70 | |||
71 | esc :: Word8 | ||
72 | esc = 27 | ||
73 | |||
74 | escSequence :: [Word8] -> Render | ||
75 | escSequence = mapM_ pushWord8 . (esc:) | ||
76 | |||
77 | initialize :: Render | ||
78 | initialize = replicateM_ 2 $ escSequence [64] | ||
79 | |||
80 | finalize :: Render -- TODO: adjust this to produce proper padding | ||
81 | finalize = encode' "\n\n\n" | ||
82 | |||
83 | intersperse' :: b -> (a -> b) -> Seq a -> Seq b | ||
84 | intersperse' _ _ (viewl -> EmptyL) = Seq.empty | ||
85 | intersperse' _ f (viewl -> x :< (viewl -> EmptyL)) = Seq.singleton $ f x | ||
86 | intersperse' b f (viewl -> x :< xs) = f x <| b <| intersperse' b f xs | ||
87 | |||
88 | render :: Printout -> Render | ||
89 | render = sequence_ . intersperse' (encode' "\n\n") renderPar | ||
90 | |||
91 | renderPar :: Paragraph -> Render | ||
92 | renderPar = mapM_ renderChunk | ||
93 | where | ||
94 | renderChunk (Raw bs) = mapM_ pushWord8 $ LBS.unpack bs | ||
95 | renderChunk (Cooked block) = renderBlock block | ||
96 | |||
97 | renderBlock :: Block -> Render | ||
98 | renderBlock (VSpace n) = encode' $ genericReplicate n '\n' | ||
99 | renderBlock (NewlSep xs) = sequence_ intercalate (encode' "\n") . map renderBlock . toList $ xs | ||
100 | renderBlock (Line x) = undefined | ||
diff --git a/server/thermoprint-server.cabal b/server/thermoprint-server.cabal index 5783f06..5e9c61f 100644 --- a/server/thermoprint-server.cabal +++ b/server/thermoprint-server.cabal | |||
@@ -23,6 +23,7 @@ library | |||
23 | , Thermoprint.Server.Queue | 23 | , Thermoprint.Server.Queue |
24 | , Thermoprint.Server.Printer | 24 | , Thermoprint.Server.Printer |
25 | , Thermoprint.Server.Printer.Debug | 25 | , Thermoprint.Server.Printer.Debug |
26 | , Thermoprint.Server.Printer.Generic | ||
26 | other-modules: Thermoprint.Server.Database.Instances | 27 | other-modules: Thermoprint.Server.Database.Instances |
27 | -- other-extensions: | 28 | -- other-extensions: |
28 | build-depends: base >=4.8 && <5 | 29 | build-depends: base >=4.8 && <5 |
@@ -49,6 +50,9 @@ library | |||
49 | , warp >=3.1.9 && <4 | 50 | , warp >=3.1.9 && <4 |
50 | , mmorph >=1.0.4 && <2 | 51 | , mmorph >=1.0.4 && <2 |
51 | , extended-reals >=0.2.1 && <1 | 52 | , extended-reals >=0.2.1 && <1 |
53 | , filelock >=0.1.0 && <1 | ||
54 | , bytestring >=0.10.6 && <1 | ||
55 | , encoding >=0.8 && <1 | ||
52 | hs-source-dirs: src | 56 | hs-source-dirs: src |
53 | default-language: Haskell2010 | 57 | default-language: Haskell2010 |
54 | 58 | ||
diff --git a/server/thermoprint-server.nix b/server/thermoprint-server.nix index a33e6db..d4ebb60 100644 --- a/server/thermoprint-server.nix +++ b/server/thermoprint-server.nix | |||
@@ -1,6 +1,7 @@ | |||
1 | { mkDerivation, base, conduit, containers, data-default-class | 1 | { mkDerivation, base, bytestring, conduit, containers |
2 | , deepseq, dyre, either, exceptions, extended-reals, hspec, mmorph | 2 | , data-default-class, deepseq, dyre, either, encoding, exceptions |
3 | , monad-control, monad-logger, mtl, persistent, persistent-sqlite | 3 | , extended-reals, filelock, hspec, mmorph, monad-control |
4 | , monad-logger, mtl, persistent, persistent-sqlite | ||
4 | , persistent-template, QuickCheck, quickcheck-instances, resourcet | 5 | , persistent-template, QuickCheck, quickcheck-instances, resourcet |
5 | , servant-server, stdenv, stm, text, thermoprint-spec, time | 6 | , servant-server, stdenv, stm, text, thermoprint-spec, time |
6 | , transformers, wai, warp | 7 | , transformers, wai, warp |
@@ -12,16 +13,17 @@ mkDerivation { | |||
12 | isLibrary = true; | 13 | isLibrary = true; |
13 | isExecutable = true; | 14 | isExecutable = true; |
14 | libraryHaskellDepends = [ | 15 | libraryHaskellDepends = [ |
15 | base conduit containers data-default-class deepseq dyre either | 16 | base bytestring conduit containers data-default-class deepseq dyre |
16 | exceptions extended-reals mmorph monad-control monad-logger mtl | 17 | either encoding exceptions extended-reals filelock mmorph |
17 | persistent persistent-template resourcet servant-server stm text | 18 | monad-control monad-logger mtl persistent persistent-template |
18 | thermoprint-spec time transformers wai warp | 19 | resourcet servant-server stm text thermoprint-spec time |
20 | transformers wai warp | ||
19 | ]; | 21 | ]; |
20 | executableHaskellDepends = [ | 22 | executableHaskellDepends = [ |
21 | base monad-logger mtl persistent-sqlite resourcet | 23 | base monad-logger mtl persistent-sqlite resourcet |
22 | ]; | 24 | ]; |
23 | testHaskellDepends = [ | 25 | testHaskellDepends = [ |
24 | base hspec QuickCheck quickcheck-instances | 26 | base hspec QuickCheck quickcheck-instances thermoprint-spec |
25 | ]; | 27 | ]; |
26 | homepage = "http://dirty-haskell.org/tags/thermoprint.html"; | 28 | homepage = "http://dirty-haskell.org/tags/thermoprint.html"; |
27 | description = "Server for thermoprint-spec"; | 29 | description = "Server for thermoprint-spec"; |