Add back Drop glue for terminal types

It's somewhat catastrophic to not reset console mode and code pages on
Windows - it seems to really mess up the terminal and can cause a crash
- so I think it's fine to reset mode automatically on drop. While we're
at it we might as well flush the writer buffers.
This commit is contained in:
Michael Davis
2025-04-08 09:45:05 -04:00
parent 6c129f5b45
commit cac0aea6c6
4 changed files with 17 additions and 10 deletions

View File

@@ -70,7 +70,6 @@ fn main() -> io::Result<()> {
other => eprintln!("unexpected event: {other:?}\r"),
}
}
terminal.reset_mode()?;
println!("Detected features: {features:?}");
Ok(())

View File

@@ -124,8 +124,6 @@ fn main() -> io::Result<()> {
decreset!(SGRMouse),
)?;
terminal.reset_mode()?;
Ok(())
}

View File

@@ -1,7 +1,7 @@
use rustix::termios::{self, Termios};
use std::{
fs,
io::{self, BufWriter, IsTerminal as _},
io::{self, BufWriter, IsTerminal as _, Write as _},
os::unix::prelude::*,
};
@@ -141,12 +141,8 @@ impl Terminal for UnixTerminal {
}
fn reset_mode(&mut self) -> io::Result<()> {
termios::tcsetattr(
self.write.get_ref(),
termios::OptionalActions::Now,
&self.original_termios,
)?;
Ok(())
// 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)> {
@@ -174,6 +170,13 @@ impl Terminal for UnixTerminal {
}
}
impl Drop for UnixTerminal {
fn drop(&mut self) {
let _ = self.flush();
let _ = self.reset_mode();
}
}
impl io::Write for UnixTerminal {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.write.write(buf)

View File

@@ -392,6 +392,13 @@ impl Terminal for WindowsTerminal {
}
}
impl Drop for WindowsTerminal {
fn drop(&mut self) {
let _ = self.flush();
let _ = self.reset_mode();
}
}
impl io::Write for WindowsTerminal {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.output.write(buf)