From d87a968e39d37a49eb19f44f0081ca98ebc9c662 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 14 Jan 2023 02:09:14 +0100 Subject: start on a proper CLI This involves actual command line arguments, and more progress bars! --- src/main.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 269d95c..77af69f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,47 +1,92 @@ -use std::{env, fs, path::PathBuf}; +use std::{fs, io, path::PathBuf}; -use color_eyre::eyre::Result; -use indicatif::{ProgressBar, ProgressStyle}; +use clap::Parser; +use color_eyre::eyre::{bail, Result}; +use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle}; +use is_terminal::IsTerminal; mod gpx; mod layer; mod renderer; +#[derive(Parser, Debug, Clone)] +#[command(author, version, about)] +struct Args { + /// The GPX files to parse. + #[arg(required = true)] + files: Vec, + + #[arg(long, default_value_t = 0)] + min_zoom: u32, + + #[arg(long, default_value_t = 19)] + max_zoom: u32, +} + fn main() -> Result<()> { color_eyre::install()?; + let args = Args::parse(); + + if args.max_zoom < args.min_zoom { + bail!("Max zoom cannot be smaller than min zoom!"); + } + let progress_style = ProgressStyle::with_template("[{elapsed}] {prefix:.cyan} {wide_bar} {pos:.green}/{len}")?; + let zoom_style = + ProgressStyle::with_template("[{elapsed}] {prefix:.yellow} {wide_bar} {pos:.green}/{len}")?; + + let use_progress_bars = io::stdout().is_terminal(); + let make_bar = |len| { + if use_progress_bars { + ProgressBar::new(len) + } else { + ProgressBar::hidden() + } + }; - let gpx_folder = env::args().nth(1).unwrap(); - println!("Reading from {gpx_folder}"); + let bar = make_bar(args.files.len().try_into().unwrap()).with_style(progress_style.clone()); + bar.set_prefix("Reading GPX files"); let mut tracks = Vec::new(); - for file in fs::read_dir(gpx_folder).unwrap() { - let file = file.unwrap(); - let data = gpx::extract_from_file(file.path(), gpx::Compression::None).unwrap(); + for file in &args.files { + let data = gpx::extract_from_file(file, gpx::Compression::None).unwrap(); tracks.push(data); + bar.inc(1); } + bar.finish(); - for zoom in 0..=19 { - println!("Doing level {zoom}"); + let multibar = MultiProgress::new(); + if !use_progress_bars { + multibar.set_draw_target(ProgressDrawTarget::hidden()) + } + let zoom_bar = make_bar((args.max_zoom - args.min_zoom + 1).into()).with_style(zoom_style); + multibar.add(zoom_bar.clone()); + zoom_bar.set_prefix("Zoom levels"); - let bar = - ProgressBar::new(tracks.len().try_into().unwrap()).with_style(progress_style.clone()); + for zoom in args.min_zoom..=args.max_zoom { + let bar = make_bar(tracks.len().try_into().unwrap()).with_style(progress_style.clone()); + multibar.insert_from_back(1, bar.clone()); bar.set_prefix("Rendering heat zones"); let counter = renderer::render_heatcounter(zoom, &tracks, |x| bar.inc(x.try_into().unwrap())); bar.finish(); + multibar.remove(&bar); let target = ["tiles", &zoom.to_string()].iter().collect::(); fs::create_dir(&target)?; - let bar = ProgressBar::new(counter.tile_count().try_into().unwrap()) - .with_style(progress_style.clone()); + let bar = + make_bar(counter.tile_count().try_into().unwrap()).with_style(progress_style.clone()); + multibar.insert_from_back(1, bar.clone()); bar.set_prefix("Saving heat tiles"); renderer::lazy_colorization(counter, &target, |x| bar.inc(x.try_into().unwrap()))?; bar.finish(); + multibar.remove(&bar); + zoom_bar.inc(1); } + zoom_bar.finish(); Ok(()) } -- cgit v1.2.3