StyleViewText applies styleRune to a pre-rendered string while passing through ANSI CSI escape sequences untouched. Useful when post-processing the output of a third-party widget (e.g. bubbles/textarea) so the visible glyphs reflect the board's font without breaking cursor highlighting.
(s string, mode TextStyleMode)
| 391 | // the output of a third-party widget (e.g. bubbles/textarea) so the visible |
| 392 | // glyphs reflect the board's font without breaking cursor highlighting. |
| 393 | func StyleViewText(s string, mode TextStyleMode) string { |
| 394 | if mode == TextPlain { |
| 395 | return s |
| 396 | } |
| 397 | var b strings.Builder |
| 398 | b.Grow(len(s) * 4) |
| 399 | i := 0 |
| 400 | for i < len(s) { |
| 401 | if s[i] == '\x1b' && i+1 < len(s) { |
| 402 | j := i + 2 |
| 403 | for j < len(s) { |
| 404 | ch := s[j] |
| 405 | if (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') { |
| 406 | j++ |
| 407 | break |
| 408 | } |
| 409 | j++ |
| 410 | } |
| 411 | b.WriteString(s[i:j]) |
| 412 | i = j |
| 413 | continue |
| 414 | } |
| 415 | r, size := utf8Decode(s[i:]) |
| 416 | if size == 0 { |
| 417 | b.WriteByte(s[i]) |
| 418 | i++ |
| 419 | continue |
| 420 | } |
| 421 | b.WriteRune(styleRune(r, mode)) |
| 422 | i += size |
| 423 | } |
| 424 | return b.String() |
| 425 | } |
| 426 | |
| 427 | // styleRune — Unicode block offsets: |
| 428 | // |
no test coverage detected