Skip to content

Commit

Permalink
22 allow skip input validation (#28)
Browse files Browse the repository at this point in the history
* reworked for easier testing
  • Loading branch information
Arteiii authored Apr 17, 2024
1 parent e7401b7 commit a85036e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 33 deletions.
Empty file removed results.xml
Empty file.
11 changes: 2 additions & 9 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
///
Expand All @@ -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
///
Expand Down
88 changes: 64 additions & 24 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>(),
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::<Vec<_>>(),
row,
);
}

stdout.flush().unwrap();
}

pub fn cleanup() {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit a85036e

Please sign in to comment.