aarty/src/args.rs

72 lines
2.5 KiB
Rust
Raw Normal View History

2022-10-03 14:04:38 +00:00
pub mod args {
2022-10-03 18:13:59 +00:00
use super::enums::*;
use clap::{arg, ColorChoice, Parser};
2022-10-03 14:04:38 +00:00
#[derive(Parser, Debug)]
2022-10-03 19:00:39 +00:00
#[command(author, version, about, long_about = None, color = ColorChoice::Always)]
2022-10-03 14:04:38 +00:00
pub struct Arguments {
/// The art mode to use
2022-10-03 19:00:39 +00:00
#[arg(short, long, default_value = "normal-ascii")]
2022-10-03 14:04:38 +00:00
pub mode: Mode,
2022-10-03 19:00:39 +00:00
#[arg(long, default_value = "stdout", alias = "mo")]
2022-10-03 14:04:38 +00:00
pub output_method: OutputMethod,
/// The image to convert to ASCII art
pub image: String,
2022-10-03 14:22:08 +00:00
/// The character to use for drawing the image (lighter to darker)
/// You can user one character if you uses the color mode
2022-10-03 19:00:39 +00:00
#[arg(short, long, default_value = " .,-~!;:=*&%$@#")]
2022-10-03 14:22:08 +00:00
pub characters: String,
2022-10-03 18:13:59 +00:00
/// The output scale (1 is the original size)
2022-10-03 19:00:39 +00:00
#[arg(short, long, default_value = "4")]
2022-10-03 18:13:59 +00:00
pub scale: u32,
/// Enstablish how much wide is the output images, in columns. Overrides `scale`
#[arg(short, long, default_value= None )]
pub width: Option<u32>,
2022-10-03 18:50:48 +00:00
/// The background color to use
2022-10-03 19:00:39 +00:00
#[arg(short, long, default_value = None)]
2022-10-03 18:50:48 +00:00
pub background: Option<String>,
2022-10-03 14:04:38 +00:00
/// The output file to write to (if output_method is file)
2022-10-03 19:00:39 +00:00
#[arg(short, long, default_value = "ascii_image.txt")]
2022-10-03 14:22:08 +00:00
pub output: String,
2022-10-03 14:04:38 +00:00
}
2022-10-03 18:13:59 +00:00
impl Arguments {
pub fn validate(&self) -> Result<(), String> {
if self.characters.is_empty() {
2022-10-03 18:13:59 +00:00
return Err("No characters provided".to_string());
} else if self.characters.len() == 1 {
if self.mode == Mode::NormalAscii {
return Err("One character provided but mode is normal-ascii".to_string());
}
} else if self.characters.len() > 32 {
return Err("Too many characters provided, max is 32".to_string());
}
Ok(())
}
}
}
pub mod enums {
use clap::ValueEnum;
2022-10-03 19:00:39 +00:00
#[derive(Copy, Clone, ValueEnum, Debug, PartialOrd, Eq, PartialEq)]
2022-10-03 14:04:38 +00:00
pub enum Mode {
/// Normal ASCII art
#[clap(alias = "n")]
NormalAscii,
/// Colored ASCII art, the colors are based on the terminal colors
#[clap(alias = "c")]
Colored,
2022-10-03 14:04:38 +00:00
}
#[derive(Copy, Clone, ValueEnum, Debug, PartialOrd, Eq, PartialEq)]
pub enum OutputMethod {
/// Save the ascii art to a file
#[clap(alias = "f")]
File,
/// Print the ascii art to the terminal
#[clap(alias = "s")]
Stdout,
}
}