style.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package style
  2. import (
  3. "fmt"
  4. "strings"
  5. "mimicry/ansi"
  6. )
  7. func background(text string, r uint8, g uint8, b uint8) string {
  8. prefix := fmt.Sprintf("48;2;%d;%d;%d", r, g, b)
  9. return ansi.Apply(text, prefix)
  10. }
  11. func foreground(text string, r uint8, g uint8, b uint8) string {
  12. prefix := fmt.Sprintf("38;2;%d;%d;%d", r, g, b)
  13. return ansi.Apply(text, prefix)
  14. }
  15. func Bold(text string) string {
  16. return ansi.Apply(text, "1")
  17. }
  18. func Strikethrough(text string) string {
  19. return ansi.Apply(text, "9")
  20. }
  21. func Underline(text string) string {
  22. return ansi.Apply(text, "4")
  23. }
  24. func Italic(text string) string {
  25. return ansi.Apply(text, "3")
  26. }
  27. func Code(text string) string {
  28. return background(text, 75, 75, 75)
  29. }
  30. func Highlight(text string) string {
  31. return background(text, 13, 125, 0)
  32. }
  33. func Color(text string) string {
  34. return foreground(text, 164, 245, 155)
  35. }
  36. func Problem(text error) string {
  37. return foreground(text.Error(), 156, 53, 53)
  38. }
  39. func Link(text string) string {
  40. return Underline(Color(text))
  41. }
  42. func CodeBlock(text string) string {
  43. return Code(text)
  44. }
  45. func QuoteBlock(text string) string {
  46. prefixed := ansi.Indent(text, "▌", true)
  47. return Color(prefixed)
  48. }
  49. func LinkBlock(text string) string {
  50. return "‣ " + ansi.Indent(Link(text), " ", false)
  51. }
  52. func Header(text string, level uint) string {
  53. indented := ansi.Indent(text, strings.Repeat(" ", int(level+1)), false)
  54. withPrefix := strings.Repeat("⯁", int(level)) + " " + indented
  55. return Color(Bold(withPrefix))
  56. }
  57. func Bullet(text string) string {
  58. return "• " + ansi.Indent(text, " ", false)
  59. }