mirror of
https://github.com/rclone/rclone.git
synced 2025-12-11 22:14:05 +01:00
fs: Add global flag '--color' to control terminal colors
* fs: add TerminalColorMode type * fs: add new config(flags) for TerminalColorMode * lib/terminal: use TerminalColorMode to determine how to handle colors * Add documentation for '--terminal-color-mode' * tree: remove obsolete --color replaced by global --color This changes the default behaviour of tree. It now displays colors by default instead of only displaying them when the flag -C/--color was active. Old behaviour (no color) can be achieved by setting --color to 'never'. Fixes: #6604
This commit is contained in:
@@ -3,12 +3,14 @@
|
||||
package terminal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
colorable "github.com/mattn/go-colorable"
|
||||
"github.com/rclone/rclone/fs"
|
||||
)
|
||||
|
||||
// VT100 codes
|
||||
@@ -73,13 +75,21 @@ var (
|
||||
// Start the terminal - must be called before use
|
||||
func Start() {
|
||||
once.Do(func() {
|
||||
ci := fs.GetConfig(context.Background())
|
||||
|
||||
f := os.Stdout
|
||||
if !IsTerminal(int(f.Fd())) {
|
||||
// If stdout not a tty then remove escape codes
|
||||
Out = colorable.NewNonColorable(f)
|
||||
// If stdout is not a tty, remove escape codes EXCEPT if terminal color mode equals "ALWAYS"
|
||||
if ci.TerminalColorMode == fs.TerminalColorModeAlways {
|
||||
Out = colorable.NewColorable(f)
|
||||
} else {
|
||||
Out = colorable.NewNonColorable(f)
|
||||
}
|
||||
} else if runtime.GOOS == "windows" && os.Getenv("TERM") != "" {
|
||||
// If TERM is set just use stdout
|
||||
Out = f
|
||||
} else if ci.TerminalColorMode == fs.TerminalColorModeNever {
|
||||
Out = colorable.NewNonColorable(f)
|
||||
} else {
|
||||
Out = colorable.NewColorable(f)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user