MCPcopy Create free account
hub / github.com/B33pBeeps/redthread / splitAtVisual

Function splitAtVisual

internal/app/render.go:252–284  ·  view source on GitHub ↗

--- ANSI-aware string splicing ------------------------------------- splitAtVisual cuts `s` at visible cell column `col`. CSI escape sequences (`\x1b[...{letter}`) are passed through unchanged and don't count toward `col`.

(s string, col int)

Source from the content-addressed store, hash-verified

250// sequences (`\x1b[...{letter}`) are passed through unchanged and don't
251// count toward `col`.
252func splitAtVisual(s string, col int) (string, string) {
253 visible := 0
254 i := 0
255 for i < len(s) {
256 if s[i] == '\x1b' && i+1 < len(s) {
257 j := i + 2 // skip ESC + next byte (usually '[')
258 for j < len(s) {
259 ch := s[j]
260 if (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') {
261 j++
262 break
263 }
264 j++
265 }
266 if j <= len(s) {
267 i = j
268 continue
269 }
270 break
271 }
272 if visible >= col {
273 break
274 }
275 _, size := utf8Decode(s[i:])
276 if size == 0 {
277 i++
278 continue
279 }
280 i += size
281 visible++
282 }
283 return s[:i], s[i:]
284}
285
286// visibleWidth returns the visible-cell width of a string, ignoring CSI.
287func visibleWidth(s string) int {

Callers 1

SpliceOverlayFunction · 0.85

Calls 1

utf8DecodeFunction · 0.85

Tested by

no test coverage detected