mirror of
https://github.com/rclone/rclone.git
synced 2024-12-23 15:38:57 +01:00
c2dfc3e5b3
* 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
58 lines
1.4 KiB
Go
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
|
|
})
|
|
}
|