e13cb99ade
A headless xterm-compatible emulator (internal/termstate) follows each machine terminal's PTY output under the same lock as the 1 MiB tail. A terminal_output from before the retained tail (new page, reload, gap) now gets output_snapshot: an escape stream that resets the client and restores scrollback (1000 lines), main screen, the alternate screen when in use (via ?1049h so the program's exit brings the shell back), saved cursors, tabs, scroll region, cursor incl. pending wrap, modes (mouse tracking/encoding, bracketed paste, DECCKM, keypad, autowrap, origin, focus, ...), charsets, pen and title, as of output_end; frames continue there. Reads inside the tail stay raw. No Go library was adequate: charmbracelet/x/vt and vito/midterm scroll at 0.2-4 MB/s and hide modes/pen/region; vt10x is unmaintained. termstate runs at 160-200 MB/s. The app treats a snapshot reply as a replay without the dropped-output line, and every replay now starts with RIS so stale modes end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
106 lines
2.7 KiB
Go
106 lines
2.7 KiB
Go
package termstate
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// dump describes everything a snapshot restores, normalised where two
|
|
// states look and behave the same: a never-written cell and a written space
|
|
// of the same pen, trailing blanks, a soft wrap into an empty row, and the
|
|
// content of the screen not in use.
|
|
func dump(t *Terminal) string {
|
|
var b strings.Builder
|
|
fmt.Fprintf(&b, "size %dx%d alt=%v region=%d-%d title=%q shape=%d\n", t.cols, t.rows, t.scr == &t.alt, t.top, t.bottom, t.title, t.cursorShape)
|
|
c := t.cur
|
|
fmt.Fprintf(&b, "cursor %d,%d wrap=%v origin=%v pen=%+v charsets=%c%c shift=%d\n", c.x, c.y, c.wrap, c.origin, c.pen, c.charsets[0], c.charsets[1], c.shift)
|
|
fmt.Fprintf(&b, "modes %+v\n", t.m)
|
|
for _, s := range []*screen{&t.main, &t.alt} {
|
|
if s.savedValid {
|
|
fmt.Fprintf(&b, "saved %d,%d pen=%+v\n", s.saved.x, s.saved.y, s.saved.pen)
|
|
} else {
|
|
b.WriteString("saved none\n")
|
|
}
|
|
}
|
|
fmt.Fprintf(&b, "tabs %v\n", t.tabs)
|
|
var main []*line
|
|
for i := 0; i < t.scrollbackLen(); i++ {
|
|
main = append(main, t.scrollbackLine(i))
|
|
}
|
|
fmt.Fprintf(&b, "scrollback %d\n", t.scrollbackLen())
|
|
main = append(main, t.main.lines...)
|
|
for i, l := range main {
|
|
cont := l.cont && i+1 < len(main) && !blankLine(main[i+1])
|
|
b.WriteString(dumpLine(l, cont))
|
|
}
|
|
{
|
|
b.WriteString("alt\n")
|
|
for _, l := range t.alt.lines {
|
|
b.WriteString(dumpLine(l, false))
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func dumpLine(l *line, cont bool) string {
|
|
cells := make([]Cell, len(l.cells))
|
|
copy(cells, l.cells)
|
|
for i := range cells {
|
|
if cells[i].R == 0 && cells[i].Kind == cellNarrow {
|
|
cells[i].R = ' '
|
|
}
|
|
}
|
|
for len(cells) > 0 && cells[len(cells)-1] == (Cell{R: ' '}) {
|
|
cells = cells[:len(cells)-1]
|
|
}
|
|
var b strings.Builder
|
|
if cont {
|
|
b.WriteString("+")
|
|
}
|
|
pen := Attr{}
|
|
for x, c := range cells {
|
|
if c.A != pen {
|
|
fmt.Fprintf(&b, "{%x/%x/%x}", c.A.Fg, c.A.Bg, c.A.Flags)
|
|
pen = c.A
|
|
}
|
|
switch c.Kind {
|
|
case cellWideTail:
|
|
b.WriteString("~")
|
|
default:
|
|
b.WriteRune(c.R)
|
|
}
|
|
if m := l.marks[x]; m != "" {
|
|
b.WriteString("<" + m + ">")
|
|
}
|
|
}
|
|
b.WriteString("|\n")
|
|
return b.String()
|
|
}
|
|
|
|
func text(t *Terminal) string { return t.Text() }
|
|
|
|
// history is the scrollback's text, oldest first.
|
|
func history(t *Terminal) []string {
|
|
var lines []string
|
|
for i := 0; i < t.scrollbackLen(); i++ {
|
|
var b strings.Builder
|
|
for _, c := range t.scrollbackLine(i).cells {
|
|
if c.R != 0 && c.Kind != cellWideTail {
|
|
b.WriteRune(c.R)
|
|
}
|
|
}
|
|
lines = append(lines, b.String())
|
|
}
|
|
return lines
|
|
}
|
|
|
|
// blankLine: nothing but spaces and never-written cells of the default pen.
|
|
func blankLine(l *line) bool {
|
|
for _, c := range l.cells {
|
|
if c != (Cell{}) && c != (Cell{R: ' '}) {
|
|
return false
|
|
}
|
|
}
|
|
return len(l.marks) == 0
|
|
}
|