|
1 |
| -// use console::style; |
| 1 | +use clap::{arg, command, Arg, ArgAction, ArgMatches, Command}; |
| 2 | +use console::Style; |
2 | 3 | use indicatif::{ProgressBar, ProgressStyle};
|
| 4 | +use log::info; |
| 5 | +use pretty_env_logger::env_logger::Builder; |
3 | 6 |
|
4 | 7 | const ITERATIONS: u32 = 255;
|
5 | 8 | const WIDTH: u32 = 800;
|
6 | 9 | const HEIGHT: u32 = 800;
|
7 | 10 | const PATH: &str = "mandelbrot.png";
|
8 | 11 |
|
9 | 12 | 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") |
16 | 57 | );
|
| 58 | + let pb = ProgressBar::new(WIDTH as u64 * HEIGHT as u64); |
| 59 | + style_progress_bar(&pb); |
17 | 60 | 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"); |
20 | 63 |
|
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") |
27 | 67 | );
|
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 | +} |
30 | 75 |
|
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( |
33 | 78 | ProgressStyle::default_bar()
|
34 | 79 | .template("[{elapsed_precise}] [{bar:40.cyan/blue}] {percent}%")
|
35 | 80 | .unwrap()
|
36 | 81 | .progress_chars("##-"),
|
37 | 82 | );
|
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"); |
43 | 83 | }
|
0 commit comments