From 2071214d70206e0dad43222c350ce3cd1ca31192 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 11 Mar 2021 14:34:29 -0500 Subject: 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? --- src/output.rs | 27 +++++++++++++++++++++------ 1 file 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 { - 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, 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, + buf: &[u8], +) -> Result<()> { stdout.write_all(buf).await.map_err(Error::WriteStdout)?; stdout.flush().await.map_err(Error::WriteStdout)?; Ok(()) -- cgit v1.2.3-54-g00ecf