From 5d3db1d715a4ab8b00dda60b9b87054a4146c1aa Mon Sep 17 00:00:00 2001 From: Moritz Poldrack Date: Tue, 18 May 2021 09:28:39 +0200 Subject: [PATCH] added support for NO_COLOR --- README.md | 30 +++++++-------------------- colorsbg.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ colorsfg.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ init.go | 9 ++++++++ 4 files changed, 136 insertions(+), 23 deletions(-) create mode 100644 init.go diff --git a/README.md b/README.md index 7588edb..77c8b9e 100644 --- a/README.md +++ b/README.md @@ -10,26 +10,10 @@ Moving around is as simple as Specifying direction `ansi.Up()` ## ANSI Support -| Terminal | Colors | BG Colors | Styles | Movements | Saving Positions | -|------------------|--------------------|--------------------|-------------------------|--------------------|--------------------| -| Kitty | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign:¹² | :heavy_check_mark: | :heavy_check_mark: | -| Alacritty | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign:¹ | :heavy_check_mark: | :heavy_check_mark: | -| xTerm | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark:³ | :heavy_check_mark: | :heavy_check_mark: | -| st | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark:³ | :heavy_check_mark: | :heavy_check_mark: | -| guake | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark:³ | :heavy_check_mark: | :heavy_check_mark: | -| urxvt | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign:²⁴ | :heavy_check_mark: | :heavy_check_mark: | -| terminator | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign:²³ | :heavy_check_mark: | :heavy_check_mark: | -| termite | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign:²³ | :heavy_check_mark: | :heavy_check_mark: | -| Windows PWSH | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign:¹²⁴⁵⁶ | :heavy_check_mark: | :heavy_check_mark: | -| Windows CMD | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign:¹²⁴⁵⁶ | :heavy_check_mark: | :heavy_check_mark: | -| Windows Terminal | :heavy_check_mark: | :heavy_check_mark: | :heavy_minus_sign:¹⁴⁵ | :heavy_check_mark: | :heavy_check_mark: | - -¹) Blinking not supported -²) Conceal not supported -³) BUG: fast blinking -⁴) Strikethrough not supported -⁵) Italic not supported -⁶) Faint not supported -⁷) Required modifying the registry to work - -*this list may be inaccurate as it was created after an exhausting day of work at a time where I wasn't up to much* + +## User Choice + +This library implements the (informal) [`NO_COLOR`](https://no-color.org/) +standard. This disables all formatting-related ANSI-Codes but does not +restrict any other function (like cursor movement or formatting). If this +behavior is unwanted please stay on version 1.3.0 or below. diff --git a/colorsbg.go b/colorsbg.go index c1055bd..c9d58e7 100644 --- a/colorsbg.go +++ b/colorsbg.go @@ -9,11 +9,17 @@ func BlackBG(content ...interface{}) string { // SetBlackBG sets the background color to black func SetBlackBG() string { + if nocolorIsSet { + return "" + } return escape + blackbg + set } // UnsetBlackBG resets the background color from black to default. func UnsetBlackBG() string { + if nocolorIsSet { + return "" + } return escape + resetbg + set } @@ -24,11 +30,17 @@ func RedBG(content ...interface{}) string { // SetRedBG sets the background color to red func SetRedBG() string { + if nocolorIsSet { + return "" + } return escape + redbg + set } // UnsetRedBG resets the background color from red to default. func UnsetRedBG() string { + if nocolorIsSet { + return "" + } return escape + resetbg + set } @@ -39,11 +51,17 @@ func GreenBG(content ...interface{}) string { // SetGreenBG sets the background color to green func SetGreenBG() string { + if nocolorIsSet { + return "" + } return escape + greenbg + set } // UnsetGreenBG resets the background color from green to default. func UnsetGreenBG() string { + if nocolorIsSet { + return "" + } return escape + resetbg + set } @@ -54,11 +72,17 @@ func YellowBG(content ...interface{}) string { // SetYellowBG sets the background color to yellow func SetYellowBG() string { + if nocolorIsSet { + return "" + } return escape + yellowbg + set } // UnsetYellowBG resets the background color from yellow to default. func UnsetYellowBG() string { + if nocolorIsSet { + return "" + } return escape + resetbg + set } @@ -69,11 +93,17 @@ func BlueBG(content ...interface{}) string { // SetBlueBG sets the background color to blue func SetBlueBG() string { + if nocolorIsSet { + return "" + } return escape + bluebg + set } // UnsetBlueBG resets the background color from blue to default. func UnsetBlueBG() string { + if nocolorIsSet { + return "" + } return escape + resetbg + set } @@ -84,11 +114,17 @@ func MagentaBG(content ...interface{}) string { // SetMagentaBG sets the background color to magenta func SetMagentaBG() string { + if nocolorIsSet { + return "" + } return escape + magentabg + set } // UnsetMagentaBG resets the background color from magenta to default. func UnsetMagentaBG() string { + if nocolorIsSet { + return "" + } return escape + resetbg + set } @@ -99,11 +135,17 @@ func CyanBG(content ...interface{}) string { // SetCyanBG sets the background color to cyan func SetCyanBG() string { + if nocolorIsSet { + return "" + } return escape + cyanbg + set } // UnsetCyanBG resets the background color from cyan to default. func UnsetCyanBG() string { + if nocolorIsSet { + return "" + } return escape + resetbg + set } @@ -114,11 +156,17 @@ func WhiteBG(content ...interface{}) string { // SetWhiteBG sets the background color to white func SetWhiteBG() string { + if nocolorIsSet { + return "" + } return escape + whitebg + set } // UnsetWhiteBG resets the background color from white to default. func UnsetWhiteBG() string { + if nocolorIsSet { + return "" + } return escape + resetbg + set } @@ -130,11 +178,17 @@ func Color256BG(color int, content ...interface{}) string { // SetColor256BG writes the following text on the specified background color func SetColor256BG(color int) string { + if nocolorIsSet { + return "" + } return escape + fmt.Sprintf(bg256, color) + set } // UnsetColor256BG resets the background color func UnsetColor256BG() string { + if nocolorIsSet { + return "" + } return escape + resetbg + set } @@ -146,10 +200,16 @@ func ColorTrueBG(r, g, b int, content ...interface{}) string { // SetColorTrueBG sets a RGB-Color for the background func SetColorTrueBG(r, g, b int) string { + if nocolorIsSet { + return "" + } return escape + fmt.Sprintf(bgtrue, r, g, b) + set } // UnsetColorTrueBG removes the RGB-background func UnsetColorTrueBG() string { + if nocolorIsSet { + return "" + } return escape + resetbg + set } diff --git a/colorsfg.go b/colorsfg.go index 10e0d9b..4bc981c 100644 --- a/colorsfg.go +++ b/colorsfg.go @@ -9,11 +9,17 @@ func Black(content ...interface{}) string { // SetBlack sets the foreground color to black func SetBlack() string { + if nocolorIsSet { + return "" + } return escape + blackfg + set } // UnsetBlack resets the foreground color from black to default. func UnsetBlack() string { + if nocolorIsSet { + return "" + } return escape + resetfg + set } @@ -24,11 +30,17 @@ func Red(content ...interface{}) string { // SetRed sets the foreground color to red func SetRed() string { + if nocolorIsSet { + return "" + } return escape + redfg + set } // UnsetRed resets the foreground color from red to default. func UnsetRed() string { + if nocolorIsSet { + return "" + } return escape + resetfg + set } @@ -39,11 +51,17 @@ func Green(content ...interface{}) string { // SetGreen sets the foreground color to green func SetGreen() string { + if nocolorIsSet { + return "" + } return escape + greenfg + set } // UnsetGreen resets the foreground color from green to default. func UnsetGreen() string { + if nocolorIsSet { + return "" + } return escape + resetfg + set } @@ -54,11 +72,17 @@ func Yellow(content ...interface{}) string { // SetYellow sets the foreground color to yellow func SetYellow() string { + if nocolorIsSet { + return "" + } return escape + yellowfg + set } // UnsetYellow resets the foreground color from yellow to default. func UnsetYellow() string { + if nocolorIsSet { + return "" + } return escape + resetfg + set } @@ -69,11 +93,17 @@ func Blue(content ...interface{}) string { // SetBlue sets the foreground color to blue func SetBlue() string { + if nocolorIsSet { + return "" + } return escape + bluefg + set } // UnsetBlue resets the foreground color from blue to default. func UnsetBlue() string { + if nocolorIsSet { + return "" + } return escape + resetfg + set } @@ -84,11 +114,17 @@ func Magenta(content ...interface{}) string { // SetMagenta sets the foreground color to magenta func SetMagenta() string { + if nocolorIsSet { + return "" + } return escape + magentafg + set } // UnsetMagenta resets the foreground color from magenta to default. func UnsetMagenta() string { + if nocolorIsSet { + return "" + } return escape + resetfg + set } @@ -99,11 +135,17 @@ func Cyan(content ...interface{}) string { // SetCyan sets the foreground color to cyan func SetCyan() string { + if nocolorIsSet { + return "" + } return escape + cyanfg + set } // UnsetCyan resets the foreground color from cyan to default. func UnsetCyan() string { + if nocolorIsSet { + return "" + } return escape + resetfg + set } @@ -114,11 +156,17 @@ func White(content ...interface{}) string { // SetWhite sets the foreground color to white func SetWhite() string { + if nocolorIsSet { + return "" + } return escape + whitefg + set } // UnsetWhite resets the foreground color from white to default. func UnsetWhite() string { + if nocolorIsSet { + return "" + } return escape + resetfg + set } @@ -129,11 +177,17 @@ func Color256(color int, content ...interface{}) string { // SetColor256 writes the following text on the specified term256 color func SetColor256(color int) string { + if nocolorIsSet { + return "" + } return escape + fmt.Sprintf(fg256, color) + set } // UnsetColor256 resets the color func UnsetColor256() string { + if nocolorIsSet { + return "" + } return escape + resetfg + set } @@ -144,10 +198,16 @@ func ColorTrue(r, g, b int, content ...interface{}) string { // SetColorTrue sets a RGB-Color for the font func SetColorTrue(r, g, b int) string { + if nocolorIsSet { + return "" + } return escape + fmt.Sprintf(fgtrue, r, g, b) + set } // UnsetColorTrue removes the RGB-color func UnsetColorTrue() string { + if nocolorIsSet { + return "" + } return escape + resetfg + set } diff --git a/init.go b/init.go new file mode 100644 index 0000000..38704cd --- /dev/null +++ b/init.go @@ -0,0 +1,9 @@ +package ansi + +import "os" + +var nocolorIsSet bool + +func init() { + _, nocolorIsSet = os.LookupEnv("NO_COLOR") +} -- 2.45.2