From fe5498f6c22d6aafdeb83766ed606f171d393b9c Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 29 May 2009 10:02:14 +0000 Subject: [PATCH] various utilities & doc updates - commodities, daysInSpan, underline --- Ledger/Dates.hs | 5 +++++ Ledger/Ledger.hs | 7 +++++-- Ledger/RawLedger.hs | 2 +- Ledger/Utils.hs | 6 ++++++ Tests.hs | 3 +++ 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Ledger/Dates.hs b/Ledger/Dates.hs index 0e6bff69c..80f636e5d 100644 --- a/Ledger/Dates.hs +++ b/Ledger/Dates.hs @@ -67,6 +67,11 @@ splitspan startof next s@(DateSpan (Just b) (Just e)) | b >= e = [] | otherwise = [DateSpan (Just $ startof b) (Just $ next $ startof b)] ++ splitspan' startof next (DateSpan (Just $ next $ startof b) (Just e)) + +-- | Count the days in a DateSpan, or if it is open-ended return Nothing. +daysInSpan :: DateSpan -> Maybe Integer +daysInSpan (DateSpan (Just d1) (Just d2)) = Just $ diffDays d2 d1 +daysInSpan _ = Nothing -- | Parse a period expression to an Interval and overall DateSpan using -- the provided reference date. diff --git a/Ledger/Ledger.hs b/Ledger/Ledger.hs index c5997a0de..0a9e5ab01 100644 --- a/Ledger/Ledger.hs +++ b/Ledger/Ledger.hs @@ -156,7 +156,7 @@ ledgerSubAccounts :: Ledger -> Account -> [Account] ledgerSubAccounts l Account{aname=a} = map (ledgerAccount l) $ filter (`isSubAccountNameOf` a) $ accountnames l --- | List a ledger's transactions. +-- | List a ledger's "transactions", ie postings with transaction info attached. ledgerTransactions :: Ledger -> [Transaction] ledgerTransactions l = rawLedgerTransactions $ rawledger l @@ -168,7 +168,7 @@ ledgerAccountTree depth l = treemap (ledgerAccount l) $ treeprune depth $ accoun ledgerAccountTreeAt :: Ledger -> Account -> Maybe (Tree Account) ledgerAccountTreeAt l acct = subtreeat acct $ ledgerAccountTree 9999 l --- | The date span containing all the ledger's (filtered) transactions, +-- | The (fully specified) date span containing all the ledger's (filtered) transactions, -- or DateSpan Nothing Nothing if there are none. ledgerDateSpan :: Ledger -> DateSpan ledgerDateSpan l @@ -199,6 +199,9 @@ subaccounts = ledgerSubAccounts transactions :: Ledger -> [Transaction] transactions = ledgerTransactions +commodities :: Ledger -> [Commodity] +commodities = nub . rawLedgerCommodities . rawledger + accounttree :: Int -> Ledger -> Tree Account accounttree = ledgerAccountTree diff --git a/Ledger/RawLedger.hs b/Ledger/RawLedger.hs index 9eff60d78..e457950c7 100644 --- a/Ledger/RawLedger.hs +++ b/Ledger/RawLedger.hs @@ -164,7 +164,7 @@ rawLedgerConvertTimeLog t l0 = l0 { ledger_txns = convertedTimeLog ++ ledger_txn where convertedTimeLog = entriesFromTimeLogEntries t $ open_timelog_entries l0 --- | The date span containing all the raw ledger's transactions, +-- | The (fully specified) date span containing all the raw ledger's transactions, -- or DateSpan Nothing Nothing if there are none. rawLedgerDateSpan :: RawLedger -> DateSpan rawLedgerDateSpan rl diff --git a/Ledger/Utils.hs b/Ledger/Utils.hs index cd14e0347..49b7226e3 100644 --- a/Ledger/Utils.hs +++ b/Ledger/Utils.hs @@ -63,6 +63,12 @@ elideRight width s = True -> take (width - 2) s ++ ".." False -> s +underline :: String -> String +underline s = s' ++ replicate (length s) '-' ++ "\n" + where s' + | last s == '\n' = s + | otherwise = s ++ "\n" + -- | Join multi-line strings as side-by-side rectangular strings of the same height, top-padded. concatTopPadded :: [String] -> String concatTopPadded strs = intercalate "\n" $ map concat $ transpose padded diff --git a/Tests.hs b/Tests.hs index 45b25aaf6..c610e0d18 100644 --- a/Tests.hs +++ b/Tests.hs @@ -444,6 +444,9 @@ tests = [ "use the greatest precision" ~: do (rawLedgerPrecisions $ canonicaliseAmounts False $ rawLedgerWithAmounts ["1","2.00"]) `is` [2,2] + ,"commodities" ~: do + commodities ledger7 `is` [Commodity {symbol="$", side=L, spaced=False, comma=False, precision=2}] + ,"dateSpanFromOpts" ~: do let todaysdate = parsedate "2008/11/26" let opts `gives` spans = show (dateSpanFromOpts todaysdate opts) `is` spans