From a85036ea4793b5402a8e9ce51425fc555769031b Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 17 Apr 2024 18:54:43 +0200 Subject: [PATCH] 22 allow skip input validation (#28) * reworked for easier testing --- results.xml | 0 src/color.rs | 11 ++----- src/terminal.rs | 88 +++++++++++++++++++++++++++++++++++-------------- 3 files changed, 66 insertions(+), 33 deletions(-) delete mode 100644 results.xml diff --git a/results.xml b/results.xml deleted file mode 100644 index e69de29..0000000 diff --git a/src/color.rs b/src/color.rs index 31e26f6..fa33c98 100644 --- a/src/color.rs +++ b/src/color.rs @@ -71,7 +71,6 @@ pub enum ColorPalette { /// - Never: never enable color output /// /// [Read More](https://rust-cli-recommendations.sunshowers.io/colors.html#general-recommendations) -#[allow(dead_code)] #[derive(Debug, PartialEq)] enum ColorOption { /// always enable color output @@ -85,17 +84,14 @@ enum ColorOption { } /// configuration struct for managing cli color settings -#[allow(dead_code)] struct CliColorConfig { /// the chosen color option for cli output - #[allow(dead_code)] color_option: ColorOption, /// the color palette supported by the terminal color_palette: ColorPalette, } -#[allow(dead_code)] impl Default for CliColorConfig { /// creates a new `CliColorConfig` instance with default settings /// @@ -108,13 +104,10 @@ impl Default for CliColorConfig { let color_palette = CliColorConfig::get_supported_color_palette(Stream::Stdout); - Self { - color_option, - color_palette, - } + CliColorConfig::new(color_option, color_palette) } } -#[allow(dead_code)] + impl CliColorConfig { /// creates a new `CliColorConfig` instance with custom settings /// diff --git a/src/terminal.rs b/src/terminal.rs index 88b79be..553f605 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -24,34 +24,42 @@ pub(crate) mod console_render { pub fn render_styled_line(row: u16, content: &[StyledString]) { if *ENABLE_COLOR { - let mut stdout = stdout(); + render_styled(row, content); + } else { + render_unstyled(row, content); + } + } + + pub fn render_unstyled(row: u16, content: &[StyledString]) { + render_line( + &content + .iter() + .map(|styled_string| styled_string.string.clone()) + .collect::>(), + row, + ); + } + + pub fn render_styled(row: u16, content: &[StyledString]) { + let mut stdout = stdout(); + queue!( + stdout, + cursor::RestorePosition, + cursor::MoveToNextLine(row + 1), // move to the next line based on index +1 + terminal::Clear(terminal::ClearType::CurrentLine), + ) + .unwrap(); + for content in content { queue!( stdout, - cursor::RestorePosition, - cursor::MoveToNextLine(row + 1), // move to the next line based on index +1 - terminal::Clear(terminal::ClearType::CurrentLine), + style::SetStyle(content.style), // set animation color + style::Print(&content.string), + style::ResetColor, // reset colors ) .unwrap(); - for content in content { - queue!( - stdout, - style::SetStyle(content.style), // set animation color - style::Print(&content.string), - style::ResetColor, // reset colors - ) - .unwrap(); - } - - stdout.flush().unwrap(); - } else { - render_line( - &content - .iter() - .map(|styled_string| styled_string.string.clone()) - .collect::>(), - row, - ); } + + stdout.flush().unwrap(); } pub fn cleanup() { @@ -89,8 +97,40 @@ pub(crate) mod console_cursor { } /// resets the cursor to be shown and restores its saved position - #[allow(dead_code)] pub fn next_line(num: u16) { execute!(stdout(), cursor::MoveToNextLine(num)).unwrap(); } } + +#[cfg(test)] +mod tests { + use crate::style::Color; + + use crate::style::StyledString; + + use super::*; + + #[test] + fn test_render_unstyled() { + let content = vec![ + StyledString::simple("Hello, ", Some(Color::Red), None, None), + StyledString::simple(" world", Some(Color::Green), None, None), + ]; + + console_render::render_unstyled(4, &content); + + assert!(true); + } + + #[test] + fn test_render_styled() { + let content = vec![ + StyledString::simple("Hello, ", Some(Color::Red), None, None), + StyledString::simple(" world", Some(Color::Green), None, None), + ]; + + console_render::render_styled(4, &content); + + assert!(true); + } +}