All Projects → fatih → Color

fatih / Color

Licence: mit
Color package for Go (golang)

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Color

strip-ansi-stream
Strip ANSI escape codes
Stars: ✭ 32 (-99.35%)
Mutual labels:  color, ansi
Hues
Colored terminal text made easy for Python and happiness.
Stars: ✭ 345 (-92.96%)
Mutual labels:  ansi, color
go-color
A lightweight, simple and cross-platform package to colorize text in terminals
Stars: ✭ 65 (-98.67%)
Mutual labels:  color, ansi
Gvcci
color extraction to turn images into 16 color palettes
Stars: ✭ 86 (-98.25%)
Mutual labels:  ansi, color
ansi-to-svg
😹 convert ANSI Escaped CLI strings to SVGs
Stars: ✭ 18 (-99.63%)
Mutual labels:  color, ansi
Monolog Colored Line Formatter
Colored/ANSI Line Formatter for Monolog
Stars: ✭ 108 (-97.8%)
Mutual labels:  ansi, color
yachalk
🖍️ Terminal string styling done right
Stars: ✭ 131 (-97.33%)
Mutual labels:  color, ansi
leeks.js
Simple ANSI styling for your terminal
Stars: ✭ 12 (-99.76%)
Mutual labels:  color, ansi
concolor
Colouring template strings using tags with annotations 🎨
Stars: ✭ 35 (-99.29%)
Mutual labels:  color, ansi
paper-terminal
Print Markdown to a paper in your terminal
Stars: ✭ 33 (-99.33%)
Mutual labels:  color, ansi
Rang
A Minimal, Header only Modern c++ library for terminal goodies 💄✨
Stars: ✭ 1,080 (-77.98%)
Mutual labels:  ansi, color
Chalk
🖍 Terminal string styling done right
Stars: ✭ 17,566 (+258.2%)
Mutual labels:  ansi, color
Pixterm
Draw images in your ANSI terminal with true color
Stars: ✭ 782 (-84.05%)
Mutual labels:  ansi, color
Colorizeswift
Terminal string styling for Swift.
Stars: ✭ 253 (-94.84%)
Mutual labels:  ansi, color
Imgcat
It's like cat, but for images.
Stars: ✭ 577 (-88.23%)
Mutual labels:  ansi, color
log-utils
Basic logging utils: colors, symbols and timestamp.
Stars: ✭ 24 (-99.51%)
Mutual labels:  color, ansi
terminal-style
🎨 Return your terminal message in style! Change the text style, text color and text background color from the terminal, console or shell interface with ANSI color codes. Support for Laravel and Composer.
Stars: ✭ 16 (-99.67%)
Mutual labels:  color, ansi
Mordant
Full-featured text styling for Kotlin command-line applications
Stars: ✭ 382 (-92.21%)
Mutual labels:  ansi, color
Hue
🎨 Hue is the all-in-one coloring utility that you'll ever need.
Stars: ✭ 3,306 (-32.59%)
Mutual labels:  color
Colorls
A Ruby gem that beautifies the terminal's ls command, with color and font-awesome icons. 🎉
Stars: ✭ 3,896 (-20.55%)
Mutual labels:  color

color PkgGoDev

Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang). It has support for Windows too! The API can be used in several ways, pick one that suits you.

Color

Install

go get github.com/fatih/color

Examples

Standard colors

// 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

// 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)

// 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)

// 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)

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)

// 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

// 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 function

fmt.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 environment variable is set (regardless of its value).

Color has support to disable/enable colors programatically 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:

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:

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

License

The MIT License (MIT) - see LICENSE.md for more details

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].