From bf3e82f780e24753124c66819b754c8477a15ccb Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Mon, 20 Sep 2021 14:02:37 +1000 Subject: [PATCH] imp: pretty: Replace --pretty-tables argument with --pretty=WHEN argument, which takes yes or no. Default is no for now. --- .../Hledger/Reports/MultiBalanceReport.hs | 2 +- hledger-lib/Hledger/Reports/ReportOptions.hs | 17 ++++++-- hledger/Hledger/Cli/CliOptions.hs | 7 +++ hledger/Hledger/Cli/Commands/Balance.hs | 1 - hledger/Hledger/Cli/Commands/Roi.hs | 2 +- hledger/Hledger/Cli/CompoundBalanceCommand.hs | 1 - hledger/hledger.m4.md | 15 +++++++ hledger/test/balance/pretty.test | 43 +++++++++++++++++++ hledger/test/balancesheet.test | 4 +- 9 files changed, 83 insertions(+), 9 deletions(-) diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index 16765facd..f3e094466 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -570,7 +570,7 @@ cumulativeSum value start = snd . M.mapAccumWithKey accumValued start -- unless --no-elide is used. balanceReportTableAsText :: ReportOpts -> Tab.Table T.Text T.Text WideBuilder -> TB.Builder balanceReportTableAsText ReportOpts{..} = - Tab.renderTableByRowsB def{Tab.tableBorders=False, Tab.prettyTable=pretty_tables_} renderCh renderRow + Tab.renderTableByRowsB def{Tab.tableBorders=False, Tab.prettyTable=pretty_} renderCh renderRow where renderCh | not commodity_column_ || transpose_ = fmap (Tab.textCell Tab.TopRight) diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index 991bd19cd..ebc6b694a 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -141,7 +141,7 @@ data ReportOpts = ReportOpts { ,row_total_ :: Bool ,no_total_ :: Bool ,show_costs_ :: Bool -- ^ Whether to show costs for reports which normally don't show them - ,pretty_tables_ :: Bool + ,pretty_ :: Bool ,sort_amount_ :: Bool ,percent_ :: Bool ,invert_ :: Bool -- ^ if true, flip all amount signs in reports @@ -190,7 +190,7 @@ defreportopts = ReportOpts , row_total_ = False , no_total_ = False , show_costs_ = False - , pretty_tables_ = False + , pretty_ = False , sort_amount_ = False , percent_ = False , invert_ = False @@ -211,6 +211,7 @@ rawOptsToReportOpts d rawopts = let formatstring = T.pack <$> maybestringopt "format" rawopts querystring = map T.pack $ listofstringopt "args" rawopts -- doesn't handle an arg like "" right (costing, valuation) = valuationTypeFromRawOpts rawopts + pretty = fromMaybe False $ alwaysneveropt "pretty" rawopts format = case parseStringFormat <$> formatstring of Nothing -> defaultBalanceLineFormat @@ -245,7 +246,7 @@ rawOptsToReportOpts d rawopts = ,sort_amount_ = boolopt "sort-amount" rawopts ,percent_ = boolopt "percent" rawopts ,invert_ = boolopt "invert" rawopts - ,pretty_tables_ = boolopt "pretty-tables" rawopts + ,pretty_ = pretty ,color_ = useColorOnStdout -- a lower-level helper ,transpose_ = boolopt "transpose" rawopts ,commodity_column_= boolopt "commodity-column" rawopts @@ -300,6 +301,16 @@ balancecalcopt = balanceaccumopt :: RawOpts -> BalanceAccumulation balanceaccumopt = fromMaybe PerPeriod . balanceAccumulationOverride +alwaysneveropt :: String -> RawOpts -> Maybe Bool +alwaysneveropt opt rawopts = case maybestringopt opt rawopts of + Just "always" -> Just True + Just "yes" -> Just True + Just "y" -> Just True + Just "never" -> Just False + Just "no" -> Just False + Just "n" -> Just False + _ -> Nothing + balanceAccumulationOverride :: RawOpts -> Maybe BalanceAccumulation balanceAccumulationOverride rawopts = choiceopt parse rawopts <|> reportbal where diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index fb4450af9..4fb4b6a09 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -219,6 +219,12 @@ reportflags = [ ,"'never' or 'no': never." ,"A NO_COLOR environment variable overrides this." ]) + ,flagOpt "yes" ["pretty"] (\s opts -> Right $ setopt "pretty" s opts) "WHEN" + (unlines + ["Show prettier output, e.g. using unicode box-drawing characters." + ,"Accepts 'yes' (the default) or 'no'." + ,"If you provide an argument you must use '=', e.g. '--pretty=yes'." + ]) ] -- | Flags for selecting flat/tree mode, used for reports organised by account. @@ -238,6 +244,7 @@ hiddenflags :: [Flag RawOpts] hiddenflags = [ flagNone ["effective","aux-date"] (setboolopt "date2") "Ledger-compatible aliases for --date2" ,flagNone ["infer-value"] (setboolopt "infer-market-prices") "legacy flag that was renamed" + ,flagNone ["pretty-tables"] (setopt "pretty" "always") "legacy flag that was renamed" ] -- | Common output-related flags: --output-file, --output-format... diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index 7ef7ee5b0..351cc82cf 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -310,7 +310,6 @@ balancemode = hledgerCommandMode ,flagNone ["no-total","N"] (setboolopt "no-total") "omit the final total row" ,flagNone ["no-elide"] (setboolopt "no-elide") "don't squash boring parent accounts (in tree mode); don't show only 2 commodities per amount" ,flagReq ["format"] (\s opts -> Right $ setopt "format" s opts) "FORMATSTR" "use this custom line format (in simple reports)" - ,flagNone ["pretty-tables"] (setboolopt "pretty-tables") "use unicode to display prettier tables" ,flagNone ["sort-amount","S"] (setboolopt "sort-amount") "sort by amount instead of account code/name (in flat mode). With multiple columns, sorts by the row total, or by row average if that is displayed." ,flagNone ["percent", "%"] (setboolopt "percent") "express values in percentage of each column's total" ,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign" diff --git a/hledger/Hledger/Cli/Commands/Roi.hs b/hledger/Hledger/Cli/Commands/Roi.hs index 4615bee8b..df7dc518f 100644 --- a/hledger/Hledger/Cli/Commands/Roi.hs +++ b/hledger/Hledger/Cli/Commands/Roi.hs @@ -71,7 +71,7 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{_rsReportOpts=ReportO let ropts = _rsReportOpts rspec showCashFlow = boolopt "cashflow" rawopts - prettyTables = pretty_tables_ + prettyTables = pretty_ makeQuery flag = do q <- either usageError (return . fst) . parseQuery today . T.pack $ stringopt flag rawopts return . simplifyQuery $ And [queryFromFlags ropts{period_=PeriodAll}, q] diff --git a/hledger/Hledger/Cli/CompoundBalanceCommand.hs b/hledger/Hledger/Cli/CompoundBalanceCommand.hs index 3f40814f5..1e94817a6 100644 --- a/hledger/Hledger/Cli/CompoundBalanceCommand.hs +++ b/hledger/Hledger/Cli/CompoundBalanceCommand.hs @@ -82,7 +82,6 @@ compoundBalanceCommandMode CompoundBalanceCommandSpec{..} = ,flagNone ["no-total","N"] (setboolopt "no-total") "omit the final total row" ,flagNone ["no-elide"] (setboolopt "no-elide") "don't squash boring parent accounts (in tree mode); don't show only 2 commodities per amount" ,flagReq ["format"] (\s opts -> Right $ setopt "format" s opts) "FORMATSTR" "use this custom line format (in simple reports)" - ,flagNone ["pretty-tables"] (setboolopt "pretty-tables") "use unicode when displaying tables" ,flagNone ["sort-amount","S"] (setboolopt "sort-amount") "sort by amount instead of account code/name" ,flagNone ["percent", "%"] (setboolopt "percent") "express values in percentage of each column's total" ,flagNone ["commodity-column"] (setboolopt "commodity-column") diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 3eb9ae226..0a748bb81 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -1383,6 +1383,21 @@ hledger can optionally produce debug output (if enabled with `--debug=N`); this goes to stderr, and is not affected by `-o/--output-file`. If you need to capture it, use shell redirects, eg: `hledger bal --debug=3 >file 2>&1`. +## Output styling + +hledger commands can produce colour output when the terminal supports it. +This is controlled by the `--color/--colour` option: +- if the `NO_COLOR` environment variable is set, colour will not be used; +- otherwise, if the `--color/--colour` option is given a value of `yes` or `always` + (or `no` or `never`), colour will (or will not) be used; +- otherwise, colour will be used if the output (terminal or file) supports it. + +hledger commands can also use unicode box-drawing characters to produce prettier tables and output. +This is controlled by the `--pretty` option: +- if the `--pretty` option is given a value of `yes` or `always` + (or `no` or `never`), unicode characters will (or will not) be used; +- otherwise, unicode characters will not be used. + ## Output format Some commands (print, register, the balance commands) offer a choice of output format. diff --git a/hledger/test/balance/pretty.test b/hledger/test/balance/pretty.test index 9862ccb75..f2b9d657c 100644 --- a/hledger/test/balance/pretty.test +++ b/hledger/test/balance/pretty.test @@ -1,3 +1,46 @@ +# 1. Uses Unicode tables when given --pretty=yes +hledger -f balance-multicol.journal balance --pretty=yes -M +>>> +Balance changes in 2012-12-01..2013-03-31: + + ║ 2012-12 2013-01 2013-02 2013-03 +═════════════════╬════════════════════════════════════ + assets ║ 0 0 1 0 + assets:cash ║ 0 0 1 0 + assets:checking ║ 10 0 0 1 +─────────────────╫──────────────────────────────────── + ║ 10 0 2 1 +>>>=0 + +# 2. Uses Unicode tables when given --pretty +hledger -f balance-multicol.journal balance --pretty -M +>>> +Balance changes in 2012-12-01..2013-03-31: + + ║ 2012-12 2013-01 2013-02 2013-03 +═════════════════╬════════════════════════════════════ + assets ║ 0 0 1 0 + assets:cash ║ 0 0 1 0 + assets:checking ║ 10 0 0 1 +─────────────────╫──────────────────────────────────── + ║ 10 0 2 1 +>>>=0 + +# 3. Uses ASCII tables when given --pretty=no +hledger -f balance-multicol.journal balance --pretty=no -M +>>> +Balance changes in 2012-12-01..2013-03-31: + + || 2012-12 2013-01 2013-02 2013-03 +=================++==================================== + assets || 0 0 1 0 + assets:cash || 0 0 1 0 + assets:checking || 10 0 0 1 +-----------------++------------------------------------ + || 10 0 2 1 +>>>=0 + +# 4. Still accepts the legacy --pretty-tables for now hledger -f balance-multicol.journal balance --pretty-tables -M >>> Balance changes in 2012-12-01..2013-03-31: diff --git a/hledger/test/balancesheet.test b/hledger/test/balancesheet.test index 4b8f79c89..c96a6fde9 100644 --- a/hledger/test/balancesheet.test +++ b/hledger/test/balancesheet.test @@ -218,12 +218,12 @@ Balance Sheet 2017-12-31 =============++===================== Net: || $1 $1 -# 9. --pretty-tables uses unicode chars for borders +# 9. --pretty=yes uses unicode chars for borders < 2016/1/1 assets 1 b -$ hledger -f - balancesheet -M --pretty-tables +$ hledger -f - balancesheet -M --pretty=yes Balance Sheet 2016-01-31 ║ 2016-01-31