diff --git a/hledger/Hledger/Cli/BalanceView.hs b/hledger/Hledger/Cli/BalanceView.hs index 2486fcabb..d24a96a1d 100644 --- a/hledger/Hledger/Cli/BalanceView.hs +++ b/hledger/Hledger/Cli/BalanceView.hs @@ -2,22 +2,39 @@ module Hledger.Cli.BalanceView ( BalanceView(..) + ,balanceviewmode ,balanceviewReport ) where -import Data.Time.Calendar -import Data.List +import Data.Time.Calendar (Day) +import Data.List (intercalate) import Data.Monoid (Sum(..), (<>)) -import qualified Data.Text as T +import System.Console.CmdArgs.Explicit import Hledger import Hledger.Cli.Balance import Hledger.Cli.CliOptions -data BalanceView = BV { bvname :: String +data BalanceView = BV { bvmode :: String + , bvaliases :: [String] + , bvhelp :: String + , bvname :: String , bvqueries :: [(String, Journal -> Query)] } +balanceviewmode :: BalanceView -> Mode RawOpts +balanceviewmode bv@BV{..} = (defCommandMode $ bvmode : bvaliases) { + modeHelp = bvhelp `withAliases` bvaliases + ,modeGroupFlags = Group { + groupUnnamed = [ + flagNone ["flat"] (\opts -> setboolopt "flat" opts) "show accounts as a list" + ,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "flat mode: omit N leading account name parts" + ] + ,groupHidden = [] + ,groupNamed = [generalflagsgroup1] + } + } + balanceviewQueryReport :: ReportOpts -> Day diff --git a/hledger/Hledger/Cli/Balancesheet.hs b/hledger/Hledger/Cli/Balancesheet.hs index c1e4f1050..d79cda3e7 100644 --- a/hledger/Hledger/Cli/Balancesheet.hs +++ b/hledger/Hledger/Cli/Balancesheet.hs @@ -11,38 +11,26 @@ module Hledger.Cli.Balancesheet ( ,tests_Hledger_Cli_Balancesheet ) where -import qualified Data.Text.Lazy.IO as LT import System.Console.CmdArgs.Explicit import Test.HUnit -import Text.Shakespeare.Text import Hledger import Hledger.Cli.CliOptions -import Hledger.Cli.Balance import Hledger.Cli.BalanceView +bsBV = BV "balancesheet" + ["bs"] + "show a balance sheet" + "Balance Sheet" + [ ("Assets", journalAssetAccountQuery) + , ("Liabilities", journalLiabilityAccountQuery) + ] balancesheetmode :: Mode RawOpts -balancesheetmode = (defCommandMode $ ["balancesheet"]++aliases) { - modeHelp = "show a balance sheet" `withAliases` aliases - ,modeGroupFlags = Group { - groupUnnamed = [ - flagNone ["flat"] (\opts -> setboolopt "flat" opts) "show accounts as a list" - ,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "flat mode: omit N leading account name parts" - ] - ,groupHidden = [] - ,groupNamed = [generalflagsgroup1] - } - } - where aliases = ["bs"] +balancesheetmode = balanceviewmode bsBV balancesheet :: CliOpts -> Journal -> IO () -balancesheet = balanceviewReport bv - where - bv = BV "Balance Sheet" - [ ("Assets", journalAssetAccountQuery) - , ("Liabilities", journalLiabilityAccountQuery) - ] +balancesheet = balanceviewReport bsBV tests_Hledger_Cli_Balancesheet :: Test tests_Hledger_Cli_Balancesheet = TestList diff --git a/hledger/Hledger/Cli/Cashflow.hs b/hledger/Hledger/Cli/Cashflow.hs index 288ec178d..cf97b5d2e 100644 --- a/hledger/Hledger/Cli/Cashflow.hs +++ b/hledger/Hledger/Cli/Cashflow.hs @@ -14,48 +14,25 @@ module Hledger.Cli.Cashflow ( ,tests_Hledger_Cli_Cashflow ) where -import qualified Data.Text.Lazy.IO as LT import System.Console.CmdArgs.Explicit import Test.HUnit -import Text.Shakespeare.Text import Hledger import Hledger.Cli.CliOptions -import Hledger.Cli.Balance +import Hledger.Cli.BalanceView +cfBV = BV "cashflow" + ["cf"] + "show a cashflow statement statement" + "Cashflow Statement" + [ ("Cash flows", journalCashAccountQuery) + ] cashflowmode :: Mode RawOpts -cashflowmode = (defCommandMode ["cashflow","cf"]) { - modeHelp = "show a cashflow statement" `withAliases` ["cf"] - ,modeGroupFlags = Group { - groupUnnamed = [ - flagNone ["flat"] (\opts -> setboolopt "flat" opts) "show accounts as a list" - ,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "flat mode: omit N leading account name parts" - ] - ,groupHidden = [] - ,groupNamed = [generalflagsgroup1] - } - } +cashflowmode = balanceviewmode cfBV --- | Print a simple cashflow statement. cashflow :: CliOpts -> Journal -> IO () -cashflow CliOpts{reportopts_=ropts} j = do - -- let lines = case lineFormatFromOpts ropts of Left err, Right ... - d <- getCurrentDay - let q = queryFromOpts d ropts - cashreport@(_,total) = balanceReport ropts (And [q, journalCashAccountQuery j]) j - -- operatingreport@(_,operating) = balanceReport ropts (And [q, journalOperatingAccountMatcher j]) j - -- investingreport@(_,investing) = balanceReport ropts (And [q, journalInvestingAccountMatcher j]) j - -- financingreport@(_,financing) = balanceReport ropts (And [q, journalFinancingAccountMatcher j]) j - -- total = operating + investing + financing - LT.putStr $ [lt|Cashflow Statement - -Cash flows: -#{balanceReportAsText ropts cashreport} -Total: --------------------- -#{padleft 20 $ showMixedAmountWithoutPrice total} -|] +cashflow = balanceviewReport cfBV tests_Hledger_Cli_Cashflow :: Test tests_Hledger_Cli_Cashflow = TestList diff --git a/hledger/Hledger/Cli/Incomestatement.hs b/hledger/Hledger/Cli/Incomestatement.hs index b36cec3ab..92ad9c393 100644 --- a/hledger/Hledger/Cli/Incomestatement.hs +++ b/hledger/Hledger/Cli/Incomestatement.hs @@ -11,48 +11,26 @@ module Hledger.Cli.Incomestatement ( ,tests_Hledger_Cli_Incomestatement ) where -import qualified Data.Text.Lazy.IO as LT import System.Console.CmdArgs.Explicit import Test.HUnit -import Text.Shakespeare.Text import Hledger import Hledger.Cli.CliOptions -import Hledger.Cli.Balance +import Hledger.Cli.BalanceView +isBV = BV "incomestatement" + ["is"] + "show an income statement" + "Income Statement" + [ ("Revenues", journalIncomeAccountQuery) + , ("Expenses", journalExpenseAccountQuery) + ] incomestatementmode :: Mode RawOpts -incomestatementmode = (defCommandMode $ ["incomestatement"]++aliases) { - modeHelp = "show an income statement" `withAliases` aliases - ,modeGroupFlags = Group { - groupUnnamed = [ - flagNone ["flat"] (\opts -> setboolopt "flat" opts) "show accounts as a list" - ,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "flat mode: omit N leading account name parts" - ] - ,groupHidden = [] - ,groupNamed = [generalflagsgroup1] - } - } - where aliases = ["is"] +incomestatementmode = balanceviewmode isBV --- | Print a simple income statement. incomestatement :: CliOpts -> Journal -> IO () -incomestatement CliOpts{reportopts_=ropts} j = do - d <- getCurrentDay - let q = queryFromOpts d ropts - incomereport@(_,income) = balanceReport ropts (And [q, journalIncomeAccountQuery j]) j - expensereport@(_,expenses) = balanceReport ropts (And [q, journalExpenseAccountQuery j]) j - total = income + expenses - LT.putStr $ [lt|Income Statement - -Revenues: -#{balanceReportAsText ropts incomereport} -Expenses: -#{balanceReportAsText ropts expensereport} -Total: --------------------- -#{padleft 20 $ showMixedAmountWithoutPrice total} -|] +incomestatement = balanceviewReport isBV tests_Hledger_Cli_Incomestatement :: Test tests_Hledger_Cli_Incomestatement = TestList