wrapDisplayText wraps text based on display cell width.
(text string, maxWidth int)
| 111 | |
| 112 | // wrapDisplayText wraps text based on display cell width. |
| 113 | func wrapDisplayText(text string, maxWidth int) string { |
| 114 | if maxWidth <= 0 { |
| 115 | return text |
| 116 | } |
| 117 | words := strings.Fields(text) |
| 118 | if len(words) == 0 { |
| 119 | return text |
| 120 | } |
| 121 | var lines []string |
| 122 | var current string |
| 123 | for _, w := range words { |
| 124 | if lipgloss.Width(current) == 0 { |
| 125 | current = w |
| 126 | continue |
| 127 | } |
| 128 | if lipgloss.Width(current+" "+w) <= maxWidth { |
| 129 | current += " " + w |
| 130 | } else { |
| 131 | lines = append(lines, current) |
| 132 | current = w |
| 133 | } |
| 134 | } |
| 135 | if current != "" { |
| 136 | lines = append(lines, current) |
| 137 | } |
| 138 | return strings.Join(lines, "\n") |
| 139 | } |