Respect NO_COLOR

Fixes #10004
This commit is contained in:
Bjørn Erik Pedersen 2022-06-13 15:28:08 +02:00
parent 44f3c07969
commit cbc35c48d2
4 changed files with 13 additions and 8 deletions

View file

@ -284,10 +284,6 @@ func setValueFromFlag(flags *flag.FlagSet, key string, cfg config.Provider, targ
} }
} }
func isTerminal() bool {
return terminal.IsTerminal(os.Stdout)
}
func (c *commandeer) fullBuild(noBuildLock bool) error { func (c *commandeer) fullBuild(noBuildLock bool) error {
var ( var (
g errgroup.Group g errgroup.Group
@ -297,7 +293,7 @@ func (c *commandeer) fullBuild(noBuildLock bool) error {
if !c.h.quiet { if !c.h.quiet {
fmt.Println("Start building sites … ") fmt.Println("Start building sites … ")
fmt.Println(hugo.BuildVersionString()) fmt.Println(hugo.BuildVersionString())
if isTerminal() { if terminal.IsTerminal(os.Stdout) {
defer func() { defer func() {
fmt.Print(showCursor + clearLine) fmt.Print(showCursor + clearLine)
}() }()

View file

@ -290,7 +290,7 @@ func InitGlobalLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, lo
} }
func getLogWriters(outHandle, logHandle io.Writer) (io.Writer, io.Writer) { 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 { if logHandle != ioutil.Discard && isTerm {
// Remove any Ansi coloring from log output // Remove any Ansi coloring from log output
logHandle = ansiCleaner{w: logHandle} logHandle = ansiCleaner{w: logHandle}

View file

@ -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"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with 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" 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 // IsTerminal return true if the file descriptor is terminal and the TERM
// environment variable isn't a dumb one. // environment variable isn't a dumb one.
func IsTerminal(f *os.File) bool { func IsTerminal(f *os.File) bool {

View file

@ -83,7 +83,7 @@ func createPositionStringFormatter(formatStr string) func(p Position) string {
msg := fmt.Sprintf(format, args...) msg := fmt.Sprintf(format, args...)
if terminal.IsTerminal(os.Stdout) { if terminal.PrintANSIColors(os.Stdout) {
return terminal.Notice(msg) return terminal.Notice(msg)
} }