diff --git a/README.md b/README.md index 5e35f87..594afc6 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ # go-color - ![test](https://github.com/TwiN/go-color/workflows/test/badge.svg?branch=master) An extremely lightweight cross-platform package to colorize text in terminals. @@ -7,20 +6,15 @@ An extremely lightweight cross-platform package to colorize text in terminals. This is not meant for maximal compatibility, nor is it meant to handle a plethora of scenarios. It will simply wrap a message with the necessary characters, if the OS handles it. -There are many cases in which this would not work, such as the output being redirected to something other -than a terminal (such as a file, i.e. `executable >> file.txt`) - ## Usage - ```console go get github.com/TwiN/go-color ``` -### Function - -You can use the `color.Colorize(color, str)`, the `color.Ize(color, string)`, or the `color.With(color, str)` function +### Using Functions +You can use the `color.Colorize(color, str)`, the `color.Ize(color, str)`, or the `color.With(color, str)` function in conjunction with a variable like so: ```go package main @@ -28,16 +22,16 @@ package main import "github.com/TwiN/go-color" func main() { + // These all have the same effect: + println(color.With(color.Red, "This is red")) println(color.Ize(color.Red, "This is red")) - // Or if you prefer the longer version: println(color.Colorize(color.Red, "This is red")) - // Or if you prefer the non-parameterized version: println(color.InRed("This is red")) } ``` Because I felt reading `color.With()`/`color.Ize()` to be more visually pleasant than `color.Colorize()`, -I included `Ize()` and `With()` as an alias for `Colorize()`. +I included `Ize()` and `With()` as aliases for `Colorize()`. I'm not usually a big fan of having two methods doing the same thing, but since this package doesn't have much room for growth (its only purpose is to colorize @@ -86,13 +80,14 @@ color.InGreenOverBlue("This is green text on a blue background") Note that the example above is not exhaustive. -### Variables only +### Using Variables +> ⚠ **WARNING**: By using this approach, you will not be able to disable colors with `color.Toggle(false)`. +> Consider using the function approach instead for maximal cross-library compatibility. Unlike using the aforementioned approach, directly using the color variables will require you to manually prepend `color.Reset` at the end of your string. -You can either directly use the variables like so: - +You can directly use the variables like so: ```go package main @@ -115,3 +110,16 @@ func main() { **NOTE**: If you're going to use this approach, don't forget to prepend your string with `color.Reset`, otherwise everything else in your terminal will be that color until the color is reset or overridden. + + +### Disabling Colors +You can disable colors by using `color.Toggle(false)`, which will cause all functions to not colorize the text. + +As mentioned in [Using Variables](#using-variables), disabling colors will have no effect on the variables, so +make sure you are using functions if you have a need to toggle colors. + + +### Examples +```go +println("My name is " + color.InGreen("John Doe") + " and I have " + color.InRed(32) + " apples.") +``` \ No newline at end of file diff --git a/color.go b/color.go index 4f7511d..882bb22 100644 --- a/color.go +++ b/color.go @@ -41,6 +41,18 @@ var ( WhiteBackground = "\033[107m" ) +var ( + enabled = true +) + +// Toggle enables or disables color output +// +// Note that this does not apply if you are coloring by concatenating strings with color variables directly (e.g. color.Red+"Hello"+color.Reset). +// Enabling/disabling coloring only applies to all functions (Colorize, Ize, With, InRed, OverRed, etc.) +func Toggle(enable bool) { + enabled = enable +} + // Colorize wraps a given message in a given color. // // Example: @@ -49,8 +61,14 @@ var ( func Colorize(color string, s any) string { switch s.(type) { case string: + if !enabled { + return s.(string) + } return color + s.(string) + Reset default: + if !enabled { + return fmt.Sprint(s) + } return color + fmt.Sprint(s) + Reset } } diff --git a/color_test.go b/color_test.go index f07a16f..961ee04 100644 --- a/color_test.go +++ b/color_test.go @@ -184,3 +184,20 @@ func TestIn(t *testing.T) { }) } } + +func TestToggle(t *testing.T) { + Toggle(false) + if output := InRed("test"); output != "test" { + t.Errorf("Expected %s, got %s", "test", output) + } + if output := InRed(123); output != "123" { + t.Errorf("Expected %s, got %s", "test", output) + } + Toggle(true) + if output := InRed("test"); output != "\033[31mtest\033[0m" { + t.Errorf("Expected %s, got %s", "\033[31mtest\033[0m", output) + } + if output := InRed(123); output != "\033[31m123\033[0m" { + t.Errorf("Expected %s, got %s", "\033[31mtest\033[0m", output) + } +}