diff options
author | Ben Sima <ben@bsima.me> | 2021-02-15 21:05:00 -0500 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2021-02-15 21:05:00 -0500 |
commit | 1c796258b034653d3fa2a3fcf95d5acc738ee75d (patch) | |
tree | 0fca9d2284f78f13b93ec592b4e7c418e2293363 /hledger-overview.hs | |
parent | a73984dea9fa352ce31e2a9373be2f85afa7c103 (diff) |
hledger-overview: display Diff numbers
Diffstat (limited to 'hledger-overview.hs')
-rwxr-xr-x | hledger-overview.hs | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/hledger-overview.hs b/hledger-overview.hs index 652e852..1edc00d 100755 --- a/hledger-overview.hs +++ b/hledger-overview.hs @@ -51,9 +51,9 @@ main = do row "cred load" (Target 0 netCash) $ Just "net cash: credit spending minus USD cash assets. keep it positive" let monthlyNut = nut t $ balVal "^ex" let thisMonth = balVal "^ex date:thismonth" - row "month exp" (Limit monthlyNut thisMonth) $ Just $ display $ monthlyNut - thisMonth + row "month exp" (Limit monthlyNut thisMonth) $ Just $ display $ Diff $ monthlyNut - thisMonth row "net worth" netWorth Nothing - row " level" (level netWorth) (Just $ "+" <> (display $ netWorth - (unlevel $ roundTo' floor 1 $ level netWorth))) + row " level" (level netWorth) (Just $ display $ Diff $ netWorth - (unlevel $ roundTo' floor 1 $ level netWorth)) let levelup n = level netWorth & (+ n) & roundTo' floor 1 & unlevel & \target -> target - netWorth row " next" (levelup 0.1) (Just $ display $ roundTo' floor 1 $ level netWorth + 0.1) row " nnext" (levelup 0.2) (Just $ display $ roundTo' floor 1 $ level netWorth + 0.2) @@ -101,25 +101,35 @@ instance Display Chunk where instance Display Metric where display (Target expected actual) = color $ display actual - where color = if actual >= expected then fore green else fore red + where + color = if actual >= expected then fore green else fore red display (Limit expected actual) = color $ display actual - where color = if actual <= expected then fore green else fore red + where + color = if actual <= expected then fore green else fore red -- | Tag numbers for different kinds of displays -data Number = Months_ Quantity | Percent_ Quantity +data Number + = Months_ Quantity + | Percent_ Quantity + | Diff Quantity instance Display Number where display (Months_ q) = display q <> " months" display (Percent_ p) = display p <> "%" + display (Diff n) + | n > 0 = "+" <> display n + | n < 0 = "-" <> display n + | n == 0 = "==" instance Display Text where display t = chunk t -- | Pretty-print a number. From https://stackoverflow.com/a/61070523/1146898 instance Display Quantity where - display d = chunk $ T.intercalate "." $ case T.splitOn "." $ T.pack $ show $ roundTo 2 d of - x : xs -> (T.pack . reverse . go . reverse . T.unpack) x : xs - xs -> xs + display d = chunk $ + T.intercalate "." $ case T.splitOn "." $ T.pack $ show $ roundTo 2 d of + x : xs -> (T.pack . reverse . go . reverse . T.unpack) x : xs + xs -> xs where go (x : y : z : []) = x : y : z : [] go (x : y : z : ['-']) = x : y : z : ['-'] |