From 8cd58b71ab6a617635c28e3738d03f1527769ae6 Mon Sep 17 00:00:00 2001 From: Dmitry Astapov Date: Sun, 19 Nov 2017 13:07:07 +0000 Subject: [PATCH] bal: show percentage of budget spent --- hledger/Hledger/Cli/Commands/Balance.hs | 10 +++++++- hledger/hledger.cabal | 3 +++ hledger/package.yaml | 3 +++ tests/budget/budget.test | 34 ++++++++++++------------- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index 83ca1ac58..3e4af3743 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -252,6 +252,7 @@ import qualified Data.Map as Map -- import Data.Monoid import qualified Data.Text as T import System.Console.CmdArgs.Explicit as C +import Data.Decimal (roundTo) import Text.CSV import Test.HUnit import Text.Printf (printf) @@ -559,7 +560,14 @@ multiBalanceReportWithBudgetAsText opts budget r = HistoricalBalance -> "Ending balances (historical)" showcell (real, Nothing) = showamt real showcell (real, Just budget) = - printf "%s [%s]" (showamt real) (showamt budget) + case percentage real budget of + Just pct -> printf "%s [%s%% of %s]" (showamt real) (show $ roundTo 0 pct) (showamt budget) + Nothing -> printf "%s [%s]" (showamt real) (showamt budget) + percentage real budget = + case (real, budget) of + (Mixed [a1], Mixed [a2]) | acommodity a1 == acommodity a2 && aquantity a2 /= 0 -> + Just $ 100 * aquantity a1 / aquantity a2 + _ -> Nothing showamt | color_ opts = cshowMixedAmountOneLineWithoutPrice | otherwise = showMixedAmountOneLineWithoutPrice -- combine reportTable budgetTable will combine them into a single table where cells diff --git a/hledger/hledger.cabal b/hledger/hledger.cabal index 2b6f4a957..eadc13ec6 100644 --- a/hledger/hledger.cabal +++ b/hledger/hledger.cabal @@ -113,6 +113,7 @@ library , text >=0.11 , utf8-string >=0.3.5 && <1.1 , wizards ==1.0.* + , Decimal if (!(os(windows))) && (flag(terminfo)) build-depends: terminfo @@ -192,6 +193,7 @@ executable hledger , text >=0.11 , utf8-string >=0.3.5 && <1.1 , wizards ==1.0.* + , Decimal if (!(os(windows))) && (flag(terminfo)) build-depends: terminfo @@ -241,6 +243,7 @@ test-suite test , text >=0.11 , utf8-string >=0.3.5 && <1.1 , wizards ==1.0.* + , Decimal , test-framework , test-framework-hunit if (!(os(windows))) && (flag(terminfo)) diff --git a/hledger/package.yaml b/hledger/package.yaml index f32e18c1e..9560ab69e 100644 --- a/hledger/package.yaml +++ b/hledger/package.yaml @@ -149,6 +149,7 @@ library: - text >=0.11 - utf8-string >=0.3.5 && <1.1 - wizards ==1.0.* + - Decimal executables: hledger: @@ -178,6 +179,7 @@ executables: - text >=0.11 - utf8-string >=0.3.5 && <1.1 - wizards ==1.0.* + - Decimal tests: test: @@ -204,6 +206,7 @@ tests: - text >=0.11 - utf8-string >=0.3.5 && <1.1 - wizards ==1.0.* + - Decimal - test-framework - test-framework-hunit diff --git a/tests/budget/budget.test b/tests/budget/budget.test index e7c457dc9..7d7e22bc6 100644 --- a/tests/budget/budget.test +++ b/tests/budget/budget.test @@ -32,14 +32,14 @@ hledger bal -D -b 2016-12-01 -e 2016-12-04 -f - --budget >>> Balance changes in 2016/12/01-2016/12/03: - || 2016/12/01 2016/12/02 2016/12/03 -=======================++======================================= - :expenses || 0 0 $40 - assets:cash || $-10 [$-25] $-14 [$-25] $-51 [$-25] - expenses:food || $10 [$10] $9 [$10] $11 [$10] - expenses:leisure || 0 [$15] $5 [$15] 0 [$15] ------------------------++--------------------------------------- - || 0 0 0 + || 2016/12/01 2016/12/02 2016/12/03 +=======================++============================================================= + :expenses || 0 0 $40 + assets:cash || $-10 [40% of $-25] $-14 [56% of $-25] $-51 [204% of $-25] + expenses:food || $10 [100% of $10] $9 [90% of $10] $11 [110% of $10] + expenses:leisure || 0 [$15] $5 [33% of $15] 0 [$15] +-----------------------++------------------------------------------------------------- + || 0 0 0 >>>2 >>>=0 @@ -78,15 +78,15 @@ hledger bal -D -b 2016-12-01 -e 2016-12-04 -f - --budget --show-unbudgeted >>> Balance changes in 2016/12/01-2016/12/03: - || 2016/12/01 2016/12/02 2016/12/03 -==================++======================================= - assets:cash || $-10 [$-25] $-14 [$-25] $-51 [$-25] - expenses:cab || 0 0 $15 - expenses:food || $10 [$10] $9 [$10] $11 [$10] - expenses:leisure || 0 [$15] $5 [$15] 0 [$15] - expenses:movies || 0 0 $25 -------------------++--------------------------------------- - || 0 0 0 + || 2016/12/01 2016/12/02 2016/12/03 +==================++============================================================= + assets:cash || $-10 [40% of $-25] $-14 [56% of $-25] $-51 [204% of $-25] + expenses:cab || 0 0 $15 + expenses:food || $10 [100% of $10] $9 [90% of $10] $11 [110% of $10] + expenses:leisure || 0 [$15] $5 [33% of $15] 0 [$15] + expenses:movies || 0 0 $25 +------------------++------------------------------------------------------------- + || 0 0 0 >>>2 >>>=0