1 {-| 
    2 
    3 A ledger-compatible @register@ command.
    4 
    5 -}
    6 
    7 module Hledger.Cli.Register (
    8   register
    9  ,postingsReportAsText
   10  ,showPostingWithBalanceForVty
   11  ,tests_Hledger_Cli_Register
   12 ) where
   13 
   14 import Data.List
   15 import Data.Maybe
   16 import Test.HUnit
   17 import Text.Printf
   18 
   19 import Hledger
   20 import Prelude hiding (putStr)
   21 import Hledger.Utils.UTF8 (putStr)
   22 import Hledger.Cli.Options
   23 
   24 
   25 -- | Print a (posting) register report.
   26 register :: CliOpts -> Journal -> IO ()
   27 register CliOpts{reportopts_=ropts} j = do
   28   d <- getCurrentDay
   29   putStr $ postingsReportAsText ropts $ postingsReport ropts (optsToFilterSpec ropts d) j
   30 
   31 -- | Render a register report as plain text suitable for console output.
   32 postingsReportAsText :: ReportOpts -> PostingsReport -> String
   33 postingsReportAsText opts = unlines . map (postingsReportItemAsText opts) . snd
   34 
   35 -- | Render one register report line item as plain text. Eg:
   36 -- @
   37 -- date (10)  description (20)     account (22)            amount (11)  balance (12)
   38 -- DDDDDDDDDD dddddddddddddddddddd aaaaaaaaaaaaaaaaaaaaaa  AAAAAAAAAAA AAAAAAAAAAAA
   39 -- ^ displayed for first postings^
   40 --   only, otherwise blank
   41 -- @
   42 postingsReportItemAsText :: ReportOpts -> PostingsReportItem -> String
   43 postingsReportItemAsText _ (dd, p, b) = concatTopPadded [datedesc, pstr, " ", bal]
   44     where
   45       datedesc = case dd of Nothing -> replicate datedescwidth ' '
   46                             Just (da, de) -> printf "%s %s " date desc
   47                                 where
   48                                   date = showDate da
   49                                   desc = printf ("%-"++(show descwidth)++"s") $ elideRight descwidth de :: String
   50           where
   51             descwidth = datedescwidth - datewidth - 2
   52             datedescwidth = 32
   53             datewidth = 10
   54       pstr = showPostingForRegister p
   55       bal = padleft 12 (showMixedAmountWithoutPrice b)
   56 
   57 -- XXX
   58 showPostingWithBalanceForVty showtxninfo p b = postingsReportItemAsText defreportopts $ mkpostingsReportItem showtxninfo p b
   59 
   60 tests_Hledger_Cli_Register :: Test
   61 tests_Hledger_Cli_Register = TestList
   62  [
   63 
   64  ]