Skip to content

Commit

Permalink
Initial refactor (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
interrrp authored Oct 6, 2023
1 parent dc437f8 commit d78b9ac
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 39 deletions.
41 changes: 41 additions & 0 deletions src/color_printer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use owo_colors::OwoColorize;

use crate::colors;

/// Colors the given lines in rainbow colors.
///
/// # Arguments
///
/// * `lines` - The lines to color.
///
/// # Returns
///
/// The colored output.
pub fn rainbow(string: &str) -> String {
string
.lines()
.zip(colors::colors().iter().cycle())
.map(|(line, color)| line.color(*color).to_string())
.collect::<Vec<_>>()
.join("\n")
}

/// Colors the given lines in random colors.
///
/// # Arguments
///
/// * `lines` - The lines to color.
///
/// # Returns
///
/// The colored output.
pub fn random(string: &str) -> String {
string
.lines()
.map(|line| {
let color = colors::random();
line.color(color).to_string()
})
.collect::<Vec<_>>()
.join("\n")
}
31 changes: 31 additions & 0 deletions src/colors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use owo_colors::DynColors;
use rand::prelude::SliceRandom;

/// Returns colors sorted in the rainbow order (ROYGBIV). This is a constant
/// function, and is guaranteed to always return the same value.
///
/// # Returns
///
/// Colors sorted in the rainbow order.
pub fn colors() -> [DynColors; 7] {
[
"#B80A41", // Red
"#E23838", // Orange
"#DAAC06", // Yellow
"#6EB122", // Green
"#00938A", // Blue
"#4E4BA8", // Indigo
"#BA55D3", // Purple
]
.map(|color| color.parse().unwrap())
}

/// Returns a random color from the `COLORS` array.
///
/// # Returns
///
/// A random color from the `COLORS` array.
pub fn random() -> DynColors {
let mut rng = rand::thread_rng();
*colors().choose(&mut rng).unwrap()
}
45 changes: 45 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use clap::{builder::Arg, crate_authors, crate_version, Command};

/// The color mode.
pub enum ColorMode {
None,
Rainbow,
Random,
}

/// Creates the command. This is guaranteed to always return the same value, so
/// this should be called once and the result stored.
///
/// # Returns
///
/// A `clap` `Command`.
pub fn create_command() -> Command {
Command::new("clidogs")
.about("∪・ω・U")
.version(crate_version!())
.author(crate_authors!("\n"))
.arg(Arg::new("rainbow").long("rainbow"))
.arg(Arg::new("random").long("random").conflicts_with("rainbow"))
}

/// Returns the color mode. This assumes that the command passed is the one
/// returned by `create_command`.
///
/// # Arguments
///
/// * `command` - The command to get the color mode from.
///
/// # Returns
///
/// The color mode.
pub fn get_color_mode(command: Command) -> ColorMode {
let matches = command.get_matches();

if matches.contains_id("rainbow") {
ColorMode::Rainbow
} else if matches.contains_id("random") {
ColorMode::Random
} else {
ColorMode::None
}
}
51 changes: 12 additions & 39 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,19 @@
use clap::{builder::Arg, crate_authors, crate_version, Command};
use owo_colors::{DynColors, OwoColorize};
use rand::seq::SliceRandom;
mod color_printer;
mod colors;
mod command;

/// The first dog's ASCII art.
static DOG1: &str = include_str!("./art/dog1.txt");

fn main() {
let colors: [DynColors; 7] = [
"#B80A41", // Red
"#E23838", // Orange
"#DAAC06", // Yellow
"#6EB122", // Green
"#00938A", // Blue
"#4E4BA8", // Indigo
"#BA55D3", // Purple
]
.map(|color| color.parse().unwrap());
let command = command::create_command();

let command = Command::new("clidogs")
.about("∪・ω・U")
.version(crate_version!())
.author(crate_authors!("\n"))
.arg(Arg::new("rainbow").long("rainbow"))
.arg(Arg::new("random").long("random").conflicts_with("rainbow"));

let lines: Vec<_> = DOG1.lines().collect();

let matches = command.get_matches();

if matches.contains_id("rainbow") {
lines
.iter()
.zip(colors.iter().cycle())
.map(|(line, color)| line.color(*color))
.for_each(|line| println!("{line}"));
} else if matches.contains_id("random") {
let mut rng = rand::thread_rng();
for line in lines {
println!("{}", line.color(*colors.choose(&mut rng).unwrap()));
}
} else {
for line in lines {
println!("{line}");
println!(
"{}",
match command::get_color_mode(command) {
command::ColorMode::None => DOG1.to_string(),
command::ColorMode::Rainbow => color_printer::rainbow(DOG1),
command::ColorMode::Random => color_printer::random(DOG1),
}
}
);
}

0 comments on commit d78b9ac

Please sign in to comment.