You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Color [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/fatih/color) [![Build Status](http://img.shields.io/travis/fatih/color.svg?style=flat-square)](https://travis-ci.org/fatih/color)
  2. Color lets you use colorized outputs in terms of [ANSI Escape
  3. Codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors) in Go (Golang). It
  4. has support for Windows too! The API can be used in several ways, pick one that
  5. suits you.
  6. ![Color](http://i.imgur.com/c1JI0lA.png)
  7. ## Install
  8. ```bash
  9. go get github.com/fatih/color
  10. ```
  11. Note that the `vendor` folder is here for stability. Remove the folder if you
  12. already have the dependencies in your GOPATH.
  13. ## Examples
  14. ### Standard colors
  15. ```go
  16. // Print with default helper functions
  17. color.Cyan("Prints text in cyan.")
  18. // A newline will be appended automatically
  19. color.Blue("Prints %s in blue.", "text")
  20. // These are using the default foreground colors
  21. color.Red("We have red")
  22. color.Magenta("And many others ..")
  23. ```
  24. ### Mix and reuse colors
  25. ```go
  26. // Create a new color object
  27. c := color.New(color.FgCyan).Add(color.Underline)
  28. c.Println("Prints cyan text with an underline.")
  29. // Or just add them to New()
  30. d := color.New(color.FgCyan, color.Bold)
  31. d.Printf("This prints bold cyan %s\n", "too!.")
  32. // Mix up foreground and background colors, create new mixes!
  33. red := color.New(color.FgRed)
  34. boldRed := red.Add(color.Bold)
  35. boldRed.Println("This will print text in bold red.")
  36. whiteBackground := red.Add(color.BgWhite)
  37. whiteBackground.Println("Red text with white background.")
  38. ```
  39. ### Use your own output (io.Writer)
  40. ```go
  41. // Use your own io.Writer output
  42. color.New(color.FgBlue).Fprintln(myWriter, "blue color!")
  43. blue := color.New(color.FgBlue)
  44. blue.Fprint(writer, "This will print text in blue.")
  45. ```
  46. ### Custom print functions (PrintFunc)
  47. ```go
  48. // Create a custom print function for convenience
  49. red := color.New(color.FgRed).PrintfFunc()
  50. red("Warning")
  51. red("Error: %s", err)
  52. // Mix up multiple attributes
  53. notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
  54. notice("Don't forget this...")
  55. ```
  56. ### Custom fprint functions (FprintFunc)
  57. ```go
  58. blue := color.New(FgBlue).FprintfFunc()
  59. blue(myWriter, "important notice: %s", stars)
  60. // Mix up with multiple attributes
  61. success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
  62. success(myWriter, "Don't forget this...")
  63. ```
  64. ### Insert into noncolor strings (SprintFunc)
  65. ```go
  66. // Create SprintXxx functions to mix strings with other non-colorized strings:
  67. yellow := color.New(color.FgYellow).SprintFunc()
  68. red := color.New(color.FgRed).SprintFunc()
  69. fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))
  70. info := color.New(color.FgWhite, color.BgGreen).SprintFunc()
  71. fmt.Printf("This %s rocks!\n", info("package"))
  72. // Use helper functions
  73. fmt.Println("This", color.RedString("warning"), "should be not neglected.")
  74. fmt.Printf("%v %v\n", color.GreenString("Info:"), "an important message.")
  75. // Windows supported too! Just don't forget to change the output to color.Output
  76. fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
  77. ```
  78. ### Plug into existing code
  79. ```go
  80. // Use handy standard colors
  81. color.Set(color.FgYellow)
  82. fmt.Println("Existing text will now be in yellow")
  83. fmt.Printf("This one %s\n", "too")
  84. color.Unset() // Don't forget to unset
  85. // You can mix up parameters
  86. color.Set(color.FgMagenta, color.Bold)
  87. defer color.Unset() // Use it in your function
  88. fmt.Println("All text will now be bold magenta.")
  89. ```
  90. ### Disable color
  91. There might be a case where you want to disable color output (for example to
  92. pipe the standard output of your app to somewhere else). `Color` has support to
  93. disable colors both globally and for single color definition. For example
  94. suppose you have a CLI app and a `--no-color` bool flag. You can easily disable
  95. the color output with:
  96. ```go
  97. var flagNoColor = flag.Bool("no-color", false, "Disable color output")
  98. if *flagNoColor {
  99. color.NoColor = true // disables colorized output
  100. }
  101. ```
  102. It also has support for single color definitions (local). You can
  103. disable/enable color output on the fly:
  104. ```go
  105. c := color.New(color.FgCyan)
  106. c.Println("Prints cyan text")
  107. c.DisableColor()
  108. c.Println("This is printed without any color")
  109. c.EnableColor()
  110. c.Println("This prints again cyan...")
  111. ```
  112. ## Todo
  113. * Save/Return previous values
  114. * Evaluate fmt.Formatter interface
  115. ## Credits
  116. * [Fatih Arslan](https://github.com/fatih)
  117. * Windows support via @mattn: [colorable](https://github.com/mattn/go-colorable)
  118. ## License
  119. The MIT License (MIT) - see [`LICENSE.md`](https://github.com/fatih/color/blob/master/LICENSE.md) for more details