1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE NamedFieldPuns #-}
4 {-# LANGUAGE OverloadedStrings #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TypeFamilies #-}
7 module Hcompta.Format.Ledger.Write where
9 -- import Control.Applicative ((<$>), (<*))
10 import qualified Data.Char (isSpace)
11 import qualified Data.Functor.Compose
12 import qualified Data.Foldable
13 -- import Data.Foldable (Foldable)
14 import qualified Data.List
15 import qualified Data.List.NonEmpty
16 import qualified Data.Map.Strict as Data.Map
17 import qualified Data.Text.Lazy as TL
18 import qualified Data.Text as Text
19 import qualified Hcompta.Lib.Leijen as W
20 import Hcompta.Lib.Leijen (Doc, (<>))
21 import System.IO (Handle)
22 import qualified Text.Parsec as R hiding (satisfy, char)
23 import Text.Parsec (Stream, ParsecT)
25 import qualified Hcompta.Account as Account
26 import Hcompta.Account (Account)
27 import qualified Hcompta.Account.Read as Account.Read
28 import qualified Hcompta.Amount as Amount
29 import qualified Hcompta.Amount.Write as Amount.Write
30 import qualified Hcompta.Format.Ledger as Ledger
31 import Hcompta.Format.Ledger
34 , Posting(..), Posting_by_Account
37 import qualified Hcompta.Date.Write as Date.Write
38 import qualified Hcompta.Format.Ledger.Read as Read
39 -- import Hcompta.Lib.Consable (Consable(..))
40 import qualified Hcompta.Lib.Parsec as R
41 import Hcompta.Posting (Posting_Type(..))
42 import Hcompta.Tag (Tag)
46 account :: Posting_Type -> Account -> Doc
49 Posting_Type_Regular -> account_
50 Posting_Type_Virtual -> \acct ->
51 W.char Read.posting_type_virtual_begin <> do
53 W.char Read.posting_type_virtual_end
54 Posting_Type_Virtual_Balanced -> \acct ->
55 W.char Read.posting_type_virtual_balanced_begin <> do
57 W.char Read.posting_type_virtual_balanced_end
59 account_ :: Account -> Doc
62 Data.List.NonEmpty.toList $
63 Data.List.NonEmpty.intersperse
64 (W.bold $ W.yellow $ W.char Account.Read.section_sep)
65 (Data.List.NonEmpty.map account_name acct)
67 account_name :: Account.Name -> Doc
68 account_name = W.strict_text
70 -- ** Measure 'Account'
72 account_length :: Posting_Type -> Account -> Int
73 account_length type_ acct =
75 (\acc -> (1 +) . (acc +) . Text.length)
78 Posting_Type_Regular -> 0
79 Posting_Type_Virtual -> 2
80 Posting_Type_Virtual_Balanced -> 2
82 -- ** Measure 'Amount's
84 amounts_length :: Amount.By_Unit -> Int
90 (\n -> (3 +) . (+) (Amount.Write.amount_length n))
95 comment :: Comment -> Doc
98 W.char Read.comment_begin
99 <> (case Text.uncons com of
100 Just (c, _) | not $ Data.Char.isSpace c -> W.space
102 <> do W.if_color colorize (W.strict_text com)
107 pre <- R.many $ R.try $ do
108 ns <- R.many $ R.satisfy (\c -> c /= Read.tag_value_sep && not (Data.Char.isSpace c))
109 sh <- R.space_horizontal
111 ((W.text $ TL.pack $ concat pre) <>) <$> tags <* R.eof)
113 Left _ -> W.strict_text com
115 tags :: Stream s m Char => ParsecT s u m Doc
119 <*> (W.hcat <$> R.many (R.try (tag_sep >>= (\s -> (s <>) <$> tag_))))
121 tag_sep :: Stream s m Char => ParsecT s u m Doc
123 s <- R.char Read.tag_sep
124 sh <- R.many R.space_horizontal
126 do W.bold $ W.dullblack $ W.char s
127 <> do W.text $ TL.pack sh
128 tag_ :: Stream s m Char => ParsecT s u m Doc
132 foldMap (\s -> W.dullyellow (W.strict_text s) <> do
133 W.bold $ W.dullblack $ W.char Read.tag_value_sep) p <>
134 (W.red $ W.strict_text v)
136 comments :: Doc -> [Comment] -> Doc
139 Data.List.intersperse W.line .
140 Data.List.map (\c -> prefix <> comment c)
146 foldMap (\s -> W.dullyellow (W.strict_text s) <> W.char Read.tag_value_sep) p <>
147 (W.dullred $ W.strict_text v)
151 posting :: Posting_Lengths -> Posting_Type -> Posting -> Doc
152 posting max_posting_length type_
154 { posting_account=acct
156 , posting_comments=cmts
158 , posting_status=status_
163 case Data.Map.null posting_amounts of
164 True -> account type_ acct
166 let len_acct = account_length type_ acct in
167 let len_amts = amounts_length posting_amounts in
168 account type_ acct <> do
169 W.fill (2 + max_posting_length - (len_acct + len_amts)) (W.space <> W.space) <> do
171 (W.space <> (W.bold $ W.yellow $ W.char Read.amount_sep) <> W.space)
172 Amount.Write.amount posting_amounts
175 [c] -> W.space <> comment c
176 _ -> W.line <> do comments (W.text "\t ") cmts)
178 status :: Ledger.Status -> Doc
179 status = \x -> case x of
183 -- ** Measure 'Posting'
185 type Posting_Lengths = (Int)
189 -> Posting_by_Account
192 postings_lengths type_ ps pl =
196 ( account_length type_ (posting_account p)
197 + amounts_length (posting_amounts p) )
199 (Data.Functor.Compose.Compose ps)
201 -- * Write 'Transaction'
203 transaction :: Transaction -> Doc
204 transaction t = transaction_with_lengths (transaction_lengths t 0) t
206 transactions :: Foldable ts => ts Transaction -> Doc
208 let transaction_lengths_ =
209 Data.Foldable.foldr transaction_lengths 0 ts
212 transaction_with_lengths transaction_lengths_ t <>
213 (if W.is_empty doc then W.empty else W.line <> doc)
218 transaction_with_lengths :: Transaction_Lengths -> Transaction -> Doc
219 transaction_with_lengths
222 { transaction_code=code_
223 , transaction_comments_before
224 , transaction_comments_after
225 , transaction_dates=(first_date, dates)
226 , transaction_description
227 , transaction_postings
228 , transaction_virtual_postings
229 , transaction_balanced_virtual_postings
230 , transaction_status=status_
231 -- , transaction_tags
233 (case transaction_comments_before of
235 _ -> comments W.space transaction_comments_before <> W.line) <> do
237 Data.List.intersperse
238 (W.char Read.date_sep)
239 (Data.List.map Date.Write.date (first_date:dates))) <> do
241 True -> W.space <> status status_
242 False -> W.empty) <> do
244 (case transaction_description of
246 _ -> W.space <> (W.dullmagenta $ W.strict_text transaction_description)) <> do
248 (case transaction_comments_after of
250 _ -> comments W.space transaction_comments_after <> W.line) <> do
254 (W.intercalate W.line
255 (W.vsep . fmap (posting posting_lengths_ type_))
259 [ (Posting_Type_Regular , transaction_postings)
260 , (Posting_Type_Virtual , transaction_virtual_postings)
261 , (Posting_Type_Virtual_Balanced, transaction_balanced_virtual_postings)
265 code :: Ledger.Code -> Doc
266 code = \x -> case x of
268 t -> W.space <> W.char '(' <> W.strict_text t <> W.char ')'
270 -- ** Measure 'Transaction'
272 type Transaction_Lengths = Posting_Lengths
274 transaction_lengths :: Transaction -> Posting_Lengths -> Posting_Lengths
277 { transaction_postings
278 , transaction_virtual_postings
279 , transaction_balanced_virtual_postings
280 } posting_lengths_ = do
282 (flip (uncurry postings_lengths))
284 [ (Posting_Type_Regular, transaction_postings)
285 , (Posting_Type_Virtual, transaction_virtual_postings)
286 , (Posting_Type_Virtual_Balanced, transaction_balanced_virtual_postings)
293 , Monoid (ts Transaction)
294 ) => Journal (ts Transaction) -> Doc
295 journal Journal{ journal_transactions } =
296 transactions journal_transactions
302 { style_align :: Bool
303 , style_color :: Bool
312 show :: Style -> Doc -> TL.Text
313 show Style{style_color, style_align} =
316 then W.renderPretty style_color 1.0 maxBound
317 else W.renderCompact style_color
319 put :: Style -> Handle -> Doc -> IO ()
320 put Style{style_color, style_align} handle =
323 then W.renderPretty style_color 1.0 maxBound
324 else W.renderCompact style_color