Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fatih/color
Color package for Go (golang)
https://github.com/fatih/color
ansi color coloring go golang
Last synced: 2 months ago
JSON representation
Color package for Go (golang)
- Host: GitHub
- URL: https://github.com/fatih/color
- Owner: fatih
- License: mit
- Created: 2014-02-17T09:13:35.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-04-08T12:08:59.000Z (2 months ago)
- Last Synced: 2024-04-08T13:29:10.072Z (2 months ago)
- Topics: ansi, color, coloring, go, golang
- Language: Go
- Homepage: https://pkg.go.dev/github.com/fatih/color
- Size: 1.97 MB
- Stars: 6,998
- Watchers: 53
- Forks: 606
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Lists
- awesome-stars - fatih/color - Color package for Go (golang) (Go)
- awesome-go - Color - line output in Go. (Command Line)
- awesome-stars-eryajf - fatih/color - Color package for Go (golang) (Go)
- awesome-stars - color
- go-awesome - Color - 命令行文字颜色 (开源类库 / 命令行)
- awesome-go - color - Versatile package for colored terminal output. (Command Line / Advanced Console UIs)
- awesome-ccamel - fatih/color - Color package for Go (golang) (Go)
- awesome - fatih/color - Color package for Go (golang) (Go)
- awesome-stars - fatih/color - Color package for Go (golang) (Go)
- fucking-awesome-go - :octocat: color - Versatile package for colored terminal output. :star: 920 :fork_and_knife: 73 (Command Line / Advanced Console UIs)
- awesome-stars - color
- awesome-go - color - | - | - | (Command Line / Advanced Console UIs)
- awesome-go - color - Versatile package for colored terminal output. (Command Line / Advanced Console UIs)
- awesome-go-with-framework - color - Versatile package for colored terminal output. (Command Line / Advanced Console UIs)
- awesome-stars - fatih/color - Color package for Go (golang) (Go)
- awesome-go - color - Versatile package for colored terminal output. (Command Line / Advanced Console UIs)
- awesome-stars - fatih/color - Color package for Go (golang) (Go)
- awesome-go - color - Versatile package for colored terminal output. (Command Line / Advanced Console UIs)
- awesome-stars - fatih/color - Color package for Go (golang) (Go)
- awesome-go - color - 彩色终端输出的多功能包装。 (<span id="命令行-command-line">命令行 Command Line</span> / <span id="高级控制台用户界面-advanced-console-uis">高级控制台用户界面 Advanced Console UIs</span>)
- awesome-go. - color - Versatile package for colored terminal output. (Command Line / Advanced Console UIs)
- awesome-go-cn - color - 多功能的用于彩色化命令行输出的库 (命令行 / 高级控制台界面)
- awesome-stars - color - Color package for Go (golang) (Go)
- awesome-star - fatih/color
- awesome-go - color - Versatile package for colored terminal output. (Command Line / Advanced Console UIs)
- awesome-go-cn - color
- awesome-stars - color - Color package for Go (golang) (Go)
- awesome-go - color - 多功能的用於彩色化命令行輸出的庫 (命令行 / 高級控制台界面)
- awesome-stars - fatih/color - Color package for Go (golang) (Go)
- awesome-stars - fatih/color - Color package for Go (golang) (Go)
- awesome-go-handwritten - color - Versatile package for colored terminal output. (Command Line / Advanced Console UIs)
- awesome-go - fatih/color
- my-awesome-list - color - Color package for Go (golang) (Programming Languages / Go)
- go-awesome - Color - color of command line text (Open source library / Command Line)
- awesome-go2 - color - Versatile package for colored terminal output. (Command Line / Advanced Console UIs)
- awesome-go - color - Versatile package for colored terminal output. - :arrow_down:2775 - :star:1081 (Command Line / Advanced Console UIs)
- awesome-go-cn - color
- awesome-go - color - Versatile package for colored terminal output. (Command Line / Advanced Console UIs)
- awesome-x - color
- awesome-stars - fatih/color - `★7098` Color package for Go (golang) (Go)
- awesome-go - color - Color package for Go (golang) - ★ 2745 (Command Line)
- go-awesome - Color - 命令行文字颜色 (开源类库 / 命令行)
README
# color [![](https://github.com/fatih/color/workflows/build/badge.svg)](https://github.com/fatih/color/actions) [![PkgGoDev](https://pkg.go.dev/badge/github.com/fatih/color)](https://pkg.go.dev/github.com/fatih/color)
Color lets you use colorized outputs in terms of [ANSI Escape
Codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors) in Go (Golang). It
has support for Windows too! The API can be used in several ways, pick one that
suits you.![Color](https://user-images.githubusercontent.com/438920/96832689-03b3e000-13f4-11eb-9803-46f4c4de3406.jpg)
## Install
```bash
go get github.com/fatih/color
```## Examples
### Standard colors
```go
// Print with default helper functions
color.Cyan("Prints text in cyan.")// A newline will be appended automatically
color.Blue("Prints %s in blue.", "text")// These are using the default foreground colors
color.Red("We have red")
color.Magenta("And many others ..")```
### Mix and reuse colors
```go
// Create a new color object
c := color.New(color.FgCyan).Add(color.Underline)
c.Println("Prints cyan text with an underline.")// Or just add them to New()
d := color.New(color.FgCyan, color.Bold)
d.Printf("This prints bold cyan %s\n", "too!.")// Mix up foreground and background colors, create new mixes!
red := color.New(color.FgRed)boldRed := red.Add(color.Bold)
boldRed.Println("This will print text in bold red.")whiteBackground := red.Add(color.BgWhite)
whiteBackground.Println("Red text with white background.")
```### Use your own output (io.Writer)
```go
// Use your own io.Writer output
color.New(color.FgBlue).Fprintln(myWriter, "blue color!")blue := color.New(color.FgBlue)
blue.Fprint(writer, "This will print text in blue.")
```### Custom print functions (PrintFunc)
```go
// Create a custom print function for convenience
red := color.New(color.FgRed).PrintfFunc()
red("Warning")
red("Error: %s", err)// Mix up multiple attributes
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
notice("Don't forget this...")
```### Custom fprint functions (FprintFunc)
```go
blue := color.New(color.FgBlue).FprintfFunc()
blue(myWriter, "important notice: %s", stars)// Mix up with multiple attributes
success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
success(myWriter, "Don't forget this...")
```### Insert into noncolor strings (SprintFunc)
```go
// Create SprintXxx functions to mix strings with other non-colorized strings:
yellow := color.New(color.FgYellow).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))info := color.New(color.FgWhite, color.BgGreen).SprintFunc()
fmt.Printf("This %s rocks!\n", info("package"))// Use helper functions
fmt.Println("This", color.RedString("warning"), "should be not neglected.")
fmt.Printf("%v %v\n", color.GreenString("Info:"), "an important message.")// Windows supported too! Just don't forget to change the output to color.Output
fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
```### Plug into existing code
```go
// Use handy standard colors
color.Set(color.FgYellow)fmt.Println("Existing text will now be in yellow")
fmt.Printf("This one %s\n", "too")color.Unset() // Don't forget to unset
// You can mix up parameters
color.Set(color.FgMagenta, color.Bold)
defer color.Unset() // Use it in your functionfmt.Println("All text will now be bold magenta.")
```### Disable/Enable color
There might be a case where you want to explicitly disable/enable color output. the
`go-isatty` package will automatically disable color output for non-tty output streams
(for example if the output were piped directly to `less`).The `color` package also disables color output if the [`NO_COLOR`](https://no-color.org) environment
variable is set to a non-empty string.`Color` has support to disable/enable colors programmatically both globally and
for single color definitions. For example suppose you have a CLI app and a
`-no-color` bool flag. You can easily disable the color output with:```go
var flagNoColor = flag.Bool("no-color", false, "Disable color output")if *flagNoColor {
color.NoColor = true // disables colorized output
}
```It also has support for single color definitions (local). You can
disable/enable color output on the fly:```go
c := color.New(color.FgCyan)
c.Println("Prints cyan text")c.DisableColor()
c.Println("This is printed without any color")c.EnableColor()
c.Println("This prints again cyan...")
```## GitHub Actions
To output color in GitHub Actions (or other CI systems that support ANSI colors), make sure to set `color.NoColor = false` so that it bypasses the check for non-tty output streams.
## Todo
* Save/Return previous values
* Evaluate fmt.Formatter interface## Credits
* [Fatih Arslan](https://github.com/fatih)
* Windows support via @mattn: [colorable](https://github.com/mattn/go-colorable)## License
The MIT License (MIT) - see [`LICENSE.md`](https://github.com/fatih/color/blob/master/LICENSE.md) for more details