MCPcopy Create free account
hub / github.com/codehamr/codehamr / wrapRows

Function wrapRows

internal/tui/render.go:190–234  ·  view source on GitHub ↗

wrapRows mirrors bubbles/textarea.wrap()'s row count so the prompt's auto-grow stays in lock step with what the textarea renders: word-boundary aware with a hard-wrap fallback for over-wide words, plus the trailing cursor-anchor row when content exactly fills the width. Adapted from charmbracelet/bu

(s string, width int)

Source from the content-addressed store, hash-verified

188// auto-grow stays in lock step with what the textarea renders: word-boundary
189// aware with a hard-wrap fallback for over-wide words, plus the trailing
190// cursor-anchor row when content exactly fills the width.
191// Adapted from charmbracelet/bubbles v0.20 textarea.
192//
193// Caveat: width is summed per-rune (runewidth) rather than per grapheme cluster
194// like bubbles' uniseg, so ASCII and CJK match exactly, but a multi-rune cluster
195// (a ZWJ-family emoji, a keycap) can over-count, harmlessly over-growing the
196// prompt by a row on emoji-heavy input. Not worth pulling in uniseg for that.
197func wrapRows(s string, width int) int {
198 if width <= 0 {
199 return 1
200 }
201 row := 0
202 var lineW, wordW, spaces, charW int
203 var hadWord bool
204
205 for _, r := range s {
206 charW = 0
207 if unicode.IsSpace(r) {
208 spaces++
209 } else {
210 charW = runewidth.RuneWidth(r)
211 wordW += charW
212 hadWord = true
213 }
214
215 switch {
216 case spaces > 0:
217 if lineW+wordW+spaces > width {
218 row++
219 lineW = wordW + spaces
220 } else {
221 lineW += wordW + spaces
222 }
223 wordW, spaces = 0, 0
224 hadWord = false
225 case hadWord && wordW+charW > width:
226 // Space-less word grew past the width; matches bubbles'
227 // StringWidth(word)+lastCharLen check.
228 if lineW > 0 {
229 row++
230 }
231 lineW = wordW
232 wordW = 0
233 hadWord = false
234 }
235 }
236
237 if lineW+wordW+spaces >= width {

Callers 2

visualPromptLinesMethod · 0.85

Calls

no outgoing calls

Tested by 1