minttea icon indicating copy to clipboard operation
minttea copied to clipboard

[feat] Graceful shutdown

Open chshersh opened this issue 1 year ago • 0 comments

When starting a Mint Tea app, I enter the alt screen and hide the cursor like this:

let init _model = Command.(Seq [ Hide_cursor; Enter_alt_screen ])

When exiting, I restore their behaviour:

  (* if we press `q` or the escape key, we exit *)
  | Event.KeyDown ((Key "q" | Escape), _modifier) ->
      ( { model with exit = true },
        Command.(Seq [ Exit_alt_screen; Show_cursor; Quit ]) )

However, if exit with Ctrl+C, then I'm still stuck in an alt screen with a hidden cursor.

I tried to exit gracefully myself by installing a signal handler like this:

let graceful_shutdown _sigint =
  Tty.Escape_seq.exit_alt_screen_seq ();
  Tty.Escape_seq.show_cursor_seq ();
  exit 0

let set_sigint_handler () =
  Sys.(set_signal sigint (Signal_handle graceful_shutdown))

let main () =
  set_sigint_handler ();

This handles the signal but for some reason breaks my terminal completely (can't type anything, can't run anything).

I assume that Mint Tea wants to do some other clean up that I'm missing, so it would be really nice if this clean up functionality was exposed or if Mint Tea provided a way to configure graceful shutdown handlers.

chshersh avatar Jun 24 '24 18:06 chshersh