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.

doc.go 4.3KB

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