Skip to content

Commit

Permalink
Merge pull request #4260 from mpilgrem/fix4204B
Browse files Browse the repository at this point in the history
Fix (in part) #4204: Honour `NO_COLOR` variable
  • Loading branch information
mihaimaruseac authored Aug 25, 2018
2 parents 2c8cdfb + cf66414 commit 73df857
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion src/unix/Stack/DefaultColorWhen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 14 additions & 1 deletion src/windows/Stack/DefaultColorWhen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 73df857

Please sign in to comment.