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.

83 lines
1.7 KiB

9 years ago
9 years ago
  1. package common
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. const (
  7. ANSIReset = "\x1b[0m"
  8. ANSIBright = "\x1b[1m"
  9. ANSIDim = "\x1b[2m"
  10. ANSIUnderscore = "\x1b[4m"
  11. ANSIBlink = "\x1b[5m"
  12. ANSIReverse = "\x1b[7m"
  13. ANSIHidden = "\x1b[8m"
  14. ANSIFgBlack = "\x1b[30m"
  15. ANSIFgRed = "\x1b[31m"
  16. ANSIFgGreen = "\x1b[32m"
  17. ANSIFgYellow = "\x1b[33m"
  18. ANSIFgBlue = "\x1b[34m"
  19. ANSIFgMagenta = "\x1b[35m"
  20. ANSIFgCyan = "\x1b[36m"
  21. ANSIFgWhite = "\x1b[37m"
  22. ANSIBgBlack = "\x1b[40m"
  23. ANSIBgRed = "\x1b[41m"
  24. ANSIBgGreen = "\x1b[42m"
  25. ANSIBgYellow = "\x1b[43m"
  26. ANSIBgBlue = "\x1b[44m"
  27. ANSIBgMagenta = "\x1b[45m"
  28. ANSIBgCyan = "\x1b[46m"
  29. ANSIBgWhite = "\x1b[47m"
  30. )
  31. // color the string s with color 'color'
  32. // unless s is already colored
  33. func treat(s string, color string) string {
  34. if len(s) > 2 && s[:2] == "\x1b[" {
  35. return s
  36. }
  37. return color + s + ANSIReset
  38. }
  39. func treatAll(color string, args ...interface{}) string {
  40. var parts []string
  41. for _, arg := range args {
  42. parts = append(parts, treat(fmt.Sprintf("%v", arg), color))
  43. }
  44. return strings.Join(parts, "")
  45. }
  46. func Black(args ...interface{}) string {
  47. return treatAll(ANSIFgBlack, args...)
  48. }
  49. func Red(args ...interface{}) string {
  50. return treatAll(ANSIFgRed, args...)
  51. }
  52. func Green(args ...interface{}) string {
  53. return treatAll(ANSIFgGreen, args...)
  54. }
  55. func Yellow(args ...interface{}) string {
  56. return treatAll(ANSIFgYellow, args...)
  57. }
  58. func Blue(args ...interface{}) string {
  59. return treatAll(ANSIFgBlue, args...)
  60. }
  61. func Magenta(args ...interface{}) string {
  62. return treatAll(ANSIFgMagenta, args...)
  63. }
  64. func Cyan(args ...interface{}) string {
  65. return treatAll(ANSIFgCyan, args...)
  66. }
  67. func White(args ...interface{}) string {
  68. return treatAll(ANSIFgWhite, args...)
  69. }