on master help sign in
Share the restore_term code between panic and normal exits (#3612)

It was starting to diverge as the normal exit code was restoring the prompt but the panic code wasn't, and the panic code was disabling bracketed paste but the normal code wasn't. This changes the panic path slightly in that we won't disable raw mode if exiting alternate screen and disabling bracketed paste fails. If that happens, things are so busted I don't think it matters anyway.

commit5c3f43a7f0c5392242424912bde992d9305c0c8e authorCharlie Groves <c@sevorg.org> dateWed 31 Aug 2022, 16:23 (4 years ago) parent93c6a33
1 file changed+20−22
helix-term/src/application.rs+20−22
helix-term/src/application.rs+20−22
@@ -85,6 +85,22 @@ fn setup_integration_logging() {
8585 .apply();
8686}
8787
88+fn restore_term() -> Result<(), Error> {
89+ let mut stdout = stdout();
90+ // reset cursor shape
91+ write!(stdout, "\x1B[0 q")?;
92+ // Ignore errors on disabling, this might trigger on windows if we call
93+ // disable without calling enable previously
94+ let _ = execute!(stdout, DisableMouseCapture);
95+ execute!(
96+ stdout,
97+ DisableBracketedPaste,
98+ terminal::LeaveAlternateScreen
99+ )?;
100+ terminal::disable_raw_mode()?;
101+ Ok(())
102+}
103+
88104impl Application {
89105 pub fn new(args: Args, config: Config) -> Result<Self, Error> {
90106 #[cfg(feature = "integration")]
@@ -389,7 +405,7 @@ impl Application {
389405 match signal {
390406 signal::SIGTSTP => {
391407 self.compositor.save_cursor();
392− self.restore_term().unwrap();
408+ restore_term().unwrap();
393409 low_level::emulate_default_handler(signal::SIGTSTP).unwrap();
394410 }
395411 signal::SIGCONT => {
@@ -803,18 +819,6 @@ impl Application {
803819 Ok(())
804820 }
805821
806− fn restore_term(&mut self) -> Result<(), Error> {
807− let mut stdout = stdout();
808− // reset cursor shape
809− write!(stdout, "\x1B[0 q")?;
810− // Ignore errors on disabling, this might trigger on windows if we call
811− // disable without calling enable previously
812− let _ = execute!(stdout, DisableMouseCapture);
813− execute!(stdout, terminal::LeaveAlternateScreen)?;
814− terminal::disable_raw_mode()?;
815− Ok(())
816− }
817−
818822 pub async fn run<S>(&mut self, input_stream: &mut S) -> Result<i32, Error>
819823 where
820824 S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
@@ -826,20 +830,14 @@ impl Application {
826830 std::panic::set_hook(Box::new(move |info| {
827831 // We can't handle errors properly inside this closure. And it's
828832 // probably not a good idea to `unwrap()` inside a panic handler.
829− // So we just ignore the `Result`s.
830− let _ = execute!(std::io::stdout(), DisableMouseCapture);
831− let _ = execute!(
832− std::io::stdout(),
833− terminal::LeaveAlternateScreen,
834− DisableBracketedPaste
835− );
836− let _ = terminal::disable_raw_mode();
833+ // So we just ignore the `Result`.
834+ let _ = restore_term();
837835 hook(info);
838836 }));
839837
840838 self.event_loop(input_stream).await;
841839 self.close().await?;
842− self.restore_term()?;
840+ restore_term()?;
843841
844842 Ok(self.editor.exit_code)
845843 }