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>
101 lines
3.3 KiB
Go
101 lines
3.3 KiB
Go
package termstate
|
||
|
||
import (
|
||
"fmt"
|
||
"math/rand"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// randomOutput mixes text and the sequences full-screen programs write.
|
||
func randomOutput(r *rand.Rand, n int) []byte {
|
||
pieces := []func() string{
|
||
func() string {
|
||
s := strings.Repeat(string(rune('a'+r.Intn(26))), 1+r.Intn(30))
|
||
if r.Intn(4) == 0 {
|
||
// A repeat follows the character it repeats.
|
||
s += fmt.Sprintf("\x1b[%db", r.Intn(30))
|
||
}
|
||
return s
|
||
},
|
||
func() string { return []string{"日本", "é", "é", "😀", "€", ""}[r.Intn(6)] },
|
||
func() string { return []string{"\r", "\n", "\r\n", "\t", "\b", "\x0e", "\x0f", "\x07"}[r.Intn(8)] },
|
||
func() string { return fmt.Sprintf("\x1b[%d;%dH", r.Intn(12), r.Intn(30)) },
|
||
func() string { return fmt.Sprintf("\x1b[%d%c", r.Intn(4), "ABCDEFGIJKLMPSTXZ@`adeg"[r.Intn(23)]) },
|
||
func() string { return fmt.Sprintf("\x1b[%d;%dr", r.Intn(10), r.Intn(12)) },
|
||
func() string {
|
||
return fmt.Sprintf("\x1b[%sm", []string{"0", "1", "4:3", "7", "31", "38;5;99", "48;2;1;2;3", "38:2::4:5:6", "22;24;27", "44", ""}[r.Intn(11)])
|
||
},
|
||
func() string {
|
||
modes := []int{1, 5, 6, 7, 12, 25, 47, 66, 1000, 1002, 1003, 1004, 1006, 1015, 1047, 1048, 1049, 2004}
|
||
return fmt.Sprintf("\x1b[?%d%c", modes[r.Intn(len(modes))], "hl"[r.Intn(2)])
|
||
},
|
||
func() string {
|
||
return []string{"\x1b[4h", "\x1b[4l", "\x1b[20h", "\x1b[20l", "\x1b[3 q", "\x1b[!p"}[r.Intn(6)]
|
||
},
|
||
func() string {
|
||
return []string{"\x1b7", "\x1b8", "\x1bD", "\x1bE", "\x1bM", "\x1bH", "\x1b=", "\x1b>", "\x1b(0", "\x1b(B", "\x1b)0", "\x1b#8"}[r.Intn(12)]
|
||
},
|
||
func() string {
|
||
return []string{"\x1b]2;t\x07", "\x1b]0;title\x1b\\", "\x1bPq#0;1\x1b\\", "\x1b_apc\x1b\\", "\x1b[>4;1m", "\x1b[?u"}[r.Intn(6)]
|
||
},
|
||
func() string { return "\x1bc" },
|
||
}
|
||
var b strings.Builder
|
||
for b.Len() < n {
|
||
i := r.Intn(len(pieces))
|
||
if i == len(pieces)-1 && r.Intn(20) != 0 {
|
||
continue
|
||
}
|
||
b.WriteString(pieces[i]())
|
||
}
|
||
return []byte(b.String())
|
||
}
|
||
|
||
// Snapshots of random output at random cuts reproduce the state, and the
|
||
// rest of the output then gives the same state as writing it all.
|
||
func TestSnapshotRandomOutput(t *testing.T) {
|
||
for seed := int64(0); seed < 500; seed++ {
|
||
r := rand.New(rand.NewSource(seed))
|
||
stream := randomOutput(r, 3000)
|
||
cols, rows := 10+r.Intn(30), 3+r.Intn(10)
|
||
whole := New(cols, rows, 20)
|
||
whole.Write(stream)
|
||
for k := 0; k < 8; k++ {
|
||
cut := r.Intn(len(stream) + 1)
|
||
term := New(cols, rows, 20)
|
||
term.Write(stream[:cut])
|
||
if term.cur.origin && (term.cur.y < term.top || term.cur.y > term.bottom) {
|
||
// Origin mode with the cursor outside the scroll region (a
|
||
// DECRC can leave it there) has no sequence to recreate it.
|
||
continue
|
||
}
|
||
rep := replica(term, stream[:cut])
|
||
sameState(t, fmt.Sprintf("seed %d cut %d", seed, cut), term, rep)
|
||
rep.Write(stream[cut:])
|
||
sameState(t, fmt.Sprintf("seed %d cut %d then rest", seed, cut), whole, rep)
|
||
}
|
||
}
|
||
}
|
||
|
||
// Arbitrary bytes and resizes never panic.
|
||
func TestArbitraryBytes(t *testing.T) {
|
||
r := rand.New(rand.NewSource(1))
|
||
term := New(13, 5, 7)
|
||
buf := make([]byte, 4096)
|
||
for i := 0; i < 400; i++ {
|
||
r.Read(buf)
|
||
for j := range buf {
|
||
if r.Intn(4) == 0 {
|
||
buf[j] = "\x1b[;?0123456789mHJKr\x07\n\r"[r.Intn(21)]
|
||
}
|
||
}
|
||
term.Write(buf)
|
||
if i%50 == 0 {
|
||
term.Resize(1+r.Intn(40), 1+r.Intn(20))
|
||
}
|
||
snap, _ := term.Snapshot()
|
||
New(term.cols, term.rows, 7).Write(snap)
|
||
}
|
||
}
|