Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
forked fromfatih/color

Color package for Go (golang)

License

NotificationsYou must be signed in to change notification settings

hyunsooda/color

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Color lets you use colorized outputs in terms ofANSI EscapeCodes in Go (Golang). Ithas support for Windows too! The API can be used in several ways, pick one thatsuits you.

Color

Install

go get github.com/fatih/color

Examples

Standard colors

// Print with default helper functionscolor.Cyan("Prints text in cyan.")// A newline will be appended automaticallycolor.Blue("Prints %s in blue.","text")// These are using the default foreground colorscolor.Red("We have red")color.Magenta("And many others ..")

Mix and reuse colors

// Create a new color objectc:=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 outputcolor.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 conveniencered:=color.New(color.FgRed).PrintfFunc()red("Warning")red("Error: %s",err)// Mix up multiple attributesnotice:=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 attributessuccess:=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 functionsfmt.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.Outputfmt.Fprintf(color.Output,"Windows support: %s",color.GreenString("PASS"))

Plug into existing code

// Use handy standard colorscolor.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 parameterscolor.Set(color.FgMagenta,color.Bold)defercolor.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. thego-isatty package will automatically disable color output for non-tty output streams(for example if the output were piped directly toless).

Thecolor package also disables color output if theNO_COLOR environmentvariable is set to a non-empty string.

Color has support to disable/enable colors programmatically both globally andfor 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:

varflagNoColor=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 candisable/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 setcolor.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) - seeLICENSE.md for more details

About

Color package for Go (golang)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go100.0%

[8]ページ先頭

©2009-2025 Movatter.jp