Skip to content

Commit be25a2b

Browse files
author
Lloyd Lobo
committed
feat: add clap for arg parsing
1 parent 0bf67fd commit be25a2b

File tree

2 files changed

+68
-24
lines changed

2 files changed

+68
-24
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
anyhow = "1.0.69"
10+
clap = { version = "4.1.4", features = ["derive", "cargo"] }
911
# anyhow = "1.0.69"
1012
console = "0.15.5"
1113
image = "0.24.5"
1214
indicatif = "0.17.3"
15+
log = "0.4.17"
16+
pretty_env_logger = "0.4.0"
1317
# termion = "2.0.1"
1418
# tui = { version = "0.19.0", features = ["termion"] }

src/bin/mandelbrot.rs

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,83 @@
1-
// use console::style;
1+
use clap::{arg, command, Arg, ArgAction, ArgMatches, Command};
2+
use console::Style;
23
use indicatif::{ProgressBar, ProgressStyle};
4+
use log::info;
5+
use pretty_env_logger::env_logger::Builder;
36

47
const ITERATIONS: u32 = 255;
58
const WIDTH: u32 = 800;
69
const HEIGHT: u32 = 800;
710
const PATH: &str = "mandelbrot.png";
811

912
fn main() {
10-
let progress_bar = ProgressBar::new(WIDTH as u64 * HEIGHT as u64);
11-
progress_bar.set_style(
12-
ProgressStyle::default_bar()
13-
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {percent}%")
14-
.unwrap()
15-
.progress_chars("##-"),
13+
Builder::from_default_env().format_timestamp(None).init();
14+
15+
if let Err(e) = try_main() {
16+
eprintln!("{}", e);
17+
std::process::exit(1);
18+
}
19+
}
20+
21+
fn try_main() -> anyhow::Result<()> {
22+
let matches: ArgMatches = command!()
23+
.arg(
24+
Arg::new("ascii")
25+
.long("ascii")
26+
.help("Generates the Mandelbrot set as ASCII art and print to terminal"),
27+
)
28+
.arg(
29+
Arg::new("text")
30+
.long("text")
31+
.help("Generates the Mandelbrot set as ASCII art and saves to text file"),
32+
)
33+
.arg(
34+
Arg::new("image")
35+
.long("image")
36+
.help("Generates the Mandelbrot set as an image and saves to file"),
37+
)
38+
.after_help(
39+
"Longer explanation to appear after the options when displaying the help information \
40+
from --help or -h",
41+
)
42+
.get_matches();
43+
44+
if let Some(ascii) = matches.get_one::<String>("ascii") {
45+
dbg!(ascii);
46+
info!("Rendering image Mandelbrot set as {}", Style::new().cyan().apply_to("ASCII"));
47+
let pb = ProgressBar::new(WIDTH as u64 * HEIGHT as u64);
48+
style_progress_bar(&pb);
49+
let image = mandelbrot::mandelbrot_ascii::collect_ascii();
50+
pb.finish();
51+
mandelbrot::mandelbrot_ascii::print_ascii(image.clone());
52+
}
53+
54+
info!(
55+
"Rendering image Mandelbrot set as {}",
56+
Style::new().cyan().apply_to("ASCII and saving to file")
1657
);
58+
let pb = ProgressBar::new(WIDTH as u64 * HEIGHT as u64);
59+
style_progress_bar(&pb);
1760
let image = mandelbrot::mandelbrot_ascii::collect_ascii();
18-
progress_bar.finish();
19-
mandelbrot::mandelbrot_ascii::print_ascii(image.clone());
61+
mandelbrot::mandelbrot_ascii::write_ascii_to_file(image);
62+
pb.finish_with_message("Wrote ascii to file");
2063

21-
let progress_bar = ProgressBar::new(WIDTH as u64 * HEIGHT as u64);
22-
progress_bar.set_style(
23-
ProgressStyle::default_bar()
24-
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {percent}%")
25-
.unwrap()
26-
.progress_chars("##-"),
64+
info!(
65+
"Rendering image Mandelbrot set as {}",
66+
Style::new().cyan().apply_to("image and saving to file")
2767
);
28-
mandelbrot::mandelbrot_ascii::write_ascii_to_file(image);
29-
progress_bar.finish_with_message("Wrote ascii to file");
68+
let pb = ProgressBar::new(WIDTH as u64 * HEIGHT as u64);
69+
style_progress_bar(&pb);
70+
mandelbrot::mandelbrot_img::compose(WIDTH, HEIGHT, ITERATIONS).save(PATH)?;
71+
pb.finish_with_message("Saved image to file");
72+
73+
Ok(())
74+
}
3075

31-
let progress_bar = ProgressBar::new(WIDTH as u64 * HEIGHT as u64);
32-
progress_bar.set_style(
76+
fn style_progress_bar(pb: &ProgressBar) {
77+
pb.set_style(
3378
ProgressStyle::default_bar()
3479
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {percent}%")
3580
.unwrap()
3681
.progress_chars("##-"),
3782
);
38-
if let Err(e) = mandelbrot::mandelbrot_img::compose(WIDTH, HEIGHT, ITERATIONS).save(PATH) {
39-
eprintln!("{}", e);
40-
std::process::exit(1);
41-
}
42-
progress_bar.finish_with_message("Saved image to file");
4383
}

0 commit comments

Comments
 (0)