print.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package ansi
  2. import (
  3. "fmt"
  4. "sort"
  5. colorable "github.com/mattn/go-colorable"
  6. )
  7. // PrintStyles prints all style combinations to the terminal.
  8. func PrintStyles() {
  9. // for compatibility with Windows, not needed for *nix
  10. stdout := colorable.NewColorableStdout()
  11. bgColors := []string{
  12. "",
  13. ":black",
  14. ":red",
  15. ":green",
  16. ":yellow",
  17. ":blue",
  18. ":magenta",
  19. ":cyan",
  20. ":white",
  21. }
  22. keys := make([]string, 0, len(Colors))
  23. for k := range Colors {
  24. keys = append(keys, k)
  25. }
  26. sort.Sort(sort.StringSlice(keys))
  27. for _, fg := range keys {
  28. for _, bg := range bgColors {
  29. fmt.Fprintln(stdout, padColor(fg, []string{"" + bg, "+b" + bg, "+bh" + bg, "+u" + bg}))
  30. fmt.Fprintln(stdout, padColor(fg, []string{"+s" + bg, "+i" + bg}))
  31. fmt.Fprintln(stdout, padColor(fg, []string{"+uh" + bg, "+B" + bg, "+Bb" + bg /* backgrounds */, "" + bg + "+h"}))
  32. fmt.Fprintln(stdout, padColor(fg, []string{"+b" + bg + "+h", "+bh" + bg + "+h", "+u" + bg + "+h", "+uh" + bg + "+h"}))
  33. }
  34. }
  35. }
  36. func pad(s string, length int) string {
  37. for len(s) < length {
  38. s += " "
  39. }
  40. return s
  41. }
  42. func padColor(color string, styles []string) string {
  43. buffer := ""
  44. for _, style := range styles {
  45. buffer += Color(pad(color+style, 20), color+style)
  46. }
  47. return buffer
  48. }