From 458b7b7bd2f49328f49fb05a9a544ba91255cbbc Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Sun, 16 Jun 2024 16:00:45 -0700 Subject: [PATCH] fix: formatting and clippy --- Cargo.toml | 1 + examples/colors_rgb.rs | 1 - src/backend/crossterm.rs | 38 ++++++++++++++------------------------ 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5ea6c429..44c0bd73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ unicode-width = "0.1.13" anyhow = "1.0.71" argh = "0.1.12" better-panic = "0.3.0" +color-eyre = "0.6.2" criterion = { version = "0.5.1", features = ["html_reports"] } derive_builder = "0.20.0" fakeit = "1.1" diff --git a/examples/colors_rgb.rs b/examples/colors_rgb.rs index 60ce8e06..38dca4d1 100644 --- a/examples/colors_rgb.rs +++ b/examples/colors_rgb.rs @@ -92,7 +92,6 @@ struct ColorsWidget { } fn main() -> Result<()> { - install_error_hooks()?; let terminal = CrosstermBackend::stdout_with_defaults()?.to_terminal()?; App::default().run(terminal)?; Ok(()) diff --git a/src/backend/crossterm.rs b/src/backend/crossterm.rs index 0daeba61..f37c623c 100644 --- a/src/backend/crossterm.rs +++ b/src/backend/crossterm.rs @@ -2,7 +2,7 @@ //! the [Crossterm] crate to interact with the terminal. //! //! [Crossterm]: https://crates.io/crates/crossterm -use std::io::{self, Write}; +use std::io; #[cfg(feature = "underline-color")] use crate::crossterm::style::SetUnderlineColor; @@ -88,7 +88,7 @@ use crate::{ /// [Examples]: https://github.com/ratatui-org/ratatui/tree/main/examples/README.md #[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] #[allow(clippy::struct_excessive_bools)] -pub struct CrosstermBackend { +pub struct CrosstermBackend { /// The writer used to send commands to the terminal. writer: W, restore_raw_mode_on_drop: bool, @@ -99,10 +99,7 @@ pub struct CrosstermBackend { restore_keyboard_enhancement_flags_on_drop: bool, } -impl CrosstermBackend -where - W: Write, -{ +impl CrosstermBackend { /// Creates a new `CrosstermBackend` with the given writer. /// /// Applications will typically use [`CrosstermBackend::stdout`] or [`CrosstermBackend::stderr`] @@ -219,7 +216,7 @@ impl CrosstermBackend { } } -impl CrosstermBackend { +impl CrosstermBackend { /// Enables default settings for the terminal backend. /// /// This enables raw mode and switches to the alternate screen. Mouse support is not enabled. @@ -238,12 +235,12 @@ impl CrosstermBackend { /// let backend = CrosstermBackend::stdout().with_defaults()?; /// # std::io::Result::Ok(()) /// ``` - pub fn with_defaults(mut self) -> io::Result { + pub fn with_defaults(self) -> io::Result { let backend = self.with_raw_mode()?.with_alternate_screen()?; #[cfg(feature = "color-eyre")] let backend = backend.with_color_eyre_hooks()?; #[cfg(not(feature = "color-eyre"))] - let backend = backend.with_panic_hook()?; + let backend = backend.with_panic_hook(); Ok(backend) } @@ -376,7 +373,8 @@ impl CrosstermBackend { /// let backend = CrosstermBackend::stdout().with_panic_hook()?; /// ``` #[cfg(not(feature = "color-eyre"))] - pub fn with_panic_hook(self) -> io::Result { + #[must_use] + pub fn with_panic_hook(self) -> Self { use std::panic; let hook = panic::take_hook(); @@ -384,7 +382,7 @@ impl CrosstermBackend { let _ = CrosstermBackend::reset(io::stderr()); hook(info); })); - Ok(self) + self } /// Installs the color-eyre panic and error report hooks. @@ -397,6 +395,7 @@ impl CrosstermBackend { /// use ratatui::backend::CrosstermBackend; /// /// let backend = CrosstermBackend::stdout().with_color_eyre_hooks()?; + /// # std::io::Result::Ok(()) /// ``` #[cfg(feature = "color-eyre")] pub fn with_color_eyre_hooks(self) -> io::Result { @@ -455,7 +454,7 @@ impl CrosstermBackend { } } -impl Drop for CrosstermBackend { +impl Drop for CrosstermBackend { fn drop(&mut self) { // note that these are not checked for errors because there is nothing that can be done if // they fail. The terminal is likely in a bad state, and the application is exiting anyway. @@ -481,10 +480,7 @@ impl Drop for CrosstermBackend { } } -impl Write for CrosstermBackend -where - W: Write, -{ +impl io::Write for CrosstermBackend { /// Writes a buffer of bytes to the underlying buffer. fn write(&mut self, buf: &[u8]) -> io::Result { self.writer.write(buf) @@ -496,10 +492,7 @@ where } } -impl Backend for CrosstermBackend -where - W: Write, -{ +impl Backend for CrosstermBackend { fn draw<'a, I>(&mut self, content: I) -> io::Result<()> where I: Iterator, @@ -687,10 +680,7 @@ struct ModifierDiff { } impl ModifierDiff { - fn queue(self, mut w: W) -> io::Result<()> - where - W: io::Write, - { + fn queue(self, mut w: W) -> io::Result<()> { let removed = self.from - self.to; if removed.contains(Modifier::REVERSED) { queue!(w, SetAttribute(CAttribute::NoReverse))?;