1
mirror of https://github.com/rclone/rclone synced 2024-10-21 08:12:03 +02:00
rclone/fs/terminalcolormode.go
Kevin Verstaen c2dfc3e5b3
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
2022-12-06 12:07:06 +00:00

58 lines
1.4 KiB
Go

package fs
import (
"fmt"
"strings"
)
// TerminalColorMode describes how ANSI codes should be handled
type TerminalColorMode byte
// TerminalColorMode constants
const (
TerminalColorModeAuto TerminalColorMode = iota
TerminalColorModeNever
TerminalColorModeAlways
)
var terminalColorModeToString = []string{
TerminalColorModeAuto: "AUTO",
TerminalColorModeNever: "NEVER",
TerminalColorModeAlways: "ALWAYS",
}
// String converts a TerminalColorMode to a string
func (m TerminalColorMode) String() string {
if m >= TerminalColorMode(len(terminalColorModeToString)) {
return fmt.Sprintf("TerminalColorMode(%d)", m)
}
return terminalColorModeToString[m]
}
// Set a TerminalColorMode
func (m *TerminalColorMode) Set(s string) error {
for n, name := range terminalColorModeToString {
if s != "" && name == strings.ToUpper(s) {
*m = TerminalColorMode(n)
return nil
}
}
return fmt.Errorf("unknown terminal color mode %q", s)
}
// Type of TerminalColorMode
func (m TerminalColorMode) Type() string {
return "string"
}
// UnmarshalJSON converts a string/integer in JSON to a TerminalColorMode
func (m *TerminalColorMode) UnmarshalJSON(in []byte) error {
return UnmarshalJSONFlag(in, m, func(i int64) error {
if i < 0 || i >= int64(len(terminalColorModeToString)) {
return fmt.Errorf("out of range terminal color mode %d", i)
}
*m = (TerminalColorMode)(i)
return nil
})
}