color.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package ircclient
  2. import (
  3. "fmt"
  4. "log"
  5. "regexp"
  6. )
  7. // Stripper removes IRC color and formatting codes.
  8. func Stripper(input string) string {
  9. var rx *regexp.Regexp
  10. var err error
  11. rx, err = regexp.Compile("\x02|\x1d|\x16|\x1e|\x11|\x16|\x1f|\x0f|\x03[0-9]{2}")
  12. if err != nil {
  13. log.Panic("regexp:", err)
  14. }
  15. input = rx.ReplaceAllString(input, "")
  16. return input
  17. }
  18. // Set IRC bold
  19. func Bold(input string) string {
  20. return "\x02" + input + "\x02"
  21. }
  22. // Set IRC Italics
  23. func Italics(input string) string {
  24. return "\x1d" + input + "\x1d"
  25. }
  26. // Set IRC underline
  27. func Underline(input string) string {
  28. return "\x1f" + input + "\x1f"
  29. }
  30. // Set IRC strike through
  31. func Strike(input string) string {
  32. return "\x1e" + input + "\x1e"
  33. }
  34. // Set IRC monospace
  35. func Monospace(input string) string {
  36. return "\x11" + input + "\x11"
  37. }
  38. // Set IRC reverse
  39. func Reverse(input string) string {
  40. return "\x16" + input + "\x16"
  41. }
  42. // Reset IRC reset color and formatting
  43. func Reset() string {
  44. return "\x0f"
  45. }
  46. // Color set IRC color
  47. //
  48. // We always output the color code as 2 digits.
  49. func Color(color int) string {
  50. return fmt.Sprintf("\x03%02d", color)
  51. }
  52. // Format a string with the given color code, and reset.
  53. func ColorString(color int, input string) string {
  54. return fmt.Sprintf("\x03%02d%s\x0f", color, input)
  55. }