text.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package util
  2. import (
  3. "regexp"
  4. "strings"
  5. "github.com/charmbracelet/lipgloss/v2"
  6. )
  7. // PreventHyphenBreaks replaces regular hyphens with non-breaking hyphens to prevent
  8. // sparse word breaks in hyphenated terms like "claude-code-action".
  9. // This improves readability by keeping hyphenated words together.
  10. // Only preserves hyphens within words, not markdown syntax like bullet points.
  11. func PreventHyphenBreaks(text string) string {
  12. // Use regex to match hyphens that are between word characters
  13. // This preserves hyphens in words like "claude-code-action" but not in "- [ ]"
  14. re := regexp.MustCompile(`(\w)-(\w)`)
  15. return re.ReplaceAllString(text, "$1\u2011$2")
  16. }
  17. // RestoreHyphens converts non-breaking hyphens back to regular hyphens.
  18. // This should be called after text processing (like word wrapping) is complete.
  19. func RestoreHyphens(text string) string {
  20. return strings.ReplaceAll(text, "\u2011", "-")
  21. }
  22. // ProcessTextWithHyphens applies hyphen preservation to text during processing.
  23. // It wraps the provided processFunc with hyphen handling.
  24. func ProcessTextWithHyphens(text string, processFunc func(string) string) string {
  25. preserved := PreventHyphenBreaks(text)
  26. processed := processFunc(preserved)
  27. return RestoreHyphens(processed)
  28. }
  29. // GetMessageContainerFrame calculates the actual horizontal frame size
  30. // (padding + borders) for message containers based on current theme.
  31. func GetMessageContainerFrame() int {
  32. style := lipgloss.NewStyle().
  33. BorderStyle(lipgloss.ThickBorder()).
  34. BorderLeft(true).
  35. BorderRight(true).
  36. PaddingLeft(2).
  37. PaddingRight(2)
  38. return style.GetHorizontalFrameSize()
  39. }
  40. // GetMarkdownContainerFrame calculates the actual horizontal frame size
  41. // for markdown containers based on current theme.
  42. func GetMarkdownContainerFrame() int {
  43. // Markdown containers use the same styling as message containers
  44. return GetMessageContainerFrame()
  45. }