@@ -7,6 +7,8 @@ import EditorFile from "lib/editorFile";
77import TerminalComponent from "./terminal" ;
88import "@xterm/xterm/css/xterm.css" ;
99import toast from "components/toast" ;
10+ import confirm from "dialogs/confirm" ;
11+ import appSettings from "lib/settings" ;
1012import helpers from "utils/helpers" ;
1113
1214const TERMINAL_SESSION_STORAGE_KEY = "acodeTerminalSessions" ;
@@ -17,12 +19,15 @@ class TerminalManager {
1719 this . terminalCounter = 0 ;
1820 }
1921
20- getPersistedSessions ( ) {
22+ async getPersistedSessions ( ) {
2123 try {
2224 const stored = helpers . parseJSON (
2325 localStorage . getItem ( TERMINAL_SESSION_STORAGE_KEY ) ,
2426 ) ;
2527 if ( ! Array . isArray ( stored ) ) return [ ] ;
28+ if ( ! ( await Terminal . isAxsRunning ( ) ) ) {
29+ return [ ] ;
30+ }
2631 return stored
2732 . map ( ( entry ) => {
2833 if ( ! entry ) return null ;
@@ -56,11 +61,11 @@ class TerminalManager {
5661 }
5762 }
5863
59- persistTerminalSession ( pid , name ) {
64+ async persistTerminalSession ( pid , name ) {
6065 if ( ! pid ) return ;
6166
6267 const pidStr = String ( pid ) ;
63- const sessions = this . getPersistedSessions ( ) ;
68+ const sessions = await this . getPersistedSessions ( ) ;
6469 const existingIndex = sessions . findIndex (
6570 ( session ) => session . pid === pidStr ,
6671 ) ;
@@ -81,11 +86,11 @@ class TerminalManager {
8186 this . savePersistedSessions ( sessions ) ;
8287 }
8388
84- removePersistedSession ( pid ) {
89+ async removePersistedSession ( pid ) {
8590 if ( ! pid ) return ;
8691
8792 const pidStr = String ( pid ) ;
88- const sessions = this . getPersistedSessions ( ) ;
93+ const sessions = await this . getPersistedSessions ( ) ;
8994 const nextSessions = sessions . filter ( ( session ) => session . pid !== pidStr ) ;
9095
9196 if ( nextSessions . length !== sessions . length ) {
@@ -94,7 +99,7 @@ class TerminalManager {
9499 }
95100
96101 async restorePersistedSessions ( ) {
97- const sessions = this . getPersistedSessions ( ) ;
102+ const sessions = await this . getPersistedSessions ( ) ;
98103 if ( ! sessions . length ) return ;
99104
100105 const manager = window . editorManager ;
@@ -183,7 +188,7 @@ class TerminalManager {
183188 } ) ;
184189
185190 // Wait for tab creation and setup
186- const terminalInstance = await new Promise ( ( resolve , reject ) => {
191+ return await new Promise ( ( resolve , reject ) => {
187192 setTimeout ( async ( ) => {
188193 try {
189194 // Mount terminal component
@@ -220,7 +225,10 @@ class TerminalManager {
220225 this . terminals . set ( uniqueId , instance ) ;
221226
222227 if ( terminalComponent . serverMode && terminalComponent . pid ) {
223- this . persistTerminalSession ( terminalComponent . pid , terminalName ) ;
228+ await this . persistTerminalSession (
229+ terminalComponent . pid ,
230+ terminalName ,
231+ ) ;
224232 }
225233 resolve ( instance ) ;
226234 } catch ( error ) {
@@ -229,8 +237,6 @@ class TerminalManager {
229237 }
230238 } , 100 ) ;
231239 } ) ;
232-
233- return terminalInstance ;
234240 } catch ( error ) {
235241 console . error ( "Failed to create terminal:" , error ) ;
236242 throw error ;
@@ -334,7 +340,7 @@ class TerminalManager {
334340 } ) ;
335341
336342 // Wait for tab creation and setup
337- const terminalInstance = await new Promise ( ( resolve , reject ) => {
343+ return await new Promise ( ( resolve , reject ) => {
338344 setTimeout ( async ( ) => {
339345 try {
340346 // Mount terminal component
@@ -374,8 +380,6 @@ class TerminalManager {
374380 }
375381 } , 100 ) ;
376382 } ) ;
377-
378- return terminalInstance ;
379383 }
380384
381385 /**
@@ -384,7 +388,7 @@ class TerminalManager {
384388 * @param {TerminalComponent } terminalComponent - Terminal component
385389 * @param {string } terminalId - Terminal ID
386390 */
387- setupTerminalHandlers ( terminalFile , terminalComponent , terminalId ) {
391+ async setupTerminalHandlers ( terminalFile , terminalComponent , terminalId ) {
388392 // Handle tab focus/blur
389393 terminalFile . onfocus = ( ) => {
390394 // Guarded fit on focus: only fit if cols/rows would change, then focus
@@ -413,6 +417,22 @@ class TerminalManager {
413417 this . closeTerminal ( terminalId ) ;
414418 } ;
415419
420+ terminalFile . _skipTerminalCloseConfirm = false ;
421+ const originalRemove = terminalFile . remove . bind ( terminalFile ) ;
422+ terminalFile . remove = async ( force = false ) => {
423+ if (
424+ ! terminalFile . _skipTerminalCloseConfirm &&
425+ this . shouldConfirmTerminalClose ( )
426+ ) {
427+ const message = `${ strings [ "close" ] } ${ strings [ "terminal" ] } ?` ;
428+ const shouldClose = await confirm ( strings [ "confirm" ] , message ) ;
429+ if ( ! shouldClose ) return ;
430+ }
431+
432+ terminalFile . _skipTerminalCloseConfirm = false ;
433+ return originalRemove ( force ) ;
434+ } ;
435+
416436 // Enhanced resize handling with debouncing
417437 let resizeTimeout = null ;
418438 const RESIZE_DEBOUNCE = 200 ;
@@ -486,14 +506,17 @@ class TerminalManager {
486506 this . closeTerminal ( terminalId ) ;
487507 } ;
488508
489- terminalComponent . onTitleChange = ( title ) => {
509+ terminalComponent . onTitleChange = async ( title ) => {
490510 if ( title ) {
491511 // Format terminal title as "Terminal ! - title"
492512 const formattedTitle = `Terminal ${ this . terminalCounter } - ${ title } ` ;
493513 terminalFile . filename = formattedTitle ;
494514
495515 if ( terminalComponent . serverMode && terminalComponent . pid ) {
496- this . persistTerminalSession ( terminalComponent . pid , formattedTitle ) ;
516+ await this . persistTerminalSession (
517+ terminalComponent . pid ,
518+ formattedTitle ,
519+ ) ;
497520 }
498521
499522 // Refresh the header subtitle if this terminal is active
@@ -519,6 +542,7 @@ class TerminalManager {
519542 }
520543
521544 this . closeTerminal ( terminalId ) ;
545+ terminalFile . _skipTerminalCloseConfirm = true ;
522546 terminalFile . remove ( true ) ;
523547 toast ( message ) ;
524548 } ;
@@ -731,6 +755,14 @@ class TerminalManager {
731755 }
732756 } ) ;
733757 }
758+
759+ shouldConfirmTerminalClose ( ) {
760+ const settings = appSettings ?. value ?. terminalSettings ;
761+ if ( settings && settings . confirmTabClose === false ) {
762+ return false ;
763+ }
764+ return true ;
765+ }
734766}
735767
736768// Create singleton instance
0 commit comments