Skip to content

Commit 886fe59

Browse files
authored
Merge pull request #5360 from mpilgrem/logColors
Fix #5356 Allow all colours to be customised
2 parents 065af79 + fe25227 commit 886fe59

File tree

7 files changed

+76
-40
lines changed

7 files changed

+76
-40
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Bug fixes:
4040
Other enhancements:
4141

4242
* Add the `stack-developer-mode` flag
43+
* Customisable output styles (see `stack --help` and the `--stack-colors`
44+
option, and `stack ls stack-colors --help`) now include `info`, `debug`,
45+
`other-level`, `secondary` and `highlight`, used with verbose output.
4346

4447
Bug fixes:
4548

doc/yaml_configuration.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ Specify a variant binary distribution of GHC to use. Known values:
614614
* `integersimple`: Use a GHC bindist that uses
615615
[integer-simple instead of GMP](https://ghc.haskell.org/trac/ghc/wiki/ReplacingGMPNotes)
616616
* any other value: Use a custom GHC bindist. You should specify
617-
[setup-info](#setup-info) or [setup-info-locations](#setup-info-locations)
617+
[setup-info](#setup-info) or [setup-info-locations](#setup-info-locations)
618618
so `stack setup` knows where to download it,
619619
or pass the `stack setup --ghc-bindist` argument on the command-line
620620

@@ -1143,11 +1143,12 @@ For example, users of the popular
11431143
terminal theme might wish to set the styles as follows:
11441144

11451145
```yaml
1146-
stack-colors: error=31:good=32:shell=35:dir=34:recommendation=32:target=95:module=35:package-component=95
1146+
stack-colors: error=31:good=32:shell=35:dir=34:recommendation=32:target=95:module=35:package-component=95:secondary=92:highlight=32
11471147
```
11481148
The styles can also be set at the command line using the equivalent `--stack-colors=<STYLES>`
11491149
global option. Styles set at the command line take precedence over those set in
1150-
a yaml configuration file.
1150+
a yaml configuration file. (In respect of styles used in verbose output, some of
1151+
that output occurs before the configuration file is processed.)
11511152

11521153
(The British English spelling (colour) is also accepted. In yaml configuration
11531154
files, the American spelling is the alternative that has priority.)

package.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ dependencies:
106106
- project-template
107107
- regex-applicative-text
108108
- retry
109-
- rio
110-
- rio-prettyprint
109+
- rio >= 0.1.18.0
110+
- rio-prettyprint >= 0.1.1.0
111111
- semigroups
112112
- split
113113
- stm

src/Stack/Config.hs

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ module Stack.Config
3030
,defaultConfigYaml
3131
,getProjectConfig
3232
,withBuildConfig
33+
,withNewLogFunc
3334
) where
3435

3536
import Control.Monad.Extra (firstJustM)
3637
import Stack.Prelude
3738
import Pantry.Internal.AesonExtended
39+
import Data.Array.IArray ((!), (//))
3840
import qualified Data.ByteString as S
3941
import Data.ByteString.Builder (byteString)
4042
import Data.Coerce (coerce)
@@ -74,13 +76,16 @@ import Stack.Types.Nix
7476
import Stack.Types.Resolver
7577
import Stack.Types.SourceMap
7678
import Stack.Types.Version
77-
import System.Console.ANSI (hSupportsANSIWithoutEmulation)
79+
import System.Console.ANSI (hSupportsANSIWithoutEmulation, setSGRCode)
7880
import System.Environment
7981
import System.Info.ShortPathName (getShortPathName)
8082
import System.PosixCompat.Files (fileOwner, getFileStatus)
8183
import System.PosixCompat.User (getEffectiveUserID)
8284
import RIO.List (unzip)
83-
import RIO.PrettyPrint (stylesUpdateL, useColorL)
85+
import RIO.PrettyPrint (Style (Highlight, Secondary),
86+
logLevelToStyle, stylesUpdateL, useColorL)
87+
import RIO.PrettyPrint.StylesUpdate (StylesUpdate (..))
88+
import RIO.PrettyPrint.DefaultStyles (defaultStyles)
8489
import RIO.Process
8590
import RIO.Time (toGregorian)
8691

@@ -354,10 +359,12 @@ configFromConfigMonoid
354359
ColorNever -> False
355360
ColorAlways -> True
356361
ColorAuto -> useAnsi
357-
configRunner = configRunner'
358-
& processContextL .~ origEnv
359-
& stylesUpdateL .~ stylesUpdate'
360-
& useColorL .~ fromMaybe useColor' mUseColor
362+
useColor'' = fromMaybe useColor' mUseColor
363+
configRunner'' = configRunner'
364+
& processContextL .~ origEnv
365+
& stylesUpdateL .~ stylesUpdate'
366+
& useColorL .~ useColor''
367+
go = runnerGlobalOpts configRunner'
361368

362369
hsc <-
363370
case getFirst configMonoidPackageIndices of
@@ -394,17 +401,47 @@ configFromConfigMonoid
394401

395402
let configStackDeveloperMode = fromFirst stackDeveloperModeDefault configMonoidStackDeveloperMode
396403

397-
withPantryConfig
398-
pantryRoot
399-
hsc
400-
(maybe HpackBundled HpackCommand $ getFirst configMonoidOverrideHpack)
401-
clConnectionCount
402-
(fromFirst defaultCasaRepoPrefix configMonoidCasaRepoPrefix)
403-
defaultCasaMaxPerRequest
404-
snapLoc
405-
(\configPantryConfig -> initUserStorage
406-
(configStackRoot </> relFileStorage)
407-
(\configUserStorage -> inner Config {..}))
404+
withNewLogFunc go useColor'' stylesUpdate' $ \logFunc -> do
405+
let configRunner = configRunner'' & logFuncL .~ logFunc
406+
withPantryConfig
407+
pantryRoot
408+
hsc
409+
(maybe HpackBundled HpackCommand $ getFirst configMonoidOverrideHpack)
410+
clConnectionCount
411+
(fromFirst defaultCasaRepoPrefix configMonoidCasaRepoPrefix)
412+
defaultCasaMaxPerRequest
413+
snapLoc
414+
(\configPantryConfig -> initUserStorage
415+
(configStackRoot </> relFileStorage)
416+
(\configUserStorage -> inner Config {..}))
417+
418+
-- | Runs the provided action with a new 'LogFunc', given a 'StylesUpdate'.
419+
withNewLogFunc :: MonadUnliftIO m
420+
=> GlobalOpts
421+
-> Bool -- ^ Use color
422+
-> StylesUpdate
423+
-> (LogFunc -> m a)
424+
-> m a
425+
withNewLogFunc go useColor (StylesUpdate update) inner = do
426+
logOptions0 <- logOptionsHandle stderr False
427+
let logOptions
428+
= setLogUseColor useColor
429+
$ setLogLevelColors logLevelColors
430+
$ setLogSecondaryColor secondaryColor
431+
$ setLogAccentColors (const highlightColor)
432+
$ setLogUseTime (globalTimeInLog go)
433+
$ setLogMinLevel (globalLogLevel go)
434+
$ setLogVerboseFormat (globalLogLevel go <= LevelDebug)
435+
$ setLogTerminal (globalTerminal go)
436+
logOptions0
437+
withLogFunc logOptions inner
438+
where
439+
styles = defaultStyles // update
440+
logLevelColors :: LogLevel -> Utf8Builder
441+
logLevelColors level =
442+
fromString $ setSGRCode $ snd $ styles ! logLevelToStyle level
443+
secondaryColor = fromString $ setSGRCode $ snd $ styles ! Secondary
444+
highlightColor = fromString $ setSGRCode $ snd $ styles ! Highlight
408445

409446
-- | Get the default location of the local programs directory.
410447
getDefaultLocalProgramsBase :: MonadThrow m

src/Stack/Runners.hs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,8 @@ withRunnerGlobal go inner = do
148148
<$> getTerminalWidth)
149149
pure (globalTermWidth go)
150150
menv <- mkDefaultProcessContext
151-
logOptions0 <- logOptionsHandle stderr False
152-
let logOptions
153-
= setLogUseColor useColor
154-
$ setLogUseTime (globalTimeInLog go)
155-
$ setLogMinLevel (globalLogLevel go)
156-
$ setLogVerboseFormat (globalLogLevel go <= LevelDebug)
157-
$ setLogTerminal (globalTerminal go)
158-
logOptions0
159-
withLogFunc logOptions $ \logFunc -> runRIO Runner
151+
let update = globalStylesUpdate go
152+
withNewLogFunc go useColor update $ \logFunc -> runRIO Runner
160153
{ runnerGlobalOpts = go
161154
, runnerUseColor = useColor
162155
, runnerLogFunc = logFunc

stack.cabal

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: 2.0
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 2a288b315557ca9df67fb60c003674a0dd6618aa5f8bc2b04527fb8feae4b16d
7+
-- hash: 40180d8b137f1935be8e5493b14ff579fc5174c0bd488f713b1020be585169e1
88

99
name: stack
1010
version: 2.4.0
@@ -284,8 +284,8 @@ library
284284
, project-template
285285
, regex-applicative-text
286286
, retry
287-
, rio
288-
, rio-prettyprint
287+
, rio >=0.1.18.0
288+
, rio-prettyprint >=0.1.1.0
289289
, semigroups
290290
, split
291291
, stm
@@ -408,8 +408,8 @@ executable stack
408408
, project-template
409409
, regex-applicative-text
410410
, retry
411-
, rio
412-
, rio-prettyprint
411+
, rio >=0.1.18.0
412+
, rio-prettyprint >=0.1.1.0
413413
, semigroups
414414
, split
415415
, stack
@@ -531,8 +531,8 @@ executable stack-integration-test
531531
, project-template
532532
, regex-applicative-text
533533
, retry
534-
, rio
535-
, rio-prettyprint
534+
, rio >=0.1.18.0
535+
, rio-prettyprint >=0.1.1.0
536536
, semigroups
537537
, split
538538
, stm
@@ -660,8 +660,8 @@ test-suite stack-test
660660
, raw-strings-qq
661661
, regex-applicative-text
662662
, retry
663-
, rio
664-
, rio-prettyprint
663+
, rio >=0.1.18.0
664+
, rio-prettyprint >=0.1.1.0
665665
, semigroups
666666
, smallcheck
667667
, split

stack.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ extra-deps:
2929
- http-download-0.2.0.0@rev:0
3030
- filelock-0.1.1.5@rev:0
3131
- pantry-0.5.1.1@rev:0
32+
- rio-0.1.18.0@rev:0
33+
- rio-prettyprint-0.1.1.0@rev:0
3234
- casa-client-0.0.1@rev:0
3335
- casa-types-0.0.1@rev:0
3436

0 commit comments

Comments
 (0)