Skip to content

Commit 6e6bd1e

Browse files
committed
fix(desktop): terminal cursor position
1 parent 81ee2d2 commit 6e6bd1e

2 files changed

Lines changed: 14 additions & 26 deletions

File tree

packages/desktop/src/addons/serialize.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -494,14 +494,9 @@ class StringSerializeHandler extends BaseSerializeHandler {
494494
}
495495

496496
if (!excludeFinalCursorPosition) {
497-
// Get cursor position relative to viewport (1-indexed for ANSI)
498-
// cursorY is relative to the viewport, cursorX is column position
499-
const cursorRow = this._buffer.cursorY + 1 // 1-indexed
500-
const cursorCol = this._buffer.cursorX + 1 // 1-indexed
501-
502-
// Use absolute cursor positioning (CUP - Cursor Position)
503-
// This is more reliable than relative moves which depend on knowing
504-
// exactly where the cursor ended up after all the content
497+
const absoluteCursorRow = (this._buffer.baseY ?? 0) + this._buffer.cursorY
498+
const cursorRow = constrain(absoluteCursorRow - this._firstRow + 1, 1, Number.MAX_SAFE_INTEGER)
499+
const cursorCol = this._buffer.cursorX + 1
505500
content += `\u001b[${cursorRow};${cursorCol}H`
506501
}
507502

@@ -549,22 +544,20 @@ export class SerializeAddon implements ITerminalAddon {
549544
return ""
550545
}
551546

552-
const activeBuffer = buffer.active || buffer.normal
553-
if (!activeBuffer) {
547+
const normalBuffer = buffer.normal || buffer.active
548+
const altBuffer = buffer.alternate
549+
550+
if (!normalBuffer) {
554551
return ""
555552
}
556553

557554
let content = options?.range
558-
? this._serializeBufferByRange(activeBuffer, options.range, true)
559-
: this._serializeBufferByScrollback(activeBuffer, options?.scrollback)
560-
561-
// Handle alternate buffer if active and not excluded
562-
if (!options?.excludeAltBuffer) {
563-
const altBuffer = buffer.alternate
564-
if (altBuffer && buffer.active?.type === "alternate") {
565-
const alternateContent = this._serializeBufferByScrollback(altBuffer, undefined)
566-
content += `\u001b[?1049h\u001b[H${alternateContent}`
567-
}
555+
? this._serializeBufferByRange(normalBuffer, options.range, true)
556+
: this._serializeBufferByScrollback(normalBuffer, options?.scrollback)
557+
558+
if (!options?.excludeAltBuffer && buffer.active?.type === "alternate" && altBuffer) {
559+
const alternateContent = this._serializeBufferByScrollback(altBuffer, undefined)
560+
content += `\u001b[?1049h\u001b[H${alternateContent}`
568561
}
569562

570563
return content

packages/desktop/src/components/terminal.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,14 @@ export const Terminal = (props: TerminalProps) => {
5252
term.open(container)
5353

5454
if (local.pty.buffer) {
55-
const originalSize = { cols: term.cols, rows: term.rows }
56-
let resized = false
5755
if (local.pty.rows && local.pty.cols) {
5856
term.resize(local.pty.cols, local.pty.rows)
59-
resized = true
6057
}
58+
term.reset()
6159
term.write(local.pty.buffer)
6260
if (local.pty.scrollY) {
6361
term.scrollToLine(local.pty.scrollY)
6462
}
65-
if (resized) {
66-
term.resize(originalSize.cols, originalSize.rows)
67-
}
6863
}
6964

7065
container.focus()

0 commit comments

Comments
 (0)