From 6e67bedd1682e9c7a13f9cc11e19cc6b2e830505 Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Wed, 7 Apr 2021 15:34:41 +0200 Subject: [PATCH 1/2] Fix colors on Windows (#749) 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. --- CHANGELOG.md | 1 + spago.cabal | 3 ++- src/Spago/RunEnv.hs | 8 ++++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89574ee62..668af24f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/spago.cabal b/spago.cabal index b6b593254..58998d653 100644 --- a/spago.cabal +++ b/spago.cabal @@ -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 diff --git a/src/Spago/RunEnv.hs b/src/Spago/RunEnv.hs index 27f1d09ce..81bacda90 100644 --- a/src/Spago/RunEnv.hs +++ b/src/Spago/RunEnv.hs @@ -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 @@ -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 From a5eb265658b4a492dd8c3be0594a0a08f0824620 Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Wed, 7 Apr 2021 18:39:39 +0200 Subject: [PATCH 2/2] Remove color output from test fixtures, add test for NO_COLOR, fix changelog --- CHANGELOG.md | 8 +++++++- src/Spago/RunEnv.hs | 10 ++++++---- test/fixtures/alternate-config-missing.txt | 6 +++--- test/fixtures/alternative2install-stderr.txt | 4 ++-- test/fixtures/bundle-stderr.txt | 2 +- .../check-direct-import-transitive-dependency.txt | 10 +++++----- test/fixtures/check-unused-dependency.txt | 6 +++--- test/fixtures/circular-dependencies.txt | 6 +++--- test/fixtures/make-module-stderr.txt | 2 +- test/fixtures/missing-dependencies.txt | 6 +++--- test/fixtures/spago-install-existing-dep-stderr.txt | 2 +- .../spago-install-purescript-prefix-stderr.txt | 12 ++++++------ test/fixtures/spago-test-not-found.txt | 2 +- test/fixtures/test-output-stderr.txt | 4 ++-- test/fixtures/verify-set-failure-no-files.txt | 8 ++++---- test/fixtures/verify-set-success.txt | 2 +- 16 files changed, 49 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 668af24f8..5954b86a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +Bugfixes: +- Color output now works correctly or is disabled on Windows (#768, #749) + +Other improvements: +- Color output is now automatically disabled when output is redirected to a file. + Also respects a [`NO_COLOR`](https://no-color.org/) environment variable (#768) + ## [0.20.0] - 2021-04-07 Breaking changes (😱!!!): @@ -14,7 +21,6 @@ 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) diff --git a/src/Spago/RunEnv.hs b/src/Spago/RunEnv.hs index 81bacda90..dfb88cdbe 100644 --- a/src/Spago/RunEnv.hs +++ b/src/Spago/RunEnv.hs @@ -27,10 +27,12 @@ withEnv GlobalOptions{..} app = do -- https://github.com/purescript/spago/issues/579 maybeTerm <- Env.lookupEnv "TERM" let termDumb = maybeTerm == Just "dumb" || maybeTerm == Just "win" - supportsAnsi <- hSupportsANSIWithoutEmulation logHandle - let useColor = case supportsAnsi of - Just True -> globalUseColor && not termDumb - _ -> False + -- Check if the terminal supports color. On Windows 10, terminal colors are enabled + -- here as a side effect. Returns Nothing if output is redirected to a file. + supportsAnsi <- hSupportsANSIWithoutEmulation logHandle <&> (== Just True) + -- Also support NO_COLOR spec https://no-color.org/ + noColor <- Env.lookupEnv "NO_COLOR" <&> isJust + let useColor = globalUseColor && not termDumb && not noColor && supportsAnsi logOptions' <- logOptionsHandle logHandle verbose let logOptions diff --git a/test/fixtures/alternate-config-missing.txt b/test/fixtures/alternate-config-missing.txt index 697896bfe..3301befec 100644 --- a/test/fixtures/alternate-config-missing.txt +++ b/test/fixtures/alternate-config-missing.txt @@ -1,5 +1,5 @@ -[error] Failed to read the config. Error was: -[error] There's no "test.dhall" in your current location. +[error] Failed to read the config. Error was: +[error] There's no "test.dhall" in your current location. If you already have a spago project you might be in the wrong subdirectory, -otherwise you might want to run `spago init` to initialize a new project. +otherwise you might want to run `spago init` to initialize a new project. diff --git a/test/fixtures/alternative2install-stderr.txt b/test/fixtures/alternative2install-stderr.txt index 042445b2c..13dce3a69 100644 --- a/test/fixtures/alternative2install-stderr.txt +++ b/test/fixtures/alternative2install-stderr.txt @@ -1,2 +1,2 @@ -[warn] Failed to add dependencies. You should have a record with the `dependencies` key for this to work. -[warn] Configuration file was not updated. +[warn] Failed to add dependencies. You should have a record with the `dependencies` key for this to work. +[warn] Configuration file was not updated. diff --git a/test/fixtures/bundle-stderr.txt b/test/fixtures/bundle-stderr.txt index e3297715d..323776feb 100644 --- a/test/fixtures/bundle-stderr.txt +++ b/test/fixtures/bundle-stderr.txt @@ -1 +1 @@ -[error] The `bundle` command has been replaced with `bundle-app`, so use that instead. +[error] The `bundle` command has been replaced with `bundle-app`, so use that instead. diff --git a/test/fixtures/check-direct-import-transitive-dependency.txt b/test/fixtures/check-direct-import-transitive-dependency.txt index cb5c8bf47..c2fba0310 100644 --- a/test/fixtures/check-direct-import-transitive-dependency.txt +++ b/test/fixtures/check-direct-import-transitive-dependency.txt @@ -1,8 +1,8 @@ purs compile: No files found using pattern: test/**/*.purs -[info] Build succeeded. -[warn] None of your project files import modules from some projects that are in the direct dependencies of your project. +[info] Build succeeded. +[warn] None of your project files import modules from some projects that are in the direct dependencies of your project. These dependencies are unused. To fix this warning, remove the following packages from the list of dependencies in your config: -- effect -[error] Some of your project files import modules from packages that are not in the direct dependencies of your project. +- effect +[error] Some of your project files import modules from packages that are not in the direct dependencies of your project. To fix this error add the following packages to the list of dependencies in your config: -- prelude +- prelude diff --git a/test/fixtures/check-unused-dependency.txt b/test/fixtures/check-unused-dependency.txt index efc1f0a19..ec3ede38b 100644 --- a/test/fixtures/check-unused-dependency.txt +++ b/test/fixtures/check-unused-dependency.txt @@ -1,5 +1,5 @@ purs compile: No files found using pattern: test/**/*.purs -[info] Build succeeded. -[warn] None of your project files import modules from some projects that are in the direct dependencies of your project. +[info] Build succeeded. +[warn] None of your project files import modules from some projects that are in the direct dependencies of your project. These dependencies are unused. To fix this warning, remove the following packages from the list of dependencies in your config: -- effect +- effect diff --git a/test/fixtures/circular-dependencies.txt b/test/fixtures/circular-dependencies.txt index f046829d6..7ccb16e87 100644 --- a/test/fixtures/circular-dependencies.txt +++ b/test/fixtures/circular-dependencies.txt @@ -1,3 +1,3 @@ -[error] The following packages have circular dependencies: -[error]  - a -[error]  - b +[error] The following packages have circular dependencies: +[error] - a +[error] - b diff --git a/test/fixtures/make-module-stderr.txt b/test/fixtures/make-module-stderr.txt index a3b5c514d..8773c5403 100644 --- a/test/fixtures/make-module-stderr.txt +++ b/test/fixtures/make-module-stderr.txt @@ -1 +1 @@ -[error] The `make-module` command has been replaced with `bundle-module`, so use that instead. +[error] The `make-module` command has been replaced with `bundle-module`, so use that instead. diff --git a/test/fixtures/missing-dependencies.txt b/test/fixtures/missing-dependencies.txt index 8437c06e1..ed420f8c5 100644 --- a/test/fixtures/missing-dependencies.txt +++ b/test/fixtures/missing-dependencies.txt @@ -1,3 +1,3 @@ -[error] The following packages do not exist in your package set: -[error]  - bar -[error]  - foo +[error] The following packages do not exist in your package set: +[error] - bar +[error] - foo diff --git a/test/fixtures/spago-install-existing-dep-stderr.txt b/test/fixtures/spago-install-existing-dep-stderr.txt index 9f90c969a..919d2475d 100644 --- a/test/fixtures/spago-install-existing-dep-stderr.txt +++ b/test/fixtures/spago-install-existing-dep-stderr.txt @@ -1 +1 @@ -[warn] Configuration file was not updated. +[warn] Configuration file was not updated. diff --git a/test/fixtures/spago-install-purescript-prefix-stderr.txt b/test/fixtures/spago-install-purescript-prefix-stderr.txt index 10978e11d..a8c266ded 100644 --- a/test/fixtures/spago-install-purescript-prefix-stderr.txt +++ b/test/fixtures/spago-install-purescript-prefix-stderr.txt @@ -1,6 +1,6 @@ -[info] The package 'purescript-newtype' was resolved to the 'newtype' package -[info] Installing 1 dependencies. -[info] Searching for packages cache metadata.. -[info] Recent packages cache metadata found, using it.. -[info] Installing "newtype" -[info] Installation complete. +[info] The package 'purescript-newtype' was resolved to the 'newtype' package +[info] Installing 1 dependencies. +[info] Searching for packages cache metadata.. +[info] Recent packages cache metadata found, using it.. +[info] Installing "newtype" +[info] Installation complete. diff --git a/test/fixtures/spago-test-not-found.txt b/test/fixtures/spago-test-not-found.txt index 3b90cedb9..b1b29f972 100644 --- a/test/fixtures/spago-test-not-found.txt +++ b/test/fixtures/spago-test-not-found.txt @@ -1 +1 @@ -[error] Module 'Test.Main' not found! Are you including it in your build? +[error] Module 'Test.Main' not found! Are you including it in your build? diff --git a/test/fixtures/test-output-stderr.txt b/test/fixtures/test-output-stderr.txt index fec95f3fe..42dbe58e3 100644 --- a/test/fixtures/test-output-stderr.txt +++ b/test/fixtures/test-output-stderr.txt @@ -1,2 +1,2 @@ -[info] Build succeeded. -[info] Tests succeeded. +[info] Build succeeded. +[info] Tests succeeded. diff --git a/test/fixtures/verify-set-failure-no-files.txt b/test/fixtures/verify-set-failure-no-files.txt index b64b04009..8b8c8aaed 100644 --- a/test/fixtures/verify-set-failure-no-files.txt +++ b/test/fixtures/verify-set-failure-no-files.txt @@ -1,6 +1,6 @@ -[error] Could not find a valid "spago.dhall" or "packages.dhall" -[error] Error was: -[error] There's no "spago.dhall" in your current location. +[error] Could not find a valid "spago.dhall" or "packages.dhall" +[error] Error was: +[error] There's no "spago.dhall" in your current location. If you already have a spago project you might be in the wrong subdirectory, -otherwise you might want to run `spago init` to initialize a new project. +otherwise you might want to run `spago init` to initialize a new project. diff --git a/test/fixtures/verify-set-success.txt b/test/fixtures/verify-set-success.txt index da4c14011..574d10786 100644 --- a/test/fixtures/verify-set-success.txt +++ b/test/fixtures/verify-set-success.txt @@ -1 +1 @@ -[error] Could not find a valid "spago.dhall" or "packages.dhall" +[error] Could not find a valid "spago.dhall" or "packages.dhall"