@@ -146,7 +146,10 @@ function readDrawerLayout(column: HTMLElement | null): Omit<DrawerLayout, "compo
146146 } ;
147147}
148148
149- function writeSystemMessage ( terminal : GhosttyTerminalSurface , message : string ) : void {
149+ function writeSystemMessage (
150+ terminal : Pick < GhosttyTerminalSurface , "write" > ,
151+ message : string ,
152+ ) : void {
150153 terminal . write ( `\r\n[terminal] ${ message } \r\n` ) ;
151154}
152155
@@ -173,6 +176,24 @@ export function synchronizeTerminalOutput(
173176 return update . cursor ;
174177}
175178
179+ /** Writes what changed between two session notifications: new output, then a new error. */
180+ export function synchronizeTerminalSession (
181+ terminal : Pick < GhosttyTerminalSurface , "resetAndWrite" | "write" | "clearSelection" > ,
182+ previous : Pick < TerminalSessionState , "output" | "version" | "error" > ,
183+ session : Pick < TerminalSessionState , "output" | "version" | "error" > ,
184+ cursor : TerminalOutputCursor ,
185+ ) : TerminalOutputCursor {
186+ const nextCursor =
187+ session . version === previous . version && session . output === previous . output
188+ ? cursor
189+ : synchronizeTerminalOutput ( terminal , session , cursor ) ;
190+ // An attach failure can change only the error, keeping the last buffer.
191+ if ( session . error !== null && session . error !== previous . error ) {
192+ writeSystemMessage ( terminal , session . error ) ;
193+ }
194+ return nextCursor ;
195+ }
196+
176197function parseTerminalColor ( value : string , fallback : GhosttyColor ) : GhosttyColor {
177198 if ( typeof document === "undefined" ) return fallback ;
178199
@@ -691,19 +712,14 @@ function TerminalSessionViewport({
691712 if ( nextResizeGeneration !== null ) terminal . resendSize ( ) ;
692713 }
693714 const previous = previousSession ;
715+ previousSession = session ;
694716 synchronizeTerminalStatus ( terminal , session . status , session . version ) ;
695- if ( session . version === previous . version && session . output === previous . output ) {
696- return ;
697- }
698- outputCursorRef . current = synchronizeTerminalOutput (
717+ outputCursorRef . current = synchronizeTerminalSession (
699718 terminal ,
719+ previous ,
700720 session ,
701721 outputCursorRef . current ,
702722 ) ;
703- if ( session . error !== null && session . error !== previous . error ) {
704- writeSystemMessage ( terminal , session . error ) ;
705- }
706- previousSession = session ;
707723 } ) ,
708724 ) ;
709725 if (
0 commit comments