From 7c06da9c0f3402efbc3954e9f14b1d039fd38929 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 12 Mar 2021 12:20:06 -0500 Subject: move the guards back onto the main objects --- examples/async.rs | 2 +- examples/basic.rs | 2 +- examples/input.rs | 4 ++-- examples/tmux.rs | 16 +++------------- src/blocking/input.rs | 13 +++++++++++-- src/blocking/output.rs | 18 +++++++++++++++--- src/input.rs | 12 ++++++++++-- src/output.rs | 13 +++++++++++-- 8 files changed, 54 insertions(+), 26 deletions(-) diff --git a/examples/async.rs b/examples/async.rs index 3ff4c92..5102aba 100644 --- a/examples/async.rs +++ b/examples/async.rs @@ -21,7 +21,7 @@ async fn run(tm: &mut textmode::Output) -> textmode::Result<()> { fn main() { smol::block_on(async { - let (mut tm, _guard) = textmode::Output::new().await.unwrap(); + let mut tm = textmode::Output::new().await.unwrap(); let e = run(&mut tm).await; e.unwrap(); }); diff --git a/examples/basic.rs b/examples/basic.rs index f3f846d..fb13d89 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,7 +1,7 @@ use textmode::Textmode as _; fn main() { - let (mut tm, _guard) = textmode::blocking::Output::new().unwrap(); + let mut tm = textmode::blocking::Output::new().unwrap(); tm.move_to(5, 5); tm.write_str("foo"); diff --git a/examples/input.rs b/examples/input.rs index fa9215f..a3b1f1d 100644 --- a/examples/input.rs +++ b/examples/input.rs @@ -1,6 +1,6 @@ #[cfg(feature = "async")] async fn async_main() { - let (mut input, _raw) = textmode::Input::new().await.unwrap(); + let mut input = textmode::Input::new().await.unwrap(); for arg in std::env::args().skip(1) { match arg.as_str() { "--disable-utf8" => input.parse_utf8(false), @@ -34,7 +34,7 @@ fn main() { #[cfg(not(feature = "async"))] fn main() { - let (mut input, _raw) = textmode::blocking::Input::new().unwrap(); + let mut input = textmode::blocking::Input::new().unwrap(); for arg in std::env::args().skip(1) { match arg.as_str() { "--disable-utf8" => input.parse_utf8(false), diff --git a/examples/tmux.rs b/examples/tmux.rs index 0ceb40b..62e52c2 100644 --- a/examples/tmux.rs +++ b/examples/tmux.rs @@ -293,32 +293,22 @@ impl State { #[must_use] struct Tmux { input: textmode::Input, - _raw: textmode::RawGuard, tm: textmode::Output, - _screen: textmode::ScreenGuard, state: State, } impl Tmux { async fn new() -> Self { - let (input, _raw) = textmode::Input::new().await.unwrap(); - let (tm, _screen) = textmode::Output::new().await.unwrap(); + let input = textmode::Input::new().await.unwrap(); + let tm = textmode::Output::new().await.unwrap(); let state = State::new(); - Self { - input, - _raw, - tm, - _screen, - state, - } + Self { input, tm, state } } async fn run(self, ex: &smol::Executor<'_>) { let Self { input, - _raw, mut tm, - _screen, mut state, } = self; diff --git a/src/blocking/input.rs b/src/blocking/input.rs index 1381776..b133e7f 100644 --- a/src/blocking/input.rs +++ b/src/blocking/input.rs @@ -50,6 +50,8 @@ impl Drop for RawGuard { } pub struct Input { + raw: Option, + buf: Vec, pos: usize, @@ -112,12 +114,15 @@ impl crate::private::Input for Input { #[allow(clippy::new_without_default)] impl Input { - pub fn new() -> Result<(Self, RawGuard)> { - Ok((Self::new_without_raw(), RawGuard::new()?)) + pub fn new() -> Result { + let mut self_ = Self::new_without_raw(); + self_.raw = Some(RawGuard::new()?); + Ok(self_) } pub fn new_without_raw() -> Self { Self { + raw: None, buf: Vec::with_capacity(4096), pos: 0, parse_utf8: true, @@ -148,6 +153,10 @@ impl Input { self.parse_single = parse; } + pub fn take_raw_guard(&mut self) -> Option { + self.raw.take() + } + pub fn read_key(&mut self) -> Result> { self.fill_buf()?; diff --git a/src/blocking/output.rs b/src/blocking/output.rs index 85faac1..83466f8 100644 --- a/src/blocking/output.rs +++ b/src/blocking/output.rs @@ -30,6 +30,8 @@ impl Drop for ScreenGuard { } pub struct Output { + screen: Option, + cur: vt100::Parser, next: vt100::Parser, } @@ -55,8 +57,10 @@ impl crate::private::Output for Output { impl crate::Textmode for Output {} impl Output { - pub fn new() -> Result<(Self, ScreenGuard)> { - Ok((Self::new_without_screen(), ScreenGuard::new()?)) + pub fn new() -> Result { + let mut self_ = Self::new_without_screen(); + self_.screen = Some(ScreenGuard::new()?); + Ok(self_) } pub fn new_without_screen() -> Self { @@ -69,7 +73,15 @@ impl Output { let cur = vt100::Parser::new(rows, cols, 0); let next = vt100::Parser::new(rows, cols, 0); - Self { cur, next } + Self { + screen: None, + cur, + next, + } + } + + pub fn take_screen_guard(&mut self) -> Option { + self.screen.take() } pub fn refresh(&mut self) -> Result<()> { diff --git a/src/input.rs b/src/input.rs index c881e44..e34187e 100644 --- a/src/input.rs +++ b/src/input.rs @@ -61,6 +61,7 @@ impl Drop for RawGuard { pub struct Input { stdin: blocking::Unblock, + raw: Option, buf: Vec, pos: usize, @@ -124,13 +125,16 @@ impl crate::private::Input for Input { #[allow(clippy::new_without_default)] impl Input { - pub async fn new() -> Result<(Self, RawGuard)> { - Ok((Self::new_without_raw(), RawGuard::new().await?)) + pub async fn new() -> Result { + let mut self_ = Self::new_without_raw(); + self_.raw = Some(RawGuard::new().await?); + Ok(self_) } pub fn new_without_raw() -> Self { Self { stdin: blocking::Unblock::new(std::io::stdin()), + raw: None, buf: Vec::with_capacity(4096), pos: 0, parse_utf8: true, @@ -161,6 +165,10 @@ impl Input { self.parse_single = parse; } + pub fn take_raw_guard(&mut self) -> Option { + self.raw.take() + } + pub async fn read_key(&mut self) -> Result> { self.fill_buf().await?; diff --git a/src/output.rs b/src/output.rs index 28411e1..1e0ed43 100644 --- a/src/output.rs +++ b/src/output.rs @@ -41,6 +41,8 @@ impl Drop for ScreenGuard { pub struct Output { stdout: blocking::Unblock, + screen: Option, + cur: vt100::Parser, next: vt100::Parser, } @@ -66,8 +68,10 @@ impl crate::private::Output for Output { impl crate::Textmode for Output {} impl Output { - pub async fn new() -> Result<(Self, ScreenGuard)> { - Ok((Self::new_without_screen(), ScreenGuard::new().await?)) + pub async fn new() -> Result { + let mut self_ = Self::new_without_screen(); + self_.screen = Some(ScreenGuard::new().await?); + Ok(self_) } pub fn new_without_screen() -> Self { @@ -81,11 +85,16 @@ impl Output { let next = vt100::Parser::new(rows, cols, 0); Self { stdout: blocking::Unblock::new(std::io::stdout()), + screen: None, cur, next, } } + pub fn take_screen_guard(&mut self) -> Option { + self.screen.take() + } + pub async fn refresh(&mut self) -> Result<()> { let diff = self.next().screen().state_diff(self.cur().screen()); write_stdout(&mut self.stdout, &diff).await?; -- cgit v1.2.3-54-g00ecf