-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
129 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
); | ||
} |