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>
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package termstate
|
|
|
|
// Resize changes the terminal's size without reflowing, as the app's live
|
|
// emulator does: narrowing cuts rows (scrollback too) at the new width;
|
|
// fewer rows move the rows above the cursor into the scrollback (main
|
|
// screen) so the cursor's row stays on screen; more rows bring scrollback
|
|
// back. The scroll region becomes the whole screen.
|
|
func (t *Terminal) Resize(cols, rows int) {
|
|
cols, rows = max(cols, 1), max(rows, 1)
|
|
if cols == t.cols && rows == t.rows {
|
|
return
|
|
}
|
|
if cols < t.cols {
|
|
for _, lines := range [][]*line{t.main.lines, t.alt.lines, t.history} {
|
|
for _, l := range lines {
|
|
l.truncate(cols)
|
|
}
|
|
}
|
|
}
|
|
mainY, altY := &t.main.saved.y, &t.alt.saved.y
|
|
if t.scr == &t.main {
|
|
mainY = &t.cur.y
|
|
} else {
|
|
altY = &t.cur.y
|
|
}
|
|
t.main.lines = t.resizeRows(t.main.lines, rows, cols, mainY, true)
|
|
t.alt.lines = t.resizeRows(t.alt.lines, rows, cols, altY, false)
|
|
// The last row's continuation (if any) is gone.
|
|
t.main.lines[rows-1].cont, t.alt.lines[rows-1].cont = false, false
|
|
old := t.tabs
|
|
t.cols, t.rows = cols, rows
|
|
t.resetTabs()
|
|
copy(t.tabs, old)
|
|
t.top, t.bottom = 0, rows-1
|
|
t.cur.x, t.cur.y = min(t.cur.x, cols-1), min(max(t.cur.y, 0), rows-1)
|
|
t.cur.wrap = t.cur.wrap && t.cur.x == cols-1
|
|
for _, s := range []*screen{&t.main, &t.alt} {
|
|
s.saved.x, s.saved.y = min(s.saved.x, cols-1), min(max(s.saved.y, 0), rows-1)
|
|
}
|
|
}
|
|
|
|
// resizeRows gives one screen rows rows, keeping the row at *anchor
|
|
// visible and moving it with the content.
|
|
func (t *Terminal) resizeRows(lines []*line, rows, cols int, anchor *int, main bool) []*line {
|
|
n := len(lines)
|
|
if rows < n {
|
|
push := min(max(0, *anchor-(rows-1)), n-rows)
|
|
for _, l := range lines[:push] {
|
|
if main {
|
|
t.keep(l)
|
|
}
|
|
}
|
|
*anchor -= push
|
|
return append([]*line(nil), lines[push:push+rows]...)
|
|
}
|
|
resized := make([]*line, 0, rows)
|
|
if main {
|
|
var back []*line
|
|
for len(back) < rows-n {
|
|
l := t.popScrollback()
|
|
if l == nil {
|
|
break
|
|
}
|
|
back = append(back, l)
|
|
}
|
|
for i := len(back) - 1; i >= 0; i-- {
|
|
resized = append(resized, back[i])
|
|
}
|
|
*anchor += len(back)
|
|
}
|
|
resized = append(resized, lines...)
|
|
for len(resized) < rows {
|
|
resized = append(resized, &line{})
|
|
}
|
|
return resized
|
|
}
|