cli: --color/--colour option; smart emacs, windows autodetection (#1296)
This commit is contained in:
		
							parent
							
								
									02043d4e65
								
							
						
					
					
						commit
						3452270241
					
				| @ -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 ( | module Hledger.Reports.ReportOptions ( | ||||||
|   ReportOpts(..), |   ReportOpts(..), | ||||||
| @ -54,7 +57,9 @@ import Data.Typeable (Typeable) | |||||||
| import Data.Time.Calendar | import Data.Time.Calendar | ||||||
| import Data.Default | import Data.Default | ||||||
| import Safe | import Safe | ||||||
| import System.Console.ANSI (hSupportsANSI) | 
 | ||||||
|  | import System.Console.ANSI (hSupportsANSIColor) | ||||||
|  | import System.Environment (lookupEnv) | ||||||
| import System.IO (stdout) | import System.IO (stdout) | ||||||
| import Text.Megaparsec.Custom | import Text.Megaparsec.Custom | ||||||
| 
 | 
 | ||||||
| @ -129,6 +134,10 @@ data ReportOpts = ReportOpts { | |||||||
|       --   sign normalisation, converting normally negative subreports to |       --   sign normalisation, converting normally negative subreports to | ||||||
|       --   normally positive for a more conventional display. |       --   normally positive for a more conventional display. | ||||||
|     ,color_          :: Bool |     ,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 |     ,forecast_       :: Maybe DateSpan | ||||||
|     ,transpose_      :: Bool |     ,transpose_      :: Bool | ||||||
|  } deriving (Show, Data, Typeable) |  } deriving (Show, Data, Typeable) | ||||||
| @ -172,7 +181,9 @@ rawOptsToReportOpts :: RawOpts -> IO ReportOpts | |||||||
| rawOptsToReportOpts rawopts = checkReportOpts <$> do | rawOptsToReportOpts rawopts = checkReportOpts <$> do | ||||||
|   let rawopts' = checkRawOpts rawopts |   let rawopts' = checkRawOpts rawopts | ||||||
|   d <- getCurrentDay |   d <- getCurrentDay | ||||||
|   color <- hSupportsANSI stdout |   no_color <- isJust <$> lookupEnv "NO_COLOR" | ||||||
|  |   supports_color <- hSupportsANSIColor stdout | ||||||
|  |   let colorflag = stringopt "color" rawopts | ||||||
|   return defreportopts{ |   return defreportopts{ | ||||||
|      today_       = Just d |      today_       = Just d | ||||||
|     ,period_      = periodFromRawOpts d rawopts' |     ,period_      = periodFromRawOpts d rawopts' | ||||||
| @ -200,7 +211,10 @@ rawOptsToReportOpts rawopts = checkReportOpts <$> do | |||||||
|     ,percent_     = boolopt "percent" rawopts' |     ,percent_     = boolopt "percent" rawopts' | ||||||
|     ,invert_      = boolopt "invert" rawopts' |     ,invert_      = boolopt "invert" rawopts' | ||||||
|     ,pretty_tables_ = boolopt "pretty-tables" 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' |     ,forecast_    = forecastPeriodFromRawOpts d rawopts' | ||||||
|     ,transpose_   = boolopt "transpose" rawopts' |     ,transpose_   = boolopt "transpose" rawopts' | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -48,7 +48,7 @@ dependencies: | |||||||
| - base-compat-batteries >=0.10.1 && <0.12 | - base-compat-batteries >=0.10.1 && <0.12 | ||||||
| - aeson >=1 | - aeson >=1 | ||||||
| - aeson-pretty | - aeson-pretty | ||||||
| - ansi-terminal >=0.6.2.3 | - ansi-terminal >=0.9 | ||||||
| - array | - array | ||||||
| - blaze-markup >=0.5.1 | - blaze-markup >=0.5.1 | ||||||
| - bytestring | - bytestring | ||||||
|  | |||||||
| @ -43,7 +43,7 @@ cpp-options: -DVERSION="1.18.99" | |||||||
| dependencies: | dependencies: | ||||||
| - hledger-lib >=1.18.99 && <1.19 | - hledger-lib >=1.18.99 && <1.19 | ||||||
| - hledger >=1.18.99 && <1.19 | - hledger >=1.18.99 && <1.19 | ||||||
| - ansi-terminal >=0.6.2.3 | - ansi-terminal >=0.9 | ||||||
| - async | - async | ||||||
| - base >=4.9 && <4.15 | - base >=4.9 && <4.15 | ||||||
| - base-compat-batteries >=0.10.1 && <0.12 | - base-compat-batteries >=0.10.1 && <0.12 | ||||||
|  | |||||||
| @ -188,6 +188,17 @@ reportflags = [ | |||||||
|    , "Also, in hledger-ui, make future transactions visible." |    , "Also, in hledger-ui, make future transactions visible." | ||||||
|    , "Note that = (and not a space) is required before PERIODEXP if you wish to supply it." |    , "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. | -- | Flags for selecting flat/tree mode, used in accounts/balance reports. | ||||||
|  | |||||||
| @ -124,10 +124,8 @@ Some example formats: | |||||||
| 
 | 
 | ||||||
| ### Colour support | ### Colour support | ||||||
| 
 | 
 | ||||||
| The balance command shows negative amounts in red, if: | In terminal output, when colour is enabled,  | ||||||
| 
 | the balance command shows negative amounts in red. | ||||||
| - the `TERM` environment variable is not set to `dumb` |  | ||||||
| - the output is not being redirected or piped anywhere |  | ||||||
| 
 | 
 | ||||||
| ### Flat mode | ### 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 | 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).  | 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 | ### Budget report | ||||||
|  | |||||||
| @ -1723,12 +1723,17 @@ directory. These are typically prototypes and not guaranteed to work. | |||||||
| 
 | 
 | ||||||
| # ENVIRONMENT | # ENVIRONMENT | ||||||
| 
 | 
 | ||||||
|  | m4_dnl Standard LEDGER_FILE description: | ||||||
|  | _LEDGER_FILE_ | ||||||
|  | 
 | ||||||
| **COLUMNS** | **COLUMNS** | ||||||
| The screen width used by the register command. | The screen width used by the register command. | ||||||
| Default: the full terminal width. | Default: the full terminal width. | ||||||
| 
 | 
 | ||||||
| m4_dnl Standard LEDGER_FILE description: | **NO_COLOR** | ||||||
| _LEDGER_FILE_ | If this variable exists with any value,  | ||||||
|  | hledger will not use ANSI color codes in terminal output. | ||||||
|  | This overrides the --color/--colour option. | ||||||
| 
 | 
 | ||||||
| # FILES | # FILES | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -110,7 +110,7 @@ ghc-options: | |||||||
| dependencies: | dependencies: | ||||||
| - hledger-lib >=1.18.99 && <1.19 | - hledger-lib >=1.18.99 && <1.19 | ||||||
| - aeson >=1 | - aeson >=1 | ||||||
| - ansi-terminal >=0.6.2.3 | - ansi-terminal >=0.9 | ||||||
| - base >=4.9 && <4.15 | - base >=4.9 && <4.15 | ||||||
| - base-compat-batteries >=0.10.1 && <0.12 | - base-compat-batteries >=0.10.1 && <0.12 | ||||||
| - bytestring | - bytestring | ||||||
|  | |||||||
| @ -16,6 +16,7 @@ packages: | |||||||
| extra-deps: | extra-deps: | ||||||
| # use the latest base-compat with all ghc versions | # use the latest base-compat with all ghc versions | ||||||
| - aeson-1.3.1.1 | - aeson-1.3.1.1 | ||||||
|  | - ansi-terminal-0.10.3 | ||||||
| - base-compat-0.10.1 | - base-compat-0.10.1 | ||||||
| - base-compat-batteries-0.10.1 | - base-compat-batteries-0.10.1 | ||||||
| - cassava-megaparsec-2.0.0 | - cassava-megaparsec-2.0.0 | ||||||
|  | |||||||
| @ -13,6 +13,7 @@ packages: | |||||||
| - hledger-web | - hledger-web | ||||||
| 
 | 
 | ||||||
| extra-deps: | extra-deps: | ||||||
|  | - ansi-terminal-0.10.3 | ||||||
| - cassava-megaparsec-2.0.0 | - cassava-megaparsec-2.0.0 | ||||||
| - config-ini-0.2.3.0 | - config-ini-0.2.3.0 | ||||||
| - doctest-0.16.3 | - doctest-0.16.3 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user