MCPcopy Create free account
hub / github.com/diillson/chatcli / wrapStreamLine

Function wrapStreamLine

cli/agent/ui_renderer.go:196–241  ·  view source on GitHub ↗

wrapStreamLine quebra UMA linha de output cru de tool na largura visível informada. Diferente de wrapText, NÃO colapsa espaços em branco: a indentação inicial é preservada (e repetida nas continuações) para que YAML/JSON estruturado continue legível dentro do box. A quebra é por runa (ANSI/wide-rune

(line string, width int)

Source from the content-addressed store, hash-verified

194// runa (ANSI/wide-rune aware via lipgloss.Width), evitando cortar
195// sequências multibyte no meio.
196func wrapStreamLine(line string, width int) []string {
197 if width <= 0 || VisibleLen(line) <= width {
198 return []string{line}
199 }
200
201 trimmed := strings.TrimLeft(line, " \t")
202 indent := line[:len(line)-len(trimmed)]
203 indentW := VisibleLen(indent)
204 if indentW >= width-1 {
205 // Indentação maior que o box: desiste de preservá-la.
206 indent = ""
207 indentW = 0
208 }
209 chunkW := width - indentW
210 if chunkW < 1 {
211 chunkW = 1
212 }
213
214 var out []string
215 var cur strings.Builder
216 curW := 0
217 flush := func() {
218 out = append(out, indent+cur.String())
219 cur.Reset()
220 curW = 0
221 }
222
223 for _, rr := range trimmed {
224 rw := lipgloss.Width(string(rr))
225 if rw < 1 {
226 rw = 1
227 }
228 if curW+rw > chunkW && cur.Len() > 0 {
229 flush()
230 }
231 cur.WriteRune(rr)
232 curW += rw
233 }
234 if cur.Len() > 0 {
235 flush()
236 }
237 if len(out) == 0 {
238 out = append(out, indent)
239 }
240 return out
241}
242
243// Colorize aplica cores ANSI (exportada com C maiúsculo). O resultado passa
244// por theme.Recolor para que os códigos básicos legados (ColorCyan, ColorYellow,

Callers 5

StreamOutputMethod · 0.85
wrapPreserveFunction · 0.85

Calls 4

VisibleLenFunction · 0.85
StringMethod · 0.45
ResetMethod · 0.45
LenMethod · 0.45