diff options
| author | 2023-04-28 01:36:44 +0800 | |
|---|---|---|
| committer | 2023-04-28 01:36:44 +0800 | |
| commit | dd84b9d64fb98746a230cd24233ff50a562c39c9 (patch) | |
| tree | b583261ef00b3afe72ec4d6dacb31e57779a6faf /cli/internal/ui/colors.go | |
| parent | 0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff) | |
| download | HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip | |
Diffstat (limited to 'cli/internal/ui/colors.go')
| -rw-r--r-- | cli/internal/ui/colors.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/cli/internal/ui/colors.go b/cli/internal/ui/colors.go new file mode 100644 index 0000000..4b2eccd --- /dev/null +++ b/cli/internal/ui/colors.go @@ -0,0 +1,54 @@ +package ui + +import ( + "os" + + "github.com/fatih/color" +) + +type ColorMode int + +const ( + ColorModeUndefined ColorMode = iota + 1 + ColorModeSuppressed + ColorModeForced +) + +func GetColorModeFromEnv() ColorMode { + // The FORCED_COLOR behavior and accepted values are taken from the supports-color NodeJS Package: + // The accepted values as documented are "0" to disable, and "1", "2", or "3" to force-enable color + // at the specified support level (1 = 16 colors, 2 = 256 colors, 3 = 16M colors). + // We don't currently use the level for anything specific, and just treat things as on and off. + // + // Note: while "false" and "true" aren't documented, the library coerces these values to 0 and 1 + // respectively, so that behavior is reproduced here as well. + // https://www.npmjs.com/package/supports-color + + switch forceColor := os.Getenv("FORCE_COLOR"); { + case forceColor == "false" || forceColor == "0": + return ColorModeSuppressed + case forceColor == "true" || forceColor == "1" || forceColor == "2" || forceColor == "3": + return ColorModeForced + default: + return ColorModeUndefined + } +} + +func applyColorMode(colorMode ColorMode) ColorMode { + switch colorMode { + case ColorModeForced: + color.NoColor = false + case ColorModeSuppressed: + color.NoColor = true + case ColorModeUndefined: + default: + // color.NoColor already gets its default value based on + // isTTY and/or the presence of the NO_COLOR env variable. + } + + if color.NoColor { + return ColorModeSuppressed + } else { + return ColorModeForced + } +} |
