imp: pretty: Replace --pretty-tables argument with --pretty=WHEN

argument, which takes yes or no. Default is no for now.
This commit is contained in:
Stephen Morgan 2021-09-20 14:02:37 +10:00 committed by Simon Michael
parent fbccd7b7ca
commit bf3e82f780
9 changed files with 83 additions and 9 deletions

View File

@ -570,7 +570,7 @@ cumulativeSum value start = snd . M.mapAccumWithKey accumValued start
-- unless --no-elide is used. -- unless --no-elide is used.
balanceReportTableAsText :: ReportOpts -> Tab.Table T.Text T.Text WideBuilder -> TB.Builder balanceReportTableAsText :: ReportOpts -> Tab.Table T.Text T.Text WideBuilder -> TB.Builder
balanceReportTableAsText ReportOpts{..} = 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 where
renderCh renderCh
| not commodity_column_ || transpose_ = fmap (Tab.textCell Tab.TopRight) | not commodity_column_ || transpose_ = fmap (Tab.textCell Tab.TopRight)

View File

@ -141,7 +141,7 @@ data ReportOpts = ReportOpts {
,row_total_ :: Bool ,row_total_ :: Bool
,no_total_ :: Bool ,no_total_ :: Bool
,show_costs_ :: Bool -- ^ Whether to show costs for reports which normally don't show them ,show_costs_ :: Bool -- ^ Whether to show costs for reports which normally don't show them
,pretty_tables_ :: Bool ,pretty_ :: Bool
,sort_amount_ :: Bool ,sort_amount_ :: Bool
,percent_ :: Bool ,percent_ :: Bool
,invert_ :: Bool -- ^ if true, flip all amount signs in reports ,invert_ :: Bool -- ^ if true, flip all amount signs in reports
@ -190,7 +190,7 @@ defreportopts = ReportOpts
, row_total_ = False , row_total_ = False
, no_total_ = False , no_total_ = False
, show_costs_ = False , show_costs_ = False
, pretty_tables_ = False , pretty_ = False
, sort_amount_ = False , sort_amount_ = False
, percent_ = False , percent_ = False
, invert_ = False , invert_ = False
@ -211,6 +211,7 @@ rawOptsToReportOpts d rawopts =
let formatstring = T.pack <$> maybestringopt "format" rawopts let formatstring = T.pack <$> maybestringopt "format" rawopts
querystring = map T.pack $ listofstringopt "args" rawopts -- doesn't handle an arg like "" right querystring = map T.pack $ listofstringopt "args" rawopts -- doesn't handle an arg like "" right
(costing, valuation) = valuationTypeFromRawOpts rawopts (costing, valuation) = valuationTypeFromRawOpts rawopts
pretty = fromMaybe False $ alwaysneveropt "pretty" rawopts
format = case parseStringFormat <$> formatstring of format = case parseStringFormat <$> formatstring of
Nothing -> defaultBalanceLineFormat Nothing -> defaultBalanceLineFormat
@ -245,7 +246,7 @@ rawOptsToReportOpts d rawopts =
,sort_amount_ = boolopt "sort-amount" rawopts ,sort_amount_ = boolopt "sort-amount" rawopts
,percent_ = boolopt "percent" rawopts ,percent_ = boolopt "percent" rawopts
,invert_ = boolopt "invert" rawopts ,invert_ = boolopt "invert" rawopts
,pretty_tables_ = boolopt "pretty-tables" rawopts ,pretty_ = pretty
,color_ = useColorOnStdout -- a lower-level helper ,color_ = useColorOnStdout -- a lower-level helper
,transpose_ = boolopt "transpose" rawopts ,transpose_ = boolopt "transpose" rawopts
,commodity_column_= boolopt "commodity-column" rawopts ,commodity_column_= boolopt "commodity-column" rawopts
@ -300,6 +301,16 @@ balancecalcopt =
balanceaccumopt :: RawOpts -> BalanceAccumulation balanceaccumopt :: RawOpts -> BalanceAccumulation
balanceaccumopt = fromMaybe PerPeriod . balanceAccumulationOverride 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 -> Maybe BalanceAccumulation
balanceAccumulationOverride rawopts = choiceopt parse rawopts <|> reportbal balanceAccumulationOverride rawopts = choiceopt parse rawopts <|> reportbal
where where

View File

@ -219,6 +219,12 @@ reportflags = [
,"'never' or 'no': never." ,"'never' or 'no': never."
,"A NO_COLOR environment variable overrides this." ,"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. -- | Flags for selecting flat/tree mode, used for reports organised by account.
@ -238,6 +244,7 @@ hiddenflags :: [Flag RawOpts]
hiddenflags = [ hiddenflags = [
flagNone ["effective","aux-date"] (setboolopt "date2") "Ledger-compatible aliases for --date2" flagNone ["effective","aux-date"] (setboolopt "date2") "Ledger-compatible aliases for --date2"
,flagNone ["infer-value"] (setboolopt "infer-market-prices") "legacy flag that was renamed" ,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... -- | Common output-related flags: --output-file, --output-format...

View File

@ -310,7 +310,6 @@ balancemode = hledgerCommandMode
,flagNone ["no-total","N"] (setboolopt "no-total") "omit the final total row" ,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" ,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)" ,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 ["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 ["percent", "%"] (setboolopt "percent") "express values in percentage of each column's total"
,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign" ,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign"

View File

@ -71,7 +71,7 @@ roi CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{_rsReportOpts=ReportO
let let
ropts = _rsReportOpts rspec ropts = _rsReportOpts rspec
showCashFlow = boolopt "cashflow" rawopts showCashFlow = boolopt "cashflow" rawopts
prettyTables = pretty_tables_ prettyTables = pretty_
makeQuery flag = do makeQuery flag = do
q <- either usageError (return . fst) . parseQuery today . T.pack $ stringopt flag rawopts q <- either usageError (return . fst) . parseQuery today . T.pack $ stringopt flag rawopts
return . simplifyQuery $ And [queryFromFlags ropts{period_=PeriodAll}, q] return . simplifyQuery $ And [queryFromFlags ropts{period_=PeriodAll}, q]

View File

@ -82,7 +82,6 @@ compoundBalanceCommandMode CompoundBalanceCommandSpec{..} =
,flagNone ["no-total","N"] (setboolopt "no-total") "omit the final total row" ,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" ,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)" ,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 ["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 ["percent", "%"] (setboolopt "percent") "express values in percentage of each column's total"
,flagNone ["commodity-column"] (setboolopt "commodity-column") ,flagNone ["commodity-column"] (setboolopt "commodity-column")

View File

@ -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`. 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`. 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 ## Output format
Some commands (print, register, the balance commands) offer a choice of output format. Some commands (print, register, the balance commands) offer a choice of output format.

View File

@ -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 hledger -f balance-multicol.journal balance --pretty-tables -M
>>> >>>
Balance changes in 2012-12-01..2013-03-31: Balance changes in 2012-12-01..2013-03-31:

View File

@ -218,12 +218,12 @@ Balance Sheet 2017-12-31
=============++===================== =============++=====================
Net: || $1 $1 Net: || $1 $1
# 9. --pretty-tables uses unicode chars for borders # 9. --pretty=yes uses unicode chars for borders
< <
2016/1/1 2016/1/1
assets 1 assets 1
b b
$ hledger -f - balancesheet -M --pretty-tables $ hledger -f - balancesheet -M --pretty=yes
Balance Sheet 2016-01-31 Balance Sheet 2016-01-31
║ 2016-01-31 ║ 2016-01-31