From 5ec96e5d9a321a654777449bcd597ebabfaa6ffa Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 7 Mar 2021 19:04:34 -0500 Subject: reorganize a bit --- src/output.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/output.rs (limited to 'src/output.rs') diff --git a/src/output.rs b/src/output.rs new file mode 100644 index 0000000..9e8c220 --- /dev/null +++ b/src/output.rs @@ -0,0 +1,94 @@ +use futures_lite::io::AsyncWriteExt as _; + +use super::private::TextmodeImpl as _; + +pub struct ScreenGuard { + cleaned_up: bool, +} + +impl ScreenGuard { + pub async fn cleanup(&mut self) -> std::io::Result<()> { + if self.cleaned_up { + return Ok(()); + } + self.cleaned_up = true; + write_stdout(super::DEINIT).await + } +} + +impl Drop for ScreenGuard { + fn drop(&mut self) { + futures_lite::future::block_on(async { + let _ = self.cleanup().await; + }); + } +} + +pub struct Output { + cur: vt100::Parser, + next: vt100::Parser, +} + +impl super::private::TextmodeImpl for Output { + fn cur(&self) -> &vt100::Parser { + &self.cur + } + + fn cur_mut(&mut self) -> &mut vt100::Parser { + &mut self.cur + } + + fn next(&self) -> &vt100::Parser { + &self.next + } + + fn next_mut(&mut self) -> &mut vt100::Parser { + &mut self.next + } +} + +impl super::Textmode for Output {} + +impl Output { + pub async fn new() -> std::io::Result<(Self, ScreenGuard)> { + write_stdout(super::INIT).await?; + + Ok(( + Self::new_without_screen(), + ScreenGuard { cleaned_up: false }, + )) + } + + pub fn new_without_screen() -> Self { + let (rows, cols) = match terminal_size::terminal_size() { + Some((terminal_size::Width(w), terminal_size::Height(h))) => { + (h, w) + } + _ => (24, 80), + }; + let cur = vt100::Parser::new(rows, cols, 0); + let next = vt100::Parser::new(rows, cols, 0); + Self { cur, next } + } + + pub async fn refresh(&mut self) -> std::io::Result<()> { + let diffs = &[ + self.next().screen().contents_diff(self.cur().screen()), + self.next().screen().input_mode_diff(self.cur().screen()), + self.next().screen().title_diff(self.cur().screen()), + self.next().screen().bells_diff(self.cur().screen()), + ]; + for diff in diffs { + write_stdout(&diff).await?; + self.cur_mut().process(&diff); + } + Ok(()) + } +} + +async fn write_stdout(buf: &[u8]) -> std::io::Result<()> { + let mut stdout = blocking::Unblock::new(std::io::stdout()); + stdout.write_all(buf).await?; + stdout.flush().await?; + Ok(()) +} -- cgit v1.2.3-54-g00ecf