From cf6641426b88e22129c27abc69a0a469a1d2ac25 Mon Sep 17 00:00:00 2001 From: Mike Pilgrem Date: Sat, 25 Aug 2018 16:33:38 +0100 Subject: [PATCH] Fix (in part) #4204: Honour `NO_COLOR` variable Adopt the standard proposed at `http://no-color.org/`, that color should not be added by default if the `NO_COLOR` environment variable is present, by adapting `defaultColorWhen` in module `Stack.DefaultColorWhen`. --- ChangeLog.md | 2 ++ src/unix/Stack/DefaultColorWhen.hs | 9 ++++++++- src/windows/Stack/DefaultColorWhen.hs | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 44ce55427a..a55b78d35e 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -15,6 +15,8 @@ Other enhancements: plan construction errors much faster, and avoid some unnecessary work when only building a subset of packages. This is especially useful for the curator use case. +* Adopt the standard proposed at http://no-color.org/, that color should not be + added by default if the `NO_COLOR` environment variable is present. * New command `stack ls stack-colors` lists the styles and the associated 'ANSI' control character sequences that stack uses to color some of its output. See `stack ls stack-colors --help` for more information. diff --git a/src/unix/Stack/DefaultColorWhen.hs b/src/unix/Stack/DefaultColorWhen.hs index 59ae009396..425859f704 100644 --- a/src/unix/Stack/DefaultColorWhen.hs +++ b/src/unix/Stack/DefaultColorWhen.hs @@ -7,5 +7,12 @@ module Stack.DefaultColorWhen import Stack.Types.Runner (ColorWhen (ColorAuto)) +-- |The default adopts the standard proposed at http://no-color.org/, that color +-- should not be added by default if the @NO_COLOR@ environment variable is +-- present. defaultColorWhen :: IO ColorWhen -defaultColorWhen = return ColorAuto +defaultColorWhen = do + mIsNoColor <- lookupEnv "NO_COLOR" + return $ case mIsNoColor of + Just _ -> ColorNever + _ -> ColorAuto diff --git a/src/windows/Stack/DefaultColorWhen.hs b/src/windows/Stack/DefaultColorWhen.hs index d539d37a17..e46eead6e1 100644 --- a/src/windows/Stack/DefaultColorWhen.hs +++ b/src/windows/Stack/DefaultColorWhen.hs @@ -16,11 +16,24 @@ import Data.Bits ((.|.), (.&.)) import Foreign.Marshal (alloca) import Foreign.Ptr (Ptr) import Foreign.Storable (peek) +import System.Environment (lookupEnv) import System.Win32.Types (BOOL, DWORD, HANDLE, iNVALID_HANDLE_VALUE, nullHANDLE, withHandleToHANDLE) +-- |The default adopts the standard proposed at http://no-color.org/, that color +-- should not be added by default if the @NO_COLOR@ environment variable is +-- present. defaultColorWhen :: IO ColorWhen -defaultColorWhen = withHandleToHANDLE stdout aNSISupport +defaultColorWhen = do + -- 'aNSISupport' has the side effect of enabling ANSI for ANSI-capable native + -- (ConHost) terminals, if not already ANSI-enabled. Consequently, it is + -- actioned even if @NO_COLOR@ might exist, as @NO_COLOR@ might be overridden + -- in a yaml configuration file or at the command line. + defColorWhen <- withHandleToHANDLE stdout aNSISupport + mIsNoColor <- lookupEnv "NO_COLOR" + return $ case mIsNoColor of + Just _ -> ColorNever + _ -> defColorWhen -- The following is based on extracts from the modules -- System.Console.ANSI.Windows.Foreign and System.Console.ANSI.Windows.Detect