style.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package style
  2. import (
  3. "servitor/ansi"
  4. "strconv"
  5. "strings"
  6. "servitor/config"
  7. )
  8. func background(text string, rgb string) string {
  9. prefix := "48;2;" + rgb
  10. return ansi.Apply(text, prefix)
  11. }
  12. func foreground(text string, rgb string) string {
  13. prefix := "38;2;" + rgb
  14. return ansi.Apply(text, prefix)
  15. }
  16. func Bold(text string) string {
  17. return ansi.Apply(text, "1")
  18. }
  19. func Strikethrough(text string) string {
  20. return ansi.Apply(text, "9")
  21. }
  22. func Underline(text string) string {
  23. return ansi.Apply(text, "4")
  24. }
  25. func Italic(text string) string {
  26. return ansi.Apply(text, "3")
  27. }
  28. func Code(text string) string {
  29. return background(text, config.Parsed.Style.Colors.Code)
  30. }
  31. func Highlight(text string) string {
  32. return background(text, config.Parsed.Style.Colors.Highlight)
  33. }
  34. func Color(text string) string {
  35. return foreground(text, config.Parsed.Style.Colors.Primary)
  36. }
  37. func Problem(issue error) string {
  38. return Red(issue.Error())
  39. }
  40. func Red(text string) string {
  41. return foreground(text, config.Parsed.Style.Colors.Error)
  42. }
  43. func Link(text string, number int) string {
  44. return Color(Underline(text) + superscript(number))
  45. }
  46. func CodeBlock(text string) string {
  47. return Code(text)
  48. }
  49. func QuoteBlock(text string) string {
  50. prefixed := ansi.Indent(text, "▌", true)
  51. return Color(prefixed)
  52. }
  53. func LinkBlock(text string, number int) string {
  54. return "‣ " + ansi.Indent(Link(text, number), " ", false)
  55. }
  56. func Header(text string, level uint) string {
  57. indented := ansi.Indent(text, strings.Repeat(" ", int(level+1)), false)
  58. withPrefix := strings.Repeat("⯁", int(level)) + " " + indented
  59. return Color(Bold(withPrefix))
  60. }
  61. func Bullet(text string) string {
  62. return "• " + ansi.Indent(text, " ", false)
  63. }
  64. func superscript(value int) string {
  65. text := strconv.Itoa(value)
  66. return strings.Map(func(input rune) rune {
  67. switch input {
  68. case '0':
  69. return '\u2070'
  70. case '1':
  71. return '\u00B9'
  72. case '2':
  73. return '\u00B2'
  74. case '3':
  75. return '\u00B3'
  76. case '4':
  77. return '\u2074'
  78. case '5':
  79. return '\u2075'
  80. case '6':
  81. return '\u2076'
  82. case '7':
  83. return '\u2077'
  84. case '8':
  85. return '\u2078'
  86. case '9':
  87. return '\u2079'
  88. default:
  89. panic("can't superscript non-digit")
  90. }
  91. }, text)
  92. }