MCPcopy Index your code
hub / github.com/codechenx/FastTableViewer / wrapText

Function wrapText

utils.go:161–207  ·  view source on GitHub ↗

wrapText wraps text to fit within maxWidth characters Returns the wrapped text with newlines

(text string, maxWidth int)

Source from the content-addressed store, hash-verified

159// wrapText wraps text to fit within maxWidth characters
160// Returns the wrapped text with newlines
161func wrapText(text string, maxWidth int) string {
162 if maxWidth <= 0 || len(text) <= maxWidth {
163 return text
164 }
165
166 var result []rune
167 runes := []rune(text)
168 lineStart := 0
169
170 for i := 0; i < len(runes); i++ {
171 // Check if we've reached the wrap point
172 if i-lineStart >= maxWidth {
173 // Find last space before maxWidth for word wrap
174 wrapPoint := i
175 for j := i; j > lineStart; j-- {
176 if runes[j] == ' ' || runes[j] == '\t' || runes[j] == '-' {
177 wrapPoint = j + 1
178 break
179 }
180 }
181
182 // If no good wrap point found, hard wrap at maxWidth
183 if wrapPoint == i && i > lineStart {
184 wrapPoint = lineStart + maxWidth
185 }
186
187 // Add the wrapped line
188 result = append(result, runes[lineStart:wrapPoint]...)
189 result = append(result, '\n')
190
191 // Skip trailing spaces on new line
192 for wrapPoint < len(runes) && (runes[wrapPoint] == ' ' || runes[wrapPoint] == '\t') {
193 wrapPoint++
194 }
195
196 lineStart = wrapPoint
197 i = wrapPoint - 1 // -1 because loop will increment
198 }
199 }
200
201 // Add remaining text
202 if lineStart < len(runes) {
203 result = append(result, runes[lineStart:]...)
204 }
205
206 return string(result)
207}
208
209// truncateText truncates text to maxWidth and adds ellipsis if needed
210func truncateText(text string, maxWidth int) string {

Callers 5

TestWrapText_BasicFunction · 0.85
TestWrapText_LongFunction · 0.85
TestWrapText_EmptyFunction · 0.85
TestWrapText_ExactLengthFunction · 0.85

Calls

no outgoing calls

Tested by 5

TestWrapText_BasicFunction · 0.68
TestWrapText_LongFunction · 0.68
TestWrapText_EmptyFunction · 0.68
TestWrapText_ExactLengthFunction · 0.68