diff --git a/hledger-lib/Hledger/Query.hs b/hledger-lib/Hledger/Query.hs index 4bf1bf926..6c88f22c9 100644 --- a/hledger-lib/Hledger/Query.hs +++ b/hledger-lib/Hledger/Query.hs @@ -64,7 +64,7 @@ import Data.Monoid ((<>)) #endif import qualified Data.Text as T import Data.Time.Calendar -import Safe (readDef, maximumByDef, maximumDef, minimumDef) +import Safe (readDef, maximumByMay, maximumMay, minimumMay) import Text.Megaparsec import Text.Megaparsec.Char @@ -488,33 +488,33 @@ queryDateSpan' _ = nulldatespan -- | What is the earliest of these dates, where Nothing is earliest ? earliestMaybeDate :: [Maybe Day] -> Maybe Day -earliestMaybeDate = minimumDef Nothing +earliestMaybeDate = fromMaybe Nothing . minimumMay -- | What is the latest of these dates, where Nothing is earliest ? latestMaybeDate :: [Maybe Day] -> Maybe Day -latestMaybeDate = maximumDef Nothing +latestMaybeDate = fromMaybe Nothing . maximumMay -- | What is the earliest of these dates, where Nothing is the latest ? earliestMaybeDate' :: [Maybe Day] -> Maybe Day -earliestMaybeDate' = minimumDef Nothing . filter isJust +earliestMaybeDate' = fromMaybe Nothing . minimumMay . filter isJust -- | What is the latest of these dates, where Nothing is the latest ? latestMaybeDate' :: [Maybe Day] -> Maybe Day -latestMaybeDate' = maximumByDef Nothing compareNothingMax +latestMaybeDate' = fromMaybe Nothing . maximumByMay compareNothingMax where compareNothingMax Nothing Nothing = EQ compareNothingMax (Just _) Nothing = LT compareNothingMax Nothing (Just _) = GT compareNothingMax (Just a) (Just b) = compare a b --- | The depth limit this query specifies, or a large number if none. -queryDepth :: Query -> Int -queryDepth = minimumDef maxBound . queryDepth' +-- | The depth limit this query specifies, if it has one +queryDepth :: Query -> Maybe Int +queryDepth = minimumMay . queryDepth' where queryDepth' (Depth d) = [d] - queryDepth' (Or qs) = concatMap queryDepth' qs - queryDepth' (And qs) = concatMap queryDepth' qs - queryDepth' _ = [] + queryDepth' (Or qs) = concatMap queryDepth' qs + queryDepth' (And qs) = concatMap queryDepth' qs + queryDepth' _ = [] -- | The account we are currently focussed on, if any, and whether subaccounts are included. -- Just looks at the first query option. diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index 644066824..1f6ed9d9b 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -289,7 +289,7 @@ acctChangesFromPostings ropts q ps = HM.fromList [(aname a, a) | a <- as] as = filterAccounts . drop 1 $ accountsFromPostings ps filterAccounts = case accountlistmode_ ropts of ALTree -> filter ((depthq `matchesAccount`) . aname) -- exclude deeper balances - ALFlat -> clipAccountsAndAggregate (queryDepth depthq) . -- aggregate deeper balances at the depth limit. + ALFlat -> maybe id clipAccountsAndAggregate (queryDepth depthq) . -- aggregate deeper balances at the depth limit. filter ((0<) . anumpostings) depthq = dbg "depthq" $ filterQuery queryIsDepth q @@ -299,8 +299,8 @@ calculateAccountChanges :: ReportOpts -> Query -> [DateSpan] -> Map DateSpan [Posting] -> HashMap ClippedAccountName (Map DateSpan Account) calculateAccountChanges ropts q colspans colps - | queryDepth q == 0 = acctchanges <> elided - | otherwise = acctchanges + | queryDepth q == Just 0 = acctchanges <> elided + | otherwise = acctchanges where -- Transpose to get each account's balance changes across all columns. acctchanges = transposeMap colacctchanges @@ -461,7 +461,7 @@ displayedAccounts ropts q valuedaccts tallies = subaccountTallies . HM.keys $ HM.filterWithKey isInteresting valuedaccts isZeroRow balance = all (mixedAmountLooksZero . balance) - depth = queryDepth q + depth = fromMaybe maxBound $ queryDepth q -- | Sort the rows by amount or by account declaration order. sortRows :: ReportOpts -> Journal -> [MultiBalanceReportRow] -> [MultiBalanceReportRow] diff --git a/hledger-lib/Hledger/Reports/PostingsReport.hs b/hledger-lib/Hledger/Reports/PostingsReport.hs index 4b39781df..974ec3a9b 100644 --- a/hledger-lib/Hledger/Reports/PostingsReport.hs +++ b/hledger-lib/Hledger/Reports/PostingsReport.hs @@ -72,7 +72,7 @@ postingsReport ropts@ReportOpts{..} q j = where reportspan = adjustReportDates ropts q j whichdate = whichDateFromOpts ropts - depth = queryDepth q + depth = fromMaybe maxBound $ queryDepth q styles = journalCommodityStyles j priceoracle = journalPriceOracle infer_value_ j multiperiod = interval_ /= NoInterval diff --git a/hledger-ui/Hledger/UI/Main.hs b/hledger-ui/Hledger/UI/Main.hs index 2de17d69e..5491b39f0 100644 --- a/hledger-ui/Hledger/UI/Main.hs +++ b/hledger-ui/Hledger/UI/Main.hs @@ -92,7 +92,7 @@ runBrickUi uopts@UIOpts{cliopts_=copts@CliOpts{inputopts_=_iopts,reportopts_=rop reportopts_= ropts{ -- incorporate any depth: query args into depth_, -- any date: query args into period_ - depth_ =depthfromoptsandargs, + depth_ =queryDepth q, period_=periodfromoptsandargs, query_ =unwords -- as in ReportOptions, with same limitations $ collectopts filteredQueryArg (rawopts_ copts), @@ -107,8 +107,6 @@ runBrickUi uopts@UIOpts{cliopts_=copts@CliOpts{inputopts_=_iopts,reportopts_=rop } where q = queryFromOpts d ropts - depthfromoptsandargs = case queryDepth q of 99999 -> Nothing - d -> Just d datespanfromargs = queryDateSpan (date2_ ropts) $ fst $ parseQuery d (T.pack $ query_ ropts) periodfromoptsandargs = dateSpanAsPeriod $ spansIntersect [periodAsDateSpan $ period_ ropts, datespanfromargs] diff --git a/hledger/Hledger/Cli/Commands/Accounts.hs b/hledger/Hledger/Cli/Commands/Accounts.hs index 744b7afa8..b88483ae4 100644 --- a/hledger/Hledger/Cli/Commands/Accounts.hs +++ b/hledger/Hledger/Cli/Commands/Accounts.hs @@ -74,10 +74,10 @@ accounts CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do -- 3. if there's a depth limit, depth-clip and remove any no longer useful items clippedaccts = dbg1 "clippedaccts" $ - filter (matchesAccount acctq) $ -- clipping can leave accounts that no longer match the query, remove such - nub $ -- clipping can leave duplicates (adjacent, hopefully) - filter (not . T.null) $ -- depth:0 can leave nulls - map (clipAccountName depth) $ -- clip at depth if specified + filter (matchesAccount acctq) $ -- clipping can leave accounts that no longer match the query, remove such + nub $ -- clipping can leave duplicates (adjacent, hopefully) + filter (not . T.null) $ -- depth:0 can leave nulls + maybe id (map . clipAccountName) depth $ -- clip at depth if specified sortedaccts -- 4. print what remains as a list or tree, maybe applying --drop in the former case