aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-03-12 11:46:14 -0500
committerJesse Luehrs <doy@tozt.net>2021-03-12 11:46:14 -0500
commitf3498d0afe3bd36cf3e9f553776518fd458a39af (patch)
treed6c7f15805eaa3c7813310b0e96797013338b5b7
parent4ead14a3153b34941570e87d67064e4aa93fa01a (diff)
downloadtextmode-f3498d0afe3bd36cf3e9f553776518fd458a39af.tar.gz
textmode-f3498d0afe3bd36cf3e9f553776518fd458a39af.zip
simplify
-rw-r--r--src/blocking/input.rs26
-rw-r--r--src/input.rs33
2 files changed, 27 insertions, 32 deletions
diff --git a/src/blocking/input.rs b/src/blocking/input.rs
index ea34536..1381776 100644
--- a/src/blocking/input.rs
+++ b/src/blocking/input.rs
@@ -6,8 +6,7 @@ use std::os::unix::io::AsRawFd as _;
use crate::private::Input as _;
pub struct RawGuard {
- termios: nix::sys::termios::Termios,
- cleaned_up: bool,
+ termios: Option<nix::sys::termios::Termios>,
}
impl RawGuard {
@@ -25,23 +24,22 @@ impl RawGuard {
)
.map_err(Error::SetRaw)?;
Ok(Self {
- termios,
- cleaned_up: false,
+ termios: Some(termios),
})
}
pub fn cleanup(&mut self) -> Result<()> {
- if self.cleaned_up {
- return Ok(());
+ if let Some(termios) = self.termios.take() {
+ let stdin = std::io::stdin().as_raw_fd();
+ nix::sys::termios::tcsetattr(
+ stdin,
+ nix::sys::termios::SetArg::TCSANOW,
+ &termios,
+ )
+ .map_err(Error::UnsetRaw)
+ } else {
+ Ok(())
}
- self.cleaned_up = true;
- let stdin = std::io::stdin().as_raw_fd();
- nix::sys::termios::tcsetattr(
- stdin,
- nix::sys::termios::SetArg::TCSANOW,
- &self.termios,
- )
- .map_err(Error::UnsetRaw)
}
}
diff --git a/src/input.rs b/src/input.rs
index 7568de2..c881e44 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -6,8 +6,7 @@ use std::os::unix::io::AsRawFd as _;
use crate::private::Input as _;
pub struct RawGuard {
- termios: nix::sys::termios::Termios,
- cleaned_up: bool,
+ termios: Option<nix::sys::termios::Termios>,
}
impl RawGuard {
@@ -30,27 +29,25 @@ impl RawGuard {
})
.await?;
Ok(Self {
- termios,
- cleaned_up: false,
+ termios: Some(termios),
})
}
pub async fn cleanup(&mut self) -> Result<()> {
- if self.cleaned_up {
- return Ok(());
+ if let Some(termios) = self.termios.take() {
+ let stdin = std::io::stdin().as_raw_fd();
+ blocking::unblock(move || {
+ nix::sys::termios::tcsetattr(
+ stdin,
+ nix::sys::termios::SetArg::TCSANOW,
+ &termios,
+ )
+ .map_err(Error::UnsetRaw)
+ })
+ .await
+ } else {
+ Ok(())
}
- self.cleaned_up = true;
- let stdin = std::io::stdin().as_raw_fd();
- let termios = self.termios.clone();
- blocking::unblock(move || {
- nix::sys::termios::tcsetattr(
- stdin,
- nix::sys::termios::SetArg::TCSANOW,
- &termios,
- )
- .map_err(Error::UnsetRaw)
- })
- .await
}
}