Skip to content

Commit

Permalink
Fix colors on Windows (#749)
Browse files Browse the repository at this point in the history
Calling the `hSupportsANSIWithoutEmulation` function on Windows 10+
enables terminal colors where supported.
https://hackage.haskell.org/package/ansi-terminal-0.11/docs/System-Console-ANSI.html#v:hSupportsANSIWithoutEmulation

Also has the added benefit that (on Windows), the ANSI codes aren't
present when output is redirected to a text file.
  • Loading branch information
stkb committed Apr 7, 2021
1 parent 7dfd223 commit 6e67bed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Breaking changes (😱!!!):

Bugfixes:
- Properly call `psa` to avoid warnings (#730)
- Color output now works on Windows (#749)

Other improvements:
- `spago build` now detects and warns about unused dependencies (#730, #598)
Expand Down
3 changes: 2 additions & 1 deletion spago.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ executable spago
default-extensions: ApplicativeDo BangPatterns BinaryLiterals ConstraintKinds DataKinds DefaultSignatures DeriveDataTypeable DeriveFoldable DeriveFunctor DeriveGeneric DeriveTraversable DerivingStrategies DoAndIfThenElse DuplicateRecordFields EmptyDataDecls ExistentialQuantification FlexibleContexts FlexibleInstances FunctionalDependencies GADTs GeneralizedNewtypeDeriving InstanceSigs KindSignatures LambdaCase LiberalTypeSynonyms MonadFailDesugaring MultiParamTypeClasses MultiWayIf NamedFieldPuns NoImplicitPrelude NoMonomorphismRestriction OverloadedStrings PartialTypeSignatures PatternGuards PolyKinds QuantifiedConstraints RankNTypes RecordWildCards ScopedTypeVariables StandaloneDeriving TupleSections TypeApplications TypeFamilies TypeSynonymInstances UndecidableInstances ViewPatterns
ghc-options: -Wall -Wcompat -Wincomplete-record-updates -Wredundant-constraints -fprint-potential-instances -optP-Wno-nonportable-include-path
build-depends:
base >=4.7 && <5
ansi-terminal
, base >=4.7 && <5
, spago
, text <1.3
, turtle
Expand Down
8 changes: 6 additions & 2 deletions src/Spago/RunEnv.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Spago.RunEnv where
import Spago.Prelude
import Spago.Env

import System.Console.ANSI (hSupportsANSIWithoutEmulation)
import qualified System.Environment as Env
import qualified Distribution.System as OS
import qualified RIO
Expand All @@ -20,14 +21,17 @@ import qualified Spago.Purs as Purs
-- and runs the app
withEnv :: GlobalOptions -> RIO Env a -> IO a
withEnv GlobalOptions{..} app = do
let logHandle = stderr
let verbose = not globalQuiet && (globalVerbose || globalVeryVerbose)

-- https://github.com/purescript/spago/issues/579
maybeTerm <- Env.lookupEnv "TERM"
let termDumb = maybeTerm == Just "dumb" || maybeTerm == Just "win"
let useColor = globalUseColor && not termDumb
supportsAnsi <- hSupportsANSIWithoutEmulation logHandle
let useColor = case supportsAnsi of
Just True -> globalUseColor && not termDumb
_ -> False

let logHandle = stderr
logOptions' <- logOptionsHandle logHandle verbose
let logOptions
= setLogUseTime globalVeryVerbose
Expand Down

0 comments on commit 6e67bed

Please sign in to comment.