Skip to content

Commit

Permalink
feat: add command-line interface (CLI)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjian85 committed Jul 7, 2024
1 parent 7bbbcc6 commit 86f8f57
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 9 deletions.
114 changes: 114 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.5.8", features = ["derive"] }
image = "0.25.1"
indicatif = "0.17.8"
47 changes: 47 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use clap::Parser;
use std::fmt::{self, Display};
use std::str::FromStr;

#[derive(Parser)]
#[command(version)]
pub struct Cli {
/// The image size of the output
#[arg(short, long, default_value_t = Size::new(1024, 768))]
pub size: Size,

/// The path to the output
#[arg(short, long, default_value_t = String::from("output.png"))]
pub output: String,
}

#[derive(Clone)]
pub struct Size {
pub width: u32,
pub height: u32,
}

impl Size {
pub fn new(width: u32, height: u32) -> Self {
Self { width, height }
}
}

impl FromStr for Size {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let size: Vec<_> = s.split('x').collect();
if size.len() != 2 {
return Err(String::from("invalid number of dimensions found in string"));
}
let width = size[0].parse().map_err(|e| format!("{e}"))?;
let height = size[1].parse().map_err(|e| format!("{e}"))?;
Ok(Self::new(width, height))
}
}

impl Display for Size {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}x{}", self.width, self.height)
}
}
22 changes: 13 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
mod cli;

use clap::Parser;
use cli::{Cli, Size};
use image::{Rgb, RgbImage};
use indicatif::ProgressBar;

fn main() {
let image_width = 256;
let image_height = 256;
let cli = Cli::parse();
let Size { width, height } = cli.size;

let mut image = RgbImage::new(image_width, image_height);
let progress_bar = ProgressBar::new(image_height as u64);
let mut image = RgbImage::new(width, height);
let progress_bar = ProgressBar::new(height as u64);

for y in 0..image_height {
for x in 0..image_width {
let r = x as f64 / (image_width - 1) as f64;
let g = y as f64 / (image_height - 1) as f64;
for y in 0..height {
for x in 0..width {
let r = x as f64 / (width - 1) as f64;
let g = y as f64 / (height - 1) as f64;
let b = 0.0;

let r = (255.999 * r) as u8;
Expand All @@ -23,6 +27,6 @@ fn main() {
progress_bar.inc(1);
}

image.save("output.png").unwrap();
image.save(cli.output).unwrap();
progress_bar.finish();
}

0 comments on commit 86f8f57

Please sign in to comment.