diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-06-13 15:28:08 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-06-13 18:07:37 +0200 |
commit | cbc35c48d252a1b44e4c30e26cfba2ff462a1f96 (patch) | |
tree | 845625eeb385601adce511f0ce972a8cb0347698 /common | |
parent | 44f3c079691f58e31bed31333afed3b7bf3b794f (diff) |
Respect NO_COLOR
Fixes #10004
Diffstat (limited to 'common')
-rw-r--r-- | common/loggers/loggers.go | 2 | ||||
-rw-r--r-- | common/terminal/colors.go | 11 | ||||
-rw-r--r-- | common/text/position.go | 2 |
3 files changed, 12 insertions, 3 deletions
diff --git a/common/loggers/loggers.go b/common/loggers/loggers.go index c61c55a67..308635fe9 100644 --- a/common/loggers/loggers.go +++ b/common/loggers/loggers.go @@ -290,7 +290,7 @@ func InitGlobalLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, lo } func getLogWriters(outHandle, logHandle io.Writer) (io.Writer, io.Writer) { - isTerm := terminal.IsTerminal(os.Stdout) + isTerm := terminal.PrintANSIColors(os.Stdout) if logHandle != ioutil.Discard && isTerm { // Remove any Ansi coloring from log output logHandle = ansiCleaner{w: logHandle} diff --git a/common/terminal/colors.go b/common/terminal/colors.go index 334b82fae..c4a78291e 100644 --- a/common/terminal/colors.go +++ b/common/terminal/colors.go @@ -1,4 +1,4 @@ -// Copyright 2018 The Hugo Authors. All rights reserved. +// Copyright 2022 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -29,6 +29,15 @@ const ( noticeColor = "\033[1;36m%s\033[0m" ) +// PrintANSIColors returns false if NO_COLOR env variable is set, +// else IsTerminal(f). +func PrintANSIColors(f *os.File) bool { + if os.Getenv("NO_COLOR") != "" { + return false + } + return IsTerminal(f) +} + // IsTerminal return true if the file descriptor is terminal and the TERM // environment variable isn't a dumb one. func IsTerminal(f *os.File) bool { diff --git a/common/text/position.go b/common/text/position.go index 34e856c5d..cc1eda354 100644 --- a/common/text/position.go +++ b/common/text/position.go @@ -83,7 +83,7 @@ func createPositionStringFormatter(formatStr string) func(p Position) string { msg := fmt.Sprintf(format, args...) - if terminal.IsTerminal(os.Stdout) { + if terminal.PrintANSIColors(os.Stdout) { return terminal.Notice(msg) } |