From a64ff2d9f5e3ff26910ae7cebb857deeb11d1def Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 14 Dec 2021 16:50:12 -0500 Subject: these don't need to be separate errors --- CHANGELOG.md | 5 +++++ src/blocking/input.rs | 12 ++++++------ src/error.rs | 10 +++------- src/input.rs | 12 ++++++------ 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61a02ee..9ae1512 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ * `hide_cursor` to hide or show the cursor +### Changed + +* combined `Error::SetRaw` and `Error::UnsetRaw` into a single + `Error::SetTerminalMode` variant + ## [0.2.2] - 2021-12-06 ### Changed diff --git a/src/blocking/input.rs b/src/blocking/input.rs index 6d6e5a0..2d154c6 100644 --- a/src/blocking/input.rs +++ b/src/blocking/input.rs @@ -15,11 +15,11 @@ impl RawGuard { /// [`Input::new`](Input::new). /// /// # Errors - /// * `Error::SetRaw`: failed to put the terminal into raw mode + /// * `Error::SetTerminalMode`: failed to put the terminal into raw mode pub fn new() -> crate::error::Result { let stdin = std::io::stdin().as_raw_fd(); let termios = nix::sys::termios::tcgetattr(stdin) - .map_err(crate::error::Error::SetRaw)?; + .map_err(crate::error::Error::SetTerminalMode)?; let mut termios_raw = termios.clone(); nix::sys::termios::cfmakeraw(&mut termios_raw); nix::sys::termios::tcsetattr( @@ -27,7 +27,7 @@ impl RawGuard { nix::sys::termios::SetArg::TCSANOW, &termios_raw, ) - .map_err(crate::error::Error::SetRaw)?; + .map_err(crate::error::Error::SetTerminalMode)?; Ok(Self { termios: Some(termios), }) @@ -36,7 +36,7 @@ impl RawGuard { /// Switch back from raw mode early. /// /// # Errors - /// * `Error::UnsetRaw`: failed to return the terminal from raw mode + /// * `Error::SetTerminalMode`: failed to return the terminal from raw mode pub fn cleanup(&mut self) -> crate::error::Result<()> { self.termios.take().map_or(Ok(()), |termios| { let stdin = std::io::stdin().as_raw_fd(); @@ -45,7 +45,7 @@ impl RawGuard { nix::sys::termios::SetArg::TCSANOW, &termios, ) - .map_err(crate::error::Error::UnsetRaw) + .map_err(crate::error::Error::SetTerminalMode) }) } } @@ -133,7 +133,7 @@ impl Input { /// instance. /// /// # Errors - /// * `Error::SetRaw`: failed to put the terminal into raw mode + /// * `Error::SetTerminalMode`: failed to put the terminal into raw mode pub fn new() -> crate::error::Result { let mut self_ = Self::new_without_raw(); self_.raw = Some(RawGuard::new()?); diff --git a/src/error.rs b/src/error.rs index 27f5bd5..90c6786 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,13 +5,9 @@ pub enum Error { #[error("error reading from stdin")] ReadStdin(#[source] std::io::Error), - /// error enabling terminal raw mode - #[error("error enabling terminal raw mode")] - SetRaw(#[source] nix::Error), - - /// error restoring terminal from raw mode - #[error("error restoring terminal from raw mode")] - UnsetRaw(#[source] nix::Error), + /// error setting terminal mode + #[error("error setting terminal mode")] + SetTerminalMode(#[source] nix::Error), /// error writing to stdout #[error("error writing to stdout")] diff --git a/src/input.rs b/src/input.rs index f24f9c3..961fc51 100644 --- a/src/input.rs +++ b/src/input.rs @@ -15,12 +15,12 @@ impl RawGuard { /// [`Input::new`](Input::new). /// /// # Errors - /// * `Error::SetRaw`: failed to put the terminal into raw mode + /// * `Error::SetTerminalMode`: failed to put the terminal into raw mode pub async fn new() -> crate::error::Result { let stdin = std::io::stdin().as_raw_fd(); let termios = blocking::unblock(move || { nix::sys::termios::tcgetattr(stdin) - .map_err(crate::error::Error::SetRaw) + .map_err(crate::error::Error::SetTerminalMode) }) .await?; let mut termios_raw = termios.clone(); @@ -31,7 +31,7 @@ impl RawGuard { nix::sys::termios::SetArg::TCSANOW, &termios_raw, ) - .map_err(crate::error::Error::SetRaw) + .map_err(crate::error::Error::SetTerminalMode) }) .await?; Ok(Self { @@ -42,7 +42,7 @@ impl RawGuard { /// Switch back from raw mode early. /// /// # Errors - /// * `Error::UnsetRaw`: failed to return the terminal from raw mode + /// * `Error::SetTerminalMode`: failed to return the terminal from raw mode pub async fn cleanup(&mut self) -> crate::error::Result<()> { if let Some(termios) = self.termios.take() { let stdin = std::io::stdin().as_raw_fd(); @@ -52,7 +52,7 @@ impl RawGuard { nix::sys::termios::SetArg::TCSANOW, &termios, ) - .map_err(crate::error::Error::UnsetRaw) + .map_err(crate::error::Error::SetTerminalMode) }) .await } else { @@ -149,7 +149,7 @@ impl Input { /// instance. /// /// # Errors - /// * `Error::SetRaw`: failed to put the terminal into raw mode + /// * `Error::SetTerminalMode`: failed to put the terminal into raw mode pub async fn new() -> crate::error::Result { let mut self_ = Self::new_without_raw(); self_.raw = Some(RawGuard::new().await?); -- cgit v1.2.3-54-g00ecf