on master help sign in
Share the restore_term code between panic and normal exits (#3612)
5c3f43aCharlie Groves
helix-term/src/application.rs+20−22
@@ -85,6 +85,22 @@ fn setup_integration_logging() {
85 .apply();
85 .apply();
86}
86}
87
87
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+
88impl Application {
104impl Application {
89 pub fn new(args: Args, config: Config) -> Result<Self, Error> {
105 pub fn new(args: Args, config: Config) -> Result<Self, Error> {
90 #[cfg(feature = "integration")]
106 #[cfg(feature = "integration")]
@@ -389,7 +405,7 @@ impl Application {
389 match signal {
405 match signal {
390 signal::SIGTSTP => {
406 signal::SIGTSTP => {
391 self.compositor.save_cursor();
407 self.compositor.save_cursor();
392− self.restore_term().unwrap();
408+ restore_term().unwrap();
393 low_level::emulate_default_handler(signal::SIGTSTP).unwrap();
409 low_level::emulate_default_handler(signal::SIGTSTP).unwrap();
394 }
410 }
395 signal::SIGCONT => {
411 signal::SIGCONT => {
@@ -803,18 +819,6 @@ impl Application {
803 Ok(())
819 Ok(())
804 }
820 }
805
821
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−
818 pub async fn run<S>(&mut self, input_stream: &mut S) -> Result<i32, Error>
822 pub async fn run<S>(&mut self, input_stream: &mut S) -> Result<i32, Error>
819 where
823 where
820 S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
824 S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
@@ -826,20 +830,14 @@ impl Application {
826 std::panic::set_hook(Box::new(move |info| {
830 std::panic::set_hook(Box::new(move |info| {
827 // We can't handle errors properly inside this closure. And it's
831 // We can't handle errors properly inside this closure. And it's
828 // probably not a good idea to `unwrap()` inside a panic handler.
832 // probably not a good idea to `unwrap()` inside a panic handler.
829− // So we just ignore the `Result`s.
833+ // So we just ignore the `Result`.
830− let _ = execute!(std::io::stdout(), DisableMouseCapture);
834+ let _ = restore_term();
831− let _ = execute!(
832− std::io::stdout(),
833− terminal::LeaveAlternateScreen,
834− DisableBracketedPaste
835− );
836− let _ = terminal::disable_raw_mode();
837 hook(info);
835 hook(info);
838 }));
836 }));
839
837
840 self.event_loop(input_stream).await;
838 self.event_loop(input_stream).await;
841 self.close().await?;
839 self.close().await?;
842− self.restore_term()?;
840+ restore_term()?;
843
841
844 Ok(self.editor.exit_code)
842 Ok(self.editor.exit_code)
845 }
843 }