@@ -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
0 commit comments