Files
sshtermy/third_party/vt/utf8.go
T
db9979andClaude Opus 5.5 2513a7818d MIT-Lizenz, weniger Speicher pro Tab, schnellere Ausgabe und Manager (#47)
* MIT-Lizenz; Terminal-Verlauf kompakt, schnellerer Bildlauf, Manager mit vielen Verbindungen schneller

- LICENSE (MIT), winget/Homebrew/apt-Metadaten, README, Webseite, --version
- termemu: Verlauf mit 8 statt 112 Byte pro Zeichen, Ringpuffer
- third_party/vt: angepasste Kopie (rotateLines, SetScrollbackHook)
- Store: Connections() zwischengespeichert; Manager: buildRows ohne Teillisten,
  Fußzeile misst Breiten einmal
- Benchmarks: make bench

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUjBAAPCAZVnGzCZxdS

* Testprogramm aus dem Commit entfernt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUjBAAPCAZVnGzCZxdS

* Testprogramm entfernt, *.test ignorieren

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUjBAAPCAZVnGzCZxdS

* TestSerialSession: auf stehenden Prompt warten statt auf das erste #

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUjBAAPCAZVnGzCZxdS

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-06 10:02:26 +01:00

134 lines
4.0 KiB
Go

package vt
import (
"unicode/utf8"
uv "github.com/charmbracelet/ultraviolet"
"github.com/charmbracelet/x/ansi"
)
// printableASCII reports whether r is a character that can never join the
// grapheme cluster beside it. No ASCII character is a combining mark, a ZWJ, a
// spacing mark or a regional indicator, and CR and LF are not printable, so a
// cluster boundary between two of these is certain without asking the
// segmenter.
func printableASCII(r rune) bool {
return r >= ansi.SP && r < ansi.DEL
}
// handlePrint handles printable characters.
//
// A character is held back rather than printed straight away, because the one
// after it may be a combining mark that belongs to the same cluster. Printing
// "e" from "e\u0301" before the mark arrives puts the base in one cell and
// leaves the mark to become a zero-width cell of its own, which the next write
// or erase then destroys.
func (e *Emulator) handlePrint(r rune) {
// No ASCII character can extend the cluster before it: none of them is
// Extend, ZWJ, SpacingMark, Prepend, a regional indicator, or part of an
// Indic conjunct. So an ASCII character arriving is proof that whatever is
// buffered is finished, and the buffer can go out.
//
// This is also what keeps the buffer from being re-segmented as it grows.
// Asking where the clusters are on every character costs the length of the
// buffer each time, which is quadratic over a long run of combining marks
// that never resolves into more than one cluster.
if printableASCII(r) {
// Two printable ASCII characters in a row is the common case by a wide
// margin, and needs no segmenting at all.
if len(e.grapheme) == 1 && printableASCII(rune(e.grapheme[0])) {
e.handleGrapheme(string(e.grapheme[0]), 1)
e.grapheme = utf8.AppendRune(e.grapheme[:0], r)
return
}
e.flushGrapheme()
}
e.grapheme = utf8.AppendRune(e.grapheme, r)
}
// flushGrapheme flushes the current grapheme buffer, if any, and handles the
// grapheme as a single unit.
func (e *Emulator) flushGrapheme() {
if len(e.grapheme) == 0 {
return
}
// XXX: We always use [ansi.GraphemeWidth] here to report accurate widths
// and it's up to the caller to decide how to handle Unicode vs non-Unicode
// modes.
method := ansi.GraphemeWidth
graphemes := e.grapheme
for len(graphemes) > 0 {
cluster, width := ansi.FirstGraphemeCluster(graphemes, method)
if len(cluster) == 0 {
break
}
e.handleGrapheme(string(cluster), width)
graphemes = graphemes[len(cluster):]
}
e.grapheme = e.grapheme[:0] // Reset the grapheme buffer.
}
// handleGrapheme handles UTF-8 graphemes.
func (e *Emulator) handleGrapheme(content string, width int) {
awm := e.isModeSet(ansi.ModeAutoWrap)
cell := uv.Cell{
Content: content,
Width: width,
Style: e.scr.cursorPen(),
Link: e.scr.cursorLink(),
}
x, y := e.scr.CursorPosition()
if e.atPhantom && awm {
// moves cursor down similar to [Terminal.linefeed] except it doesn't
// respects [ansi.LNM] mode.
// This will reset the phantom state i.e. pending wrap state.
e.index()
_, y = e.scr.CursorPosition()
x = 0
}
// A single shift applies to the next character, whatever that character
// turns out to be. Take it now, so it cannot leak onto the one after this
// when this is a cluster no charset has a mapping for.
single := e.gsingle
e.gsingle = 0
// Handle character set mappings
if len(content) == 1 { //nolint:nestif
var charset CharSet
c := content[0]
if single > 1 && single < 4 {
charset = e.charsets[single]
} else if c < 128 {
charset = e.charsets[e.gl]
} else {
charset = e.charsets[e.gr]
}
if charset != nil {
if r, ok := charset[c]; ok {
cell.Content = r
cell.Width = 1
}
}
}
if cell.Width == 1 && len(content) == 1 {
e.lastChar, _ = utf8.DecodeRuneInString(content)
}
e.scr.SetCell(x, y, &cell)
// Handle phantom state at the end of the line
e.atPhantom = awm && x >= e.scr.Width()-1
if !e.atPhantom {
x += cell.Width
}
// NOTE: We don't reset the phantom state here, we handle it up above.
e.scr.setCursor(x, y, false)
}