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.
 
 
 

129 lines
4.1 KiB

  1. /*
  2. Package color is an ANSI color package to output colorized or SGR defined
  3. output to the standard output. The API can be used in several way, pick one
  4. that suits you.
  5. Use simple and default helper functions with predefined foreground colors:
  6. color.Cyan("Prints text in cyan.")
  7. // a newline will be appended automatically
  8. color.Blue("Prints %s in blue.", "text")
  9. // More default foreground colors..
  10. color.Red("We have red")
  11. color.Yellow("Yellow color too!")
  12. color.Magenta("And many others ..")
  13. However there are times where custom color mixes are required. Below are some
  14. examples to create custom color objects and use the print functions of each
  15. separate color object.
  16. // Create a new color object
  17. c := color.New(color.FgCyan).Add(color.Underline)
  18. c.Println("Prints cyan text with an underline.")
  19. // Or just add them to New()
  20. d := color.New(color.FgCyan, color.Bold)
  21. d.Printf("This prints bold cyan %s\n", "too!.")
  22. // Mix up foreground and background colors, create new mixes!
  23. red := color.New(color.FgRed)
  24. boldRed := red.Add(color.Bold)
  25. boldRed.Println("This will print text in bold red.")
  26. whiteBackground := red.Add(color.BgWhite)
  27. whiteBackground.Println("Red text with White background.")
  28. // Use your own io.Writer output
  29. color.New(color.FgBlue).Fprintln(myWriter, "blue color!")
  30. blue := color.New(color.FgBlue)
  31. blue.Fprint(myWriter, "This will print text in blue.")
  32. You can create PrintXxx functions to simplify even more:
  33. // Create a custom print function for convenient
  34. red := color.New(color.FgRed).PrintfFunc()
  35. red("warning")
  36. red("error: %s", err)
  37. // Mix up multiple attributes
  38. notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
  39. notice("don't forget this...")
  40. You can also FprintXxx functions to pass your own io.Writer:
  41. blue := color.New(FgBlue).FprintfFunc()
  42. blue(myWriter, "important notice: %s", stars)
  43. // Mix up with multiple attributes
  44. success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
  45. success(myWriter, don't forget this...")
  46. Or create SprintXxx functions to mix strings with other non-colorized strings:
  47. yellow := New(FgYellow).SprintFunc()
  48. red := New(FgRed).SprintFunc()
  49. fmt.Printf("this is a %s and this is %s.\n", yellow("warning"), red("error"))
  50. info := New(FgWhite, BgGreen).SprintFunc()
  51. fmt.Printf("this %s rocks!\n", info("package"))
  52. Windows support is enabled by default. All Print functions works as intended.
  53. However only for color.SprintXXX functions, user should use fmt.FprintXXX and
  54. set the output to color.Output:
  55. fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
  56. info := New(FgWhite, BgGreen).SprintFunc()
  57. fmt.Fprintf(color.Output, "this %s rocks!\n", info("package"))
  58. Using with existing code is possible. Just use the Set() method to set the
  59. standard output to the given parameters. That way a rewrite of an existing
  60. code is not required.
  61. // Use handy standard colors.
  62. color.Set(color.FgYellow)
  63. fmt.Println("Existing text will be now in Yellow")
  64. fmt.Printf("This one %s\n", "too")
  65. color.Unset() // don't forget to unset
  66. // You can mix up parameters
  67. color.Set(color.FgMagenta, color.Bold)
  68. defer color.Unset() // use it in your function
  69. fmt.Println("All text will be now bold magenta.")
  70. There might be a case where you want to disable color output (for example to
  71. pipe the standard output of your app to somewhere else). `Color` has support to
  72. disable colors both globally and for single color definition. For example
  73. suppose you have a CLI app and a `--no-color` bool flag. You can easily disable
  74. the color output with:
  75. var flagNoColor = flag.Bool("no-color", false, "Disable color output")
  76. if *flagNoColor {
  77. color.NoColor = true // disables colorized output
  78. }
  79. It also has support for single color definitions (local). You can
  80. disable/enable color output on the fly:
  81. c := color.New(color.FgCyan)
  82. c.Println("Prints cyan text")
  83. c.DisableColor()
  84. c.Println("This is printed without any color")
  85. c.EnableColor()
  86. c.Println("This prints again cyan...")
  87. */
  88. package color