diff options
Diffstat (limited to 'src/blocking/input.rs')
-rw-r--r-- | src/blocking/input.rs | 56 |
1 files changed, 35 insertions, 21 deletions
diff --git a/src/blocking/input.rs b/src/blocking/input.rs index dcfb3ba..6d6e5a0 100644 --- a/src/blocking/input.rs +++ b/src/blocking/input.rs @@ -1,5 +1,3 @@ -use crate::error::*; - use std::io::Read as _; use std::os::unix::io::AsRawFd as _; @@ -15,10 +13,13 @@ impl RawGuard { /// Switches the terminal on `stdin` to raw mode and returns a guard /// object. This is typically called as part of /// [`Input::new`](Input::new). - pub fn new() -> Result<Self> { + /// + /// # Errors + /// * `Error::SetRaw`: failed to put the terminal into raw mode + pub fn new() -> crate::error::Result<Self> { let stdin = std::io::stdin().as_raw_fd(); - let termios = - nix::sys::termios::tcgetattr(stdin).map_err(Error::SetRaw)?; + let termios = nix::sys::termios::tcgetattr(stdin) + .map_err(crate::error::Error::SetRaw)?; let mut termios_raw = termios.clone(); nix::sys::termios::cfmakeraw(&mut termios_raw); nix::sys::termios::tcsetattr( @@ -26,31 +27,34 @@ impl RawGuard { nix::sys::termios::SetArg::TCSANOW, &termios_raw, ) - .map_err(Error::SetRaw)?; + .map_err(crate::error::Error::SetRaw)?; Ok(Self { termios: Some(termios), }) } /// Switch back from raw mode early. - pub fn cleanup(&mut self) -> Result<()> { - if let Some(termios) = self.termios.take() { + /// + /// # Errors + /// * `Error::UnsetRaw`: 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(); nix::sys::termios::tcsetattr( stdin, nix::sys::termios::SetArg::TCSANOW, &termios, ) - .map_err(Error::UnsetRaw) - } else { - Ok(()) - } + .map_err(crate::error::Error::UnsetRaw) + }) } } impl Drop for RawGuard { /// Calls `cleanup`. fn drop(&mut self) { + // https://github.com/rust-lang/rust-clippy/issues/8003 + #[allow(clippy::let_underscore_drop)] let _ = self.cleanup(); } } @@ -76,11 +80,11 @@ pub struct Input { impl crate::private::Input for Input { fn buf(&self) -> &[u8] { - &self.buf[self.pos..] + self.buf.get(self.pos..).unwrap() } fn buf_mut(&mut self) -> &mut [u8] { - &mut self.buf[self.pos..] + self.buf.get_mut(self.pos..).unwrap() } fn buf_mut_vec(&mut self) -> &mut Vec<u8> { @@ -127,7 +131,10 @@ impl crate::private::Input for Input { impl Input { /// Creates a new `Input` instance containing a [`RawGuard`](RawGuard) /// instance. - pub fn new() -> Result<Self> { + /// + /// # Errors + /// * `Error::SetRaw`: failed to put the terminal into raw mode + pub fn new() -> crate::error::Result<Self> { let mut self_ = Self::new_without_raw(); self_.raw = Some(RawGuard::new()?); Ok(self_) @@ -135,6 +142,7 @@ impl Input { /// Creates a new `Input` instance without creating a /// [`RawGuard`](RawGuard) instance. + #[must_use] pub fn new_without_raw() -> Self { Self { raw: None, @@ -203,7 +211,10 @@ impl Input { /// Reads a keypress from the terminal on `stdin`. Returns `Ok(None)` on /// EOF. - pub fn read_key(&mut self) -> Result<Option<crate::Key>> { + /// + /// # Errors + /// * `Error::ReadStdin`: failed to read data from stdin + pub fn read_key(&mut self) -> crate::error::Result<Option<crate::Key>> { self.fill_buf()?; if self.parse_single { @@ -225,7 +236,7 @@ impl Input { } } - fn fill_buf(&mut self) -> Result<()> { + fn fill_buf(&mut self) -> crate::error::Result<()> { if self.buf_is_empty() { self.buf.resize(4096, 0); self.pos = 0; @@ -237,12 +248,13 @@ impl Input { } if self.parse_utf8 { - let expected_bytes = self.expected_leading_utf8_bytes(); + let expected_bytes = + self.expected_leading_utf8_bytes(*self.buf().get(0).unwrap()); if self.buf.len() < self.pos + expected_bytes { let mut cur = self.buf.len(); self.buf.resize(4096 + expected_bytes, 0); while cur < self.pos + expected_bytes { - let bytes = read_stdin(&mut self.buf[cur..])?; + let bytes = read_stdin(self.buf.get_mut(cur..).unwrap())?; if bytes == 0 { return Ok(()); } @@ -256,6 +268,8 @@ impl Input { } } -fn read_stdin(buf: &mut [u8]) -> Result<usize> { - std::io::stdin().read(buf).map_err(Error::ReadStdin) +fn read_stdin(buf: &mut [u8]) -> crate::error::Result<usize> { + std::io::stdin() + .read(buf) + .map_err(crate::error::Error::ReadStdin) } |