Remove Terminal::reset_mode

This was a footgun since it isn't an opposite to `enter_raw_mode`. We
want to reset the mode in Helix when suspending but that only works on
Unix and on Unix it's the same thing to use `enter_cooked_mode`. On
Windows during Drop we want to reset the code pages (encoding) as well
but the code pages are not set in `enter_raw_mode`, they're set during
`WindowsTerminal::new`. This change removes `reset_mode` and moves its
code into `Drop for WindowsTerminal`.
This commit is contained in:
Michael Davis
2025-04-10 12:19:30 -04:00
parent e0d38d7449
commit 32f414c9e1
3 changed files with 5 additions and 20 deletions

View File

@@ -43,8 +43,6 @@ pub trait Terminal: io::Write {
/// While in "cooked" mode a terminal will interpret the incoming data in ways that are useful
/// such as waiting for an Enter key press to pass input to the application.
fn enter_cooked_mode(&mut self) -> io::Result<()>;
/// Resets the mode to what was detected in `PlatformTerminal::new`.
fn reset_mode(&mut self) -> io::Result<()>;
fn get_dimensions(&self) -> io::Result<(u16, u16)>;
fn event_stream<F: Fn(&Event) -> bool + Clone + Send + Sync + 'static>(
&self,

View File

@@ -140,11 +140,6 @@ impl Terminal for UnixTerminal {
Ok(())
}
fn reset_mode(&mut self) -> io::Result<()> {
// NOTE: this is the same as entering cooked mode on Unix but involves more on Windows.
self.enter_cooked_mode()
}
fn get_dimensions(&self) -> io::Result<(u16, u16)> {
let winsize = termios::tcgetwinsize(self.write.get_ref())?;
Ok((winsize.ws_row, winsize.ws_col))
@@ -173,7 +168,7 @@ impl Terminal for UnixTerminal {
impl Drop for UnixTerminal {
fn drop(&mut self) {
let _ = self.flush();
let _ = self.reset_mode();
let _ = self.enter_cooked_mode();
}
}

View File

@@ -355,17 +355,6 @@ impl Terminal for WindowsTerminal {
Ok(())
}
fn reset_mode(&mut self) -> io::Result<()> {
self.output.flush()?;
self.input.set_mode(self.original_input_mode)?;
self.output.get_mut().set_mode(self.original_output_mode)?;
self.input.set_code_page(self.original_input_cp)?;
self.output
.get_mut()
.set_code_page(self.original_output_cp)?;
Ok(())
}
fn get_dimensions(&self) -> io::Result<(u16, u16)> {
// NOTE: setting dimensions should be done by VT instead of `SetConsoleScreenBufferInfo`.
// <https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#window-width>
@@ -395,7 +384,10 @@ impl Terminal for WindowsTerminal {
impl Drop for WindowsTerminal {
fn drop(&mut self) {
let _ = self.flush();
let _ = self.reset_mode();
let _ = self.input.set_code_page(self.original_input_cp);
let _ = self.output.get_mut().set_code_page(self.original_output_cp);
let _ = self.input.set_mode(self.original_input_mode);
let _ = self.output.get_mut().set_mode(self.original_output_mode);
}
}