Improve the code yoo 💙🥰🦀

This commit is contained in:
Anas Elgarhy 2022-10-03 21:00:39 +02:00
parent 9d26bf939a
commit 1031014e27
2 changed files with 12 additions and 16 deletions

View file

@ -3,27 +3,27 @@ pub mod args {
use super::enums::*; use super::enums::*;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about=None, color=ColorChoice::Always)] #[command(author, version, about, long_about = None, color = ColorChoice::Always)]
pub struct Arguments { pub struct Arguments {
/// The art mode to use /// The art mode to use
#[arg(short, long, default_value="normal-ascii")] #[arg(short, long, default_value = "normal-ascii")]
pub mode: Mode, pub mode: Mode,
#[arg(long, default_value="stdout", alias="mo")] #[arg(long, default_value = "stdout", alias = "mo")]
pub output_method: OutputMethod, pub output_method: OutputMethod,
/// The image to convert to ASCII art /// The image to convert to ASCII art
pub image: String, pub image: String,
/// The character to use for drawing the image (lighter to darker) /// The character to use for drawing the image (lighter to darker)
/// You can user one character if you uses the color mode /// You can user one character if you uses the color mode
#[arg(short, long, default_value=" .,-~!;:=*&%$@#")] #[arg(short, long, default_value = " .,-~!;:=*&%$@#")]
pub characters: String, pub characters: String,
/// The output scale (1 is the original size) /// The output scale (1 is the original size)
#[arg(short, long, default_value="4")] #[arg(short, long, default_value = "4")]
pub scale: u32, pub scale: u32,
/// The background color to use /// The background color to use
#[arg(short, long, default_value=None)] #[arg(short, long, default_value = None)]
pub background: Option<String>, pub background: Option<String>,
/// The output file to write to (if output_method is file) /// The output file to write to (if output_method is file)
#[arg(short, long, default_value="ascii_image.txt")] #[arg(short, long, default_value = "ascii_image.txt")]
pub output: String, pub output: String,
} }
@ -41,14 +41,12 @@ pub mod args {
Ok(()) Ok(())
} }
} }
} }
pub mod enums { pub mod enums {
use clap::ValueEnum; use clap::ValueEnum;
#[derive(Copy, Clone, ValueEnum, Debug, PartialOrd, Eq, PartialEq)] #[derive(Copy, Clone, ValueEnum, Debug, PartialOrd, Eq, PartialEq)]
pub enum Mode { pub enum Mode {
/// Normal ASCII art /// Normal ASCII art
#[clap(alias = "n")] #[clap(alias = "n")]

View file

@ -1,10 +1,8 @@
use image::{GenericImageView, DynamicImage}; use image::{GenericImageView, DynamicImage};
use colored::{ColoredString, Colorize}; use colored::{ColoredString, Colorize};
use crate::args::{ use crate::args::{
args::Arguments, args::Arguments,
enums::Mode enums::Mode,
}; };
pub fn generate_ascii(image: DynamicImage, args: &Arguments) -> Vec<ColoredString> { pub fn generate_ascii(image: DynamicImage, args: &Arguments) -> Vec<ColoredString> {
@ -19,7 +17,7 @@ pub fn generate_ascii(image: DynamicImage, args: &Arguments) -> Vec<ColoredStrin
output.push(get_character( output.push(get_character(
image.get_pixel(x, y), image.get_pixel(x, y),
&characters, args.mode, &characters, args.mode,
&args.background &args.background,
)); ));
} }
} }
@ -34,8 +32,8 @@ pub fn generate_ascii(image: DynamicImage, args: &Arguments) -> Vec<ColoredStrin
fn get_character( fn get_character(
pixel: image::Rgba<u8>, pixel: image::Rgba<u8>,
characters: &Vec<char>, mode: Mode, characters: &Vec<char>, mode: Mode,
background: &Option<String> background: &Option<String>,
) -> ColoredString { ) -> ColoredString {
let intent = if pixel[3] == 0 { 0 } else { pixel[0] / 3 + pixel[1] / 3 + pixel[2] / 3 }; let intent = if pixel[3] == 0 { 0 } else { pixel[0] / 3 + pixel[1] / 3 + pixel[2] / 3 };