Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support toggling colors on/off #11

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
# 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.

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

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
Expand Down Expand Up @@ -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

Expand All @@ -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.")
```
18 changes: 18 additions & 0 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
}
}
Expand Down
17 changes: 17 additions & 0 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}