diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-03-11 14:34:29 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-03-11 14:34:29 -0500 |
commit | 2071214d70206e0dad43222c350ce3cd1ca31192 (patch) | |
tree | 02cb773a8fa7ee10b2960a430d26b58e61f6ad3c | |
parent | 28957d6256ac5d568eaed2555d568b5b7c047231 (diff) | |
download | textmode-2071214d70206e0dad43222c350ce3cd1ca31192.tar.gz textmode-2071214d70206e0dad43222c350ce3cd1ca31192.zip |
also avoid creating Unblocks for stdout
this doesn't cause actual problems, but i think recreating Unblocks over
and over is likely a lot less efficient due to spawning/scheduling
threads?
-rw-r--r-- | src/output.rs | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/src/output.rs b/src/output.rs index b3a9a2a..63c0b08 100644 --- a/src/output.rs +++ b/src/output.rs @@ -10,7 +10,11 @@ pub struct ScreenGuard { impl ScreenGuard { pub async fn new() -> Result<Self> { - write_stdout(crate::INIT).await?; + write_stdout( + &mut blocking::Unblock::new(std::io::stdout()), + crate::INIT, + ) + .await?; Ok(Self { cleaned_up: false }) } @@ -19,7 +23,11 @@ impl ScreenGuard { return Ok(()); } self.cleaned_up = true; - write_stdout(crate::DEINIT).await + write_stdout( + &mut blocking::Unblock::new(std::io::stdout()), + crate::DEINIT, + ) + .await } } @@ -32,6 +40,7 @@ impl Drop for ScreenGuard { } pub struct Output { + stdout: blocking::Unblock<std::io::Stdout>, cur: vt100::Parser, next: vt100::Parser, } @@ -70,19 +79,25 @@ impl Output { }; let cur = vt100::Parser::new(rows, cols, 0); let next = vt100::Parser::new(rows, cols, 0); - Self { cur, next } + Self { + stdout: blocking::Unblock::new(std::io::stdout()), + cur, + next, + } } pub async fn refresh(&mut self) -> Result<()> { let diff = self.next().screen().state_diff(self.cur().screen()); - write_stdout(&diff).await?; + write_stdout(&mut self.stdout, &diff).await?; self.cur_mut().process(&diff); Ok(()) } } -async fn write_stdout(buf: &[u8]) -> Result<()> { - let mut stdout = blocking::Unblock::new(std::io::stdout()); +async fn write_stdout( + stdout: &mut blocking::Unblock<std::io::Stdout>, + buf: &[u8], +) -> Result<()> { stdout.write_all(buf).await.map_err(Error::WriteStdout)?; stdout.flush().await.map_err(Error::WriteStdout)?; Ok(()) |