cli: --color/--colour option; smart emacs, windows autodetection (#1296)

This commit is contained in:
Simon Michael 2020-07-14 12:08:36 -07:00
parent 02043d4e65
commit 3452270241
9 changed files with 44 additions and 14 deletions

View File

@ -4,7 +4,10 @@ Options common to most hledger reports.
-}
{-# LANGUAGE OverloadedStrings, RecordWildCards, LambdaCase, DeriveDataTypeable #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Hledger.Reports.ReportOptions (
ReportOpts(..),
@ -54,7 +57,9 @@ import Data.Typeable (Typeable)
import Data.Time.Calendar
import Data.Default
import Safe
import System.Console.ANSI (hSupportsANSI)
import System.Console.ANSI (hSupportsANSIColor)
import System.Environment (lookupEnv)
import System.IO (stdout)
import Text.Megaparsec.Custom
@ -129,6 +134,10 @@ data ReportOpts = ReportOpts {
-- sign normalisation, converting normally negative subreports to
-- normally positive for a more conventional display.
,color_ :: Bool
-- ^ Whether to use ANSI color codes in text output.
-- Influenced by the --color/colour flag (cf CliOptions),
-- whether stdout is an interactive terminal, and the value of
-- TERM and existence of NO_COLOR environment variables.
,forecast_ :: Maybe DateSpan
,transpose_ :: Bool
} deriving (Show, Data, Typeable)
@ -172,7 +181,9 @@ rawOptsToReportOpts :: RawOpts -> IO ReportOpts
rawOptsToReportOpts rawopts = checkReportOpts <$> do
let rawopts' = checkRawOpts rawopts
d <- getCurrentDay
color <- hSupportsANSI stdout
no_color <- isJust <$> lookupEnv "NO_COLOR"
supports_color <- hSupportsANSIColor stdout
let colorflag = stringopt "color" rawopts
return defreportopts{
today_ = Just d
,period_ = periodFromRawOpts d rawopts'
@ -200,7 +211,10 @@ rawOptsToReportOpts rawopts = checkReportOpts <$> do
,percent_ = boolopt "percent" rawopts'
,invert_ = boolopt "invert" rawopts'
,pretty_tables_ = boolopt "pretty-tables" rawopts'
,color_ = color
,color_ = and [not no_color
,not $ colorflag `elem` ["never","no"]
,colorflag `elem` ["always","yes"] || supports_color
]
,forecast_ = forecastPeriodFromRawOpts d rawopts'
,transpose_ = boolopt "transpose" rawopts'
}

View File

@ -48,7 +48,7 @@ dependencies:
- base-compat-batteries >=0.10.1 && <0.12
- aeson >=1
- aeson-pretty
- ansi-terminal >=0.6.2.3
- ansi-terminal >=0.9
- array
- blaze-markup >=0.5.1
- bytestring

View File

@ -43,7 +43,7 @@ cpp-options: -DVERSION="1.18.99"
dependencies:
- hledger-lib >=1.18.99 && <1.19
- hledger >=1.18.99 && <1.19
- ansi-terminal >=0.6.2.3
- ansi-terminal >=0.9
- async
- base >=4.9 && <4.15
- base-compat-batteries >=0.10.1 && <0.12

View File

@ -188,6 +188,17 @@ reportflags = [
, "Also, in hledger-ui, make future transactions visible."
, "Note that = (and not a space) is required before PERIODEXP if you wish to supply it."
])
-- general output-related
,flagReq ["color","colour"] (\s opts -> Right $ setopt "color" s opts) "always|never|auto"
(unwords
["When to use color (or colour) in terminal output."
,"'auto' is the default; it tries to enable color when the terminal is color-supporting and interactive."
,"'always' or 'yes' forces color on, useful eg when piping output into 'less -R'."
,"'never' or 'no' forces color off."
,"A NO_COLOR environment variable will also force color off, overriding this option."
])
]
-- | Flags for selecting flat/tree mode, used in accounts/balance reports.

View File

@ -124,10 +124,8 @@ Some example formats:
### Colour support
The balance command shows negative amounts in red, if:
- the `TERM` environment variable is not set to `dumb`
- the output is not being redirected or piped anywhere
In terminal output, when colour is enabled,
the balance command shows negative amounts in red.
### Flat mode
@ -320,7 +318,7 @@ the width of multicommodity reports.
When the report is still too wide, a good workaround is to pipe it
into `less -RS` (-R for colour, -S to chop long lines).
Eg: `hledger bal -D | less -RS`.
Eg: `hledger bal -D --color=yes | less -RS`.
### Budget report

View File

@ -1723,12 +1723,17 @@ directory. These are typically prototypes and not guaranteed to work.
# ENVIRONMENT
m4_dnl Standard LEDGER_FILE description:
_LEDGER_FILE_
**COLUMNS**
The screen width used by the register command.
Default: the full terminal width.
m4_dnl Standard LEDGER_FILE description:
_LEDGER_FILE_
**NO_COLOR**
If this variable exists with any value,
hledger will not use ANSI color codes in terminal output.
This overrides the --color/--colour option.
# FILES

View File

@ -110,7 +110,7 @@ ghc-options:
dependencies:
- hledger-lib >=1.18.99 && <1.19
- aeson >=1
- ansi-terminal >=0.6.2.3
- ansi-terminal >=0.9
- base >=4.9 && <4.15
- base-compat-batteries >=0.10.1 && <0.12
- bytestring

View File

@ -16,6 +16,7 @@ packages:
extra-deps:
# use the latest base-compat with all ghc versions
- aeson-1.3.1.1
- ansi-terminal-0.10.3
- base-compat-0.10.1
- base-compat-batteries-0.10.1
- cassava-megaparsec-2.0.0

View File

@ -13,6 +13,7 @@ packages:
- hledger-web
extra-deps:
- ansi-terminal-0.10.3
- cassava-megaparsec-2.0.0
- config-ini-0.2.3.0
- doctest-0.16.3