diff options
author | Tim Oram <dev@mitmaro.ca> | 2022-02-09 08:55:43 -0330 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2022-02-09 10:20:26 -0330 |
commit | 889bd039d1240b1e5c8de9d3c08dcb706f86dafb (patch) | |
tree | 00d64ddf1849258a763d21235f929855ac609872 | |
parent | d0711cc132f16379ff451435a5f1e7d220dcdbde (diff) |
Fix disabled lints in the display crate
-rw-r--r-- | src/display/src/crossterm.rs | 1 | ||||
-rw-r--r-- | src/display/src/lib.rs | 52 | ||||
-rw-r--r-- | src/display/src/testutil/mockcrossterm.rs | 1 | ||||
-rw-r--r-- | src/display/src/testutil/mod.rs | 4 | ||||
-rw-r--r-- | src/display/src/tui.rs | 83 | ||||
-rw-r--r-- | src/display/src/utils.rs | 16 |
6 files changed, 138 insertions, 19 deletions
diff --git a/src/display/src/crossterm.rs b/src/display/src/crossterm.rs index 1c2b8a2..025c5ee 100644 --- a/src/display/src/crossterm.rs +++ b/src/display/src/crossterm.rs @@ -153,6 +153,7 @@ impl CrossTerm { /// Create a new instance. #[inline] #[must_use] + #[allow(clippy::new_without_default)] pub fn new() -> Self { Self { window: BufWriter::new(stdout()), diff --git a/src/display/src/lib.rs b/src/display/src/lib.rs index 132dbfd..39be959 100644 --- a/src/display/src/lib.rs +++ b/src/display/src/lib.rs @@ -70,19 +70,6 @@ rustdoc::private_intra_doc_links )] // LINT-REPLACE-END -#![allow( - clippy::as_conversions, - clippy::cast_possible_truncation, - clippy::default_numeric_fallback, - clippy::else_if_without_else, - clippy::integer_division, - clippy::missing_errors_doc, - clippy::missing_panics_doc, - clippy::new_without_default, - clippy::too_many_lines, - clippy::unwrap_used, - clippy::wildcard_enum_match_arm -)] //! Git Interactive Rebase Tool - Display Module //! @@ -155,6 +142,7 @@ pub struct Display<T: Tui> { impl<T: Tui> Display<T> { /// Create a new display instance. #[inline] + #[allow(clippy::too_many_lines)] pub fn new(tui: T, theme: &Theme) -> Self { let color_mode = tui.get_color_mode(); let normal = register_selectable_color_pairs( @@ -290,12 +278,18 @@ impl<T: Tui> Display<T> { } /// Draws a string of text to the terminal interface. + /// + /// # Errors + /// Will error if the underlying terminal interface is in an error state. #[inline] pub fn draw_str(&mut self, s: &str) -> Result<()> { self.tui.print(s) } /// Clear the terminal interface and reset any style and color attributes. + /// + /// # Errors + /// Will error if the underlying terminal interface is in an error state. #[inline] pub fn clear(&mut self) -> Result<()> { self.color(DisplayColor::Normal, false)?; @@ -306,6 +300,9 @@ impl<T: Tui> Display<T> { /// Force a refresh of the terminal interface. This normally should be called after after all /// text has been drawn to the terminal interface. This is considered a slow operation, so /// should be called only as needed. + /// + /// # Errors + /// Will error if the underlying terminal interface is in an error state. #[inline] pub fn refresh(&mut self) -> Result<()> { self.tui.flush() @@ -313,7 +310,11 @@ impl<T: Tui> Display<T> { /// Set the color of text drawn to the terminal interface. This will only change text drawn to /// the terminal after this function call. + /// + /// # Errors + /// Will error if the underlying terminal interface is in an error state. #[inline] + #[allow(clippy::too_many_lines)] pub fn color(&mut self, color: DisplayColor, selected: bool) -> Result<()> { self.tui.set_color( if selected { @@ -365,6 +366,9 @@ impl<T: Tui> Display<T> { /// Set the style attributes of text drawn to the terminal interface. This will only change text /// drawn to the terminal after this function call. + /// + /// # Errors + /// Will error if the underlying terminal interface is in an error state. #[inline] pub fn set_style(&mut self, dim: bool, underline: bool, reverse: bool) -> Result<()> { self.set_dim(dim)?; @@ -374,25 +378,37 @@ impl<T: Tui> Display<T> { /// Get the width and height of the terminal interface. This can be a slow operation, so should /// not be called unless absolutely needed. + /// + /// # Errors + /// Will error if the underlying terminal interface is in an error state. #[inline] pub fn get_window_size(&self) -> Size { self.tui.get_size() } /// Reset the cursor position to the start of the line. + /// + /// # Errors + /// Will error if the underlying terminal interface is in an error state. #[inline] pub fn ensure_at_line_start(&mut self) -> Result<()> { self.tui.move_to_column(1) } /// Move the cursor position `right` characters from the end of the line. + /// + /// # Errors + /// Will error if the underlying terminal interface is in an error state. #[inline] pub fn move_from_end_of_line(&mut self, right: u16) -> Result<()> { - let width = self.get_window_size().width(); - self.tui.move_to_column(width as u16 - right + 1) + let width = self.get_window_size().width().try_into().unwrap_or(u16::MAX); + self.tui.move_to_column(width - right + 1) } /// Move the cursor to the next line. + /// + /// # Errors + /// Will error if the underlying terminal interface is in an error state. #[inline] pub fn next_line(&mut self) -> Result<()> { self.tui.move_next_line() @@ -400,6 +416,9 @@ impl<T: Tui> Display<T> { /// Start the terminal interface interactions. This should be called before any terminal /// interactions are performed. + /// + /// # Errors + /// Will error if the underlying terminal interface is in an error state. #[inline] pub fn start(&mut self) -> Result<()> { self.tui.start()?; @@ -409,6 +428,9 @@ impl<T: Tui> Display<T> { /// End the terminal interface interactions. This should be called after all terminal /// interactions are complete. This resets the terminal interface to the default state, and /// should be called on program exit. + /// + /// # Errors + /// Will error if the underlying terminal interface is in an error state. #[inline] pub fn end(&mut self) -> Result<()> { self.tui.end()?; diff --git a/src/display/src/testutil/mockcrossterm.rs b/src/display/src/testutil/mockcrossterm.rs index a5ee870..2b96579 100644 --- a/src/display/src/testutil/mockcrossterm.rs +++ b/src/display/src/testutil/mockcrossterm.rs @@ -126,6 +126,7 @@ impl CrossTerm { /// Create a new mocked version of `CrossTerm`. #[inline] #[must_use] + #[allow(clippy::new_without_default)] pub fn new() -> Self { Self { attributes: Attributes::from(Attribute::Reset), diff --git a/src/display/src/testutil/mod.rs b/src/display/src/testutil/mod.rs index 98f9c4e..653ffb6 100644 --- a/src/display/src/testutil/mod.rs +++ b/src/display/src/testutil/mod.rs @@ -6,6 +6,10 @@ pub use self::{mockcrossterm::CrossTerm, state::State}; use crate::Display; /// Assert the the content of the Display is an expected value. +/// +/// # Panics +/// +/// Will panic is the expected output does not match the rendered output. #[inline] pub fn assert_output(display: &Display<CrossTerm>, expected: &[&str]) { assert_eq!(display.tui.get_output().join(""), format!("{}\n", expected.join("\n"))); diff --git a/src/display/src/tui.rs b/src/display/src/tui.rs index c352075..83119d5 100644 --- a/src/display/src/tui.rs +++ b/src/display/src/tui.rs @@ -7,31 +7,114 @@ use super::{color_mode::ColorMode, Size}; pub trait Tui { /// Get the supported color mode. fn get_color_mode(&self) -> ColorMode; + /// Reset the terminal interface to a default state. + /// + /// # Errors + /// + /// Errors if the Tui cannot be reset for any reason. In general this should not error, and if + /// this does generate an error, the Tui should be considered to be in a non-recoverable state. fn reset(&mut self) -> Result<()>; + /// Flush the contents printed to the terminal interface. + /// + /// # Errors + /// + /// Errors if the Tui cannot be flushed for any reason. In general this should not error, and if + /// this does generate an error, the Tui should be considered to be in a non-recoverable state. fn flush(&mut self) -> Result<()>; + /// Print text to the terminal interface. + /// + /// # Errors + /// + /// Errors if the Tui cannot be printed to for any reason. In general this should not error, and + /// if this does generate an error, the Tui should be considered to be in a non-recoverable + /// state. fn print(&mut self, s: &str) -> Result<()>; + /// Set the color attribute of text printed to the terminal interface. + /// + /// # Errors + /// + /// Errors if the Tui cannot set the color for any reason. In general this should not error, and + /// if this does generate an error, the Tui should be considered to be in a non-recoverable + /// state. fn set_color(&mut self, colors: Colors) -> Result<()>; + /// Set the dimmed style attribute of text printed to the terminal interface. + /// + /// # Errors + /// + /// Errors if the Tui cannot set the dimmed state for any reason. In general this should not + /// error, and if this does generate an error, the Tui should be considered to be in a + /// non-recoverable state. fn set_dim(&mut self, dim: bool) -> Result<()>; + /// Set the underlined style attribute of text printed to the terminal interface. + /// + /// # Errors + /// + /// Errors if the Tui cannot set the underline state for any reason. In general this should not + /// error, and if this does generate an error, the Tui should be considered to be in a + /// non-recoverable state. fn set_underline(&mut self, underline: bool) -> Result<()>; + /// Set the reversed style attribute of text printed to the terminal interface. + /// + /// # Errors + /// + /// Errors if the Tui cannot set the reversed state for any reason. In general this should not + /// error, and if this does generate an error, the Tui should be considered to be in a + /// non-recoverable state. fn set_reverse(&mut self, reverse: bool) -> Result<()>; + /// Read the next input event from the terminal interface. + /// + /// # Errors + /// + /// Errors if the Tui cannot read an event for any reason. In general this should not error, and + /// if this does generate an error, the Tui should be considered to be in a non-recoverable + /// state. fn read_event() -> Result<Option<Event>> where Self: Sized; + /// Get the number of columns and rows of the terminal interface. fn get_size(&self) -> Size; + /// Move the cursor position `x` characters from the start of the line. + /// + /// # Errors + /// + /// Errors if the Tui cannot move to a column for any reason. In general this should not error, + /// and if this does generate an error, the Tui should be considered to be in a non-recoverable + /// state. fn move_to_column(&mut self, x: u16) -> Result<()>; + /// Move the cursor to the next line. + /// + /// # Errors + /// + /// Errors if the Tui cannot move to the next line for any reason. In general this should not + /// error, and if this does generate an error, the Tui should be considered to be in a + /// non-recoverable state. fn move_next_line(&mut self) -> Result<()>; + /// Start the terminal interface interactions. + /// + /// # Errors + /// + /// Errors if the Tui cannot move to a started state any reason. In general this should not + /// error,and if this does generate an error, the Tui should be considered to be in a + /// non-recoverable state. fn start(&mut self) -> Result<()>; + /// End the terminal interface interactions. + /// + /// # Errors + /// + /// Errors if the Tui cannot move to an ended state any reason. In general this should not + /// error,and if this does generate an error, the Tui should be considered to be in a + /// non-recoverable state. fn end(&mut self) -> Result<()>; } diff --git a/src/display/src/utils.rs b/src/display/src/utils.rs index 36b2ff9..40a7925 100644 --- a/src/display/src/utils.rs +++ b/src/display/src/utils.rs @@ -20,7 +20,7 @@ pub(super) fn detect_color_mode(number_of_colors: u16) -> ColorMode { // version 0.36.00 return ColorMode::TrueColor; } - else if parsed_version > 0 { + if parsed_version > 0 { return ColorMode::EightBit; } } @@ -69,7 +69,7 @@ pub(super) fn register_selectable_color_pairs( // Modified version from gyscos/cursive (https://github.com/gyscos/cursive) // Copyright (c) 2015 Alexandre Bury - MIT License -#[allow(clippy::cast_sign_loss)] +#[allow(clippy::cast_sign_loss, clippy::too_many_lines, clippy::integer_division)] fn find_color(color_mode: ColorMode, color: Color) -> CrosstermColor { match color { Color::Default => CrosstermColor::Reset, @@ -163,7 +163,11 @@ fn find_color(color_mode: ColorMode, color: Color) -> CrosstermColor { let r = 6 * u16::from(red) / 256; let g = 6 * u16::from(green) / 256; let b = 6 * u16::from(blue) / 256; - CrosstermColor::AnsiValue((16 + 36 * r + 6 * g + b) as u8) + CrosstermColor::AnsiValue( + (16 + 36 * r + 6 * g + b) + .try_into() + .expect("Invalid 4-bit ANSI value mapping"), + ) } }, Color::Rgb { red, green, blue } => { @@ -171,7 +175,11 @@ fn find_color(color_mode: ColorMode, color: Color) -> CrosstermColor { let r = if red > 127 { 1 } else { 0 }; let g = if green > 127 { 1 } else { 0 }; let b = if blue > 127 { 1 } else { 0 }; - CrosstermColor::AnsiValue((r + 2 * g + 4 * b) as u8) + CrosstermColor::AnsiValue( + (r + 2 * g + 4 * b) + .try_into() + .expect("Invalid 8 color ANSI value mapping"), + ) }, } } |