diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-14 16:46:19 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-14 16:46:19 -0500 |
commit | bead64f9498f2787fb1d5aa247d79536f2424528 (patch) | |
tree | 6827737a841cc7e3c582e9b69e5b1dbe5fc91fa5 /src/blocking | |
parent | 89206651ffb9815aca7f2b010722fe673a25b2ac (diff) | |
download | textmode-bead64f9498f2787fb1d5aa247d79536f2424528.tar.gz textmode-bead64f9498f2787fb1d5aa247d79536f2424528.zip |
clippy
Diffstat (limited to 'src/blocking')
-rw-r--r-- | src/blocking/input.rs | 56 | ||||
-rw-r--r-- | src/blocking/output.rs | 38 |
2 files changed, 63 insertions, 31 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) } diff --git a/src/blocking/output.rs b/src/blocking/output.rs index cf24e3d..74c0590 100644 --- a/src/blocking/output.rs +++ b/src/blocking/output.rs @@ -1,5 +1,3 @@ -use crate::error::*; - use std::io::Write as _; use crate::private::Output as _; @@ -14,13 +12,19 @@ impl ScreenGuard { /// Switches the terminal on `stdout` to alternate screen mode and returns /// a guard object. This is typically called as part of /// [`Output::new`](Output::new). - pub fn new() -> Result<Self> { + /// + /// # Errors + /// * `Error::WriteStdout`: failed to write initialization to stdout + pub fn new() -> crate::error::Result<Self> { write_stdout(crate::INIT)?; Ok(Self { cleaned_up: false }) } /// Switch back from alternate screen mode early. - pub fn cleanup(&mut self) -> Result<()> { + /// + /// # Errors + /// * `Error::WriteStdout`: failed to write deinitialization to stdout + pub fn cleanup(&mut self) -> crate::error::Result<()> { if self.cleaned_up { return Ok(()); } @@ -32,6 +36,8 @@ impl ScreenGuard { impl Drop for ScreenGuard { /// Calls `cleanup`. fn drop(&mut self) { + // https://github.com/rust-lang/rust-clippy/issues/8003 + #[allow(clippy::let_underscore_drop)] let _ = self.cleanup(); } } @@ -72,7 +78,10 @@ impl crate::Textmode for Output {} impl Output { /// Creates a new `Output` instance containing a /// [`ScreenGuard`](ScreenGuard) instance. - pub fn new() -> Result<Self> { + /// + /// # Errors + /// * `Error::WriteStdout`: failed to write initialization to stdout + pub fn new() -> crate::error::Result<Self> { let mut self_ = Self::new_without_screen(); self_.screen = Some(ScreenGuard::new()?); Ok(self_) @@ -80,6 +89,7 @@ impl Output { /// Creates a new `Output` instance without creating a /// [`ScreenGuard`](ScreenGuard) instance. + #[must_use] pub fn new_without_screen() -> Self { let (rows, cols) = match terminal_size::terminal_size() { Some((terminal_size::Width(w), terminal_size::Height(h))) => { @@ -108,7 +118,10 @@ impl Output { /// Draws the in-memory screen to the terminal on `stdout`. This is done /// using a diff mechanism to only update the parts of the terminal which /// are different from the in-memory screen. - pub fn refresh(&mut self) -> Result<()> { + /// + /// # Errors + /// * `Error::WriteStdout`: failed to write screen state to stdout + pub fn refresh(&mut self) -> crate::error::Result<()> { let diff = self.next().screen().state_diff(self.cur().screen()); write_stdout(&diff)?; self.cur_mut().process(&diff); @@ -120,7 +133,10 @@ impl Output { /// mechanism like `refresh`. This can be useful when the current state of /// the terminal screen is unknown, such as after the terminal has been /// resized. - pub async fn hard_refresh(&mut self) -> Result<()> { + /// + /// # Errors + /// * `Error::WriteStdout`: failed to write screen state to stdout + pub async fn hard_refresh(&mut self) -> crate::error::Result<()> { let contents = self.next().screen().state_formatted(); write_stdout(&contents)?; self.cur_mut().process(&contents); @@ -128,9 +144,11 @@ impl Output { } } -fn write_stdout(buf: &[u8]) -> Result<()> { +fn write_stdout(buf: &[u8]) -> crate::error::Result<()> { let mut stdout = std::io::stdout(); - stdout.write_all(buf).map_err(Error::WriteStdout)?; - stdout.flush().map_err(Error::WriteStdout)?; + stdout + .write_all(buf) + .map_err(crate::error::Error::WriteStdout)?; + stdout.flush().map_err(crate::error::Error::WriteStdout)?; Ok(()) } |