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.

84 lines
1.7 KiB

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. } else {
  37. return color + s + ANSIReset
  38. }
  39. }
  40. func treatAll(color string, args ...interface{}) string {
  41. var parts []string
  42. for _, arg := range args {
  43. parts = append(parts, treat(fmt.Sprintf("%v", arg), color))
  44. }
  45. return strings.Join(parts, "")
  46. }
  47. func Black(args ...interface{}) string {
  48. return treatAll(ANSIFgBlack, args...)
  49. }
  50. func Red(args ...interface{}) string {
  51. return treatAll(ANSIFgRed, args...)
  52. }
  53. func Green(args ...interface{}) string {
  54. return treatAll(ANSIFgGreen, args...)
  55. }
  56. func Yellow(args ...interface{}) string {
  57. return treatAll(ANSIFgYellow, args...)
  58. }
  59. func Blue(args ...interface{}) string {
  60. return treatAll(ANSIFgBlue, args...)
  61. }
  62. func Magenta(args ...interface{}) string {
  63. return treatAll(ANSIFgMagenta, args...)
  64. }
  65. func Cyan(args ...interface{}) string {
  66. return treatAll(ANSIFgCyan, args...)
  67. }
  68. func White(args ...interface{}) string {
  69. return treatAll(ANSIFgWhite, args...)
  70. }