fix: colour: NO_COLOR flag no longer overrides an explicit --color option. (#1710)

http://no-color.org/ specifies that command line arguments should
override a NO_COLOR flag.
This commit is contained in:
Stephen Morgan 2021-09-21 17:03:55 +10:00 committed by Simon Michael
parent 399d28dc05
commit afdeb404b0
2 changed files with 12 additions and 13 deletions

View File

@ -170,11 +170,11 @@ debugLevel = case dropWhile (/="--debug") args of
-- This is done using unsafePerformIO so it can be used anywhere, eg in -- This is done using unsafePerformIO so it can be used anywhere, eg in
-- low-level debug utilities, which should be ok since we are just reading. -- low-level debug utilities, which should be ok since we are just reading.
-- The logic is: use color if -- The logic is: use color if
-- a NO_COLOR environment variable is not defined
-- and the program was not started with --color=no|never
-- and (
-- the program was started with --color=yes|always -- the program was started with --color=yes|always
-- or stdout supports ANSI color and -o/--output-file was not used or is "-" -- or (
-- the program was not started with --color=no|never
-- and a NO_COLOR environment variable is not defined
-- and stdout supports ANSI color and -o/--output-file was not used or is "-"
-- ). -- ).
-- Caveats: -- Caveats:
-- When running code in GHCI, this module must be reloaded to see a change. -- When running code in GHCI, this module must be reloaded to see a change.
@ -200,9 +200,8 @@ useColorOnHandle h = unsafePerformIO $ do
no_color <- isJust <$> lookupEnv "NO_COLOR" no_color <- isJust <$> lookupEnv "NO_COLOR"
supports_color <- hSupportsANSIColor h supports_color <- hSupportsANSIColor h
let coloroption = colorOption let coloroption = colorOption
return $ not no_color return $ coloroption `elem` ["always","yes"]
&& coloroption `notElem` ["never","no"] || (coloroption `notElem` ["never","no"] && not no_color && supports_color)
&& (coloroption `elem` ["always","yes"] || supports_color)
-- Keep synced with color/colour flag definition in hledger:CliOptions. -- Keep synced with color/colour flag definition in hledger:CliOptions.
-- Avoid using dbg*, pshow etc. in this function (infinite loop). -- Avoid using dbg*, pshow etc. in this function (infinite loop).

View File

@ -351,7 +351,7 @@ Default: the full terminal width.
**NO_COLOR** **NO_COLOR**
If this variable exists with any value, If this variable exists with any value,
hledger will not use ANSI color codes in terminal output. hledger will not use ANSI color codes in terminal output.
This overrides the --color/--colour option. This is overriden by the --color/--colour option.
# DATA FILES # DATA FILES
@ -1387,9 +1387,9 @@ If you need to capture it, use shell redirects, eg: `hledger bal --debug=3 >file
hledger commands can produce colour output when the terminal supports it. hledger commands can produce colour output when the terminal supports it.
This is controlled by the `--color/--colour` option: This is controlled by the `--color/--colour` option:
- if the `NO_COLOR` environment variable is set, colour will not be used; - if the `--color/--colour` option is given a value of `yes` or `always`
- 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; (or `no` or `never`), colour will (or will not) be used;
- otherwise, if the `NO_COLOR` environment variable is set, colour will not be used;
- otherwise, colour will be used if the output (terminal or file) supports it. - 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. hledger commands can also use unicode box-drawing characters to produce prettier tables and output.