1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-26 23:04:49 +03:00

term: hook flush up to ThreadedWriter

I'd forgotten that this was in here.
The recentish fixes to flush for various terminal queries
likely need this to be 100% good.  Surprised this hasn't
come up as a bug already :-/
This commit is contained in:
Wez Furlong 2022-08-12 07:51:12 -07:00
parent 4e0c7771bb
commit 259210dc2d

View File

@ -436,17 +436,31 @@ fn default_color_map() -> HashMap<u16, RgbColor> {
/// and we're in control of the write side, which represents
/// input from the interactive user, or pastes.
struct ThreadedWriter {
sender: Sender<Vec<u8>>,
sender: Sender<WriterMessage>,
}
enum WriterMessage {
Data(Vec<u8>),
Flush,
}
impl ThreadedWriter {
fn new(mut writer: Box<dyn std::io::Write + Send>) -> Self {
let (sender, receiver) = channel::<Vec<u8>>();
let (sender, receiver) = channel::<WriterMessage>();
std::thread::spawn(move || {
while let Ok(buf) = receiver.recv() {
if writer.write(&buf).is_err() {
break;
while let Ok(msg) = receiver.recv() {
match msg {
WriterMessage::Data(buf) => {
if writer.write(&buf).is_err() {
break;
}
}
WriterMessage::Flush => {
if writer.flush().is_err() {
break;
}
}
}
}
});
@ -458,11 +472,15 @@ impl ThreadedWriter {
impl std::io::Write for ThreadedWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.sender
.send(buf.to_vec())
.send(WriterMessage::Data(buf.to_vec()))
.map_err(|err| std::io::Error::new(std::io::ErrorKind::BrokenPipe, err))?;
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
self.sender
.send(WriterMessage::Flush)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::BrokenPipe, err))?;
Ok(())
}
}