@@ -9,7 +9,11 @@ import { render } from 'ink';
99import { basename } from 'node:path' ;
1010import { AppContainer } from './ui/AppContainer.js' ;
1111import { ConsolePatcher } from './ui/utils/ConsolePatcher.js' ;
12- import { registerCleanup , setupTtyCheck } from './utils/cleanup.js' ;
12+ import {
13+ registerCleanup ,
14+ removeCleanup ,
15+ setupTtyCheck ,
16+ } from './utils/cleanup.js' ;
1317import {
1418 type StartupWarning ,
1519 type Config ,
@@ -89,7 +93,6 @@ export async function startInteractiveUI(
8993 debugMode : config . getDebugMode ( ) ,
9094 } ) ;
9195 consolePatcher . patch ( ) ;
92- registerCleanup ( consolePatcher . cleanup ) ;
9396
9497 const { stdout : inkStdout , stderr : inkStderr } = createWorkingStdio ( ) ;
9598
@@ -167,11 +170,11 @@ export async function startInteractiveUI(
167170 } ,
168171 ) ;
169172
173+ let cleanupLineWrapping : ( ( ) => void ) | undefined ;
170174 if ( useAlternateBuffer ) {
171175 disableLineWrapping ( ) ;
172- registerCleanup ( ( ) => {
173- enableLineWrapping ( ) ;
174- } ) ;
176+ cleanupLineWrapping = ( ) => enableLineWrapping ( ) ;
177+ registerCleanup ( cleanupLineWrapping ) ;
175178 }
176179
177180 checkForUpdates ( settings )
@@ -185,9 +188,48 @@ export async function startInteractiveUI(
185188 }
186189 } ) ;
187190
188- registerCleanup ( ( ) => instance . unmount ( ) ) ;
189-
190- registerCleanup ( setupTtyCheck ( ) ) ;
191+ const cleanupUnmount = ( ) => instance . unmount ( ) ;
192+ registerCleanup ( cleanupUnmount ) ;
193+
194+ const cleanupTtyCheck = setupTtyCheck ( ) ;
195+ registerCleanup ( cleanupTtyCheck ) ;
196+
197+ const cleanupConsolePatcher = ( ) => consolePatcher . cleanup ( ) ;
198+ registerCleanup ( cleanupConsolePatcher ) ;
199+
200+ try {
201+ await instance . waitUntilExit ( ) ;
202+ } finally {
203+ try {
204+ removeCleanup ( cleanupConsolePatcher ) ;
205+ cleanupConsolePatcher ( ) ;
206+ } catch ( e : unknown ) {
207+ debugLogger . error ( 'Error cleaning up console patcher:' , e ) ;
208+ }
209+
210+ try {
211+ removeCleanup ( cleanupUnmount ) ;
212+ instance . unmount ( ) ;
213+ } catch ( e : unknown ) {
214+ debugLogger . error ( 'Error unmounting Ink instance:' , e ) ;
215+ }
216+
217+ try {
218+ removeCleanup ( cleanupTtyCheck ) ;
219+ cleanupTtyCheck ( ) ;
220+ } catch ( e : unknown ) {
221+ debugLogger . error ( 'Error in TTY cleanup:' , e ) ;
222+ }
223+
224+ if ( cleanupLineWrapping ) {
225+ try {
226+ removeCleanup ( cleanupLineWrapping ) ;
227+ cleanupLineWrapping ( ) ;
228+ } catch ( e : unknown ) {
229+ debugLogger . error ( 'Error restoring line wrapping:' , e ) ;
230+ }
231+ }
232+ }
191233}
192234
193235function setWindowTitle ( title : string , settings : LoadedSettings ) {
0 commit comments