From 91880a10e978e17aab7059c08ecb81c7ca08a324 Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Thu, 21 Dec 2023 22:46:11 +0100 Subject: [PATCH] refactor: simplify cli args --- src/cli.rs | 10 +++------- src/main.rs | 9 +++++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 75d895d..ef10bf1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2,14 +2,10 @@ use clap::Parser; #[derive(Parser, Debug)] pub struct Args { - /// The format to use for timestamps - #[arg(long, default_value = "screenshot-%Y-%m-%d_%H-%M-%S.png")] - pub filename_format: Option, - - /// The output directory to write the file to. + /// The output directory or file to write the file to. /// - /// If a filename is specified instead of a directory, this will be used - /// for the output filename in place of `filename_format`. + /// If a if a directory is specified, a filename will be generated based on + /// the current date and time. #[arg(short, long)] pub out: String, } diff --git a/src/main.rs b/src/main.rs index 5155e22..0c14fd5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,11 +11,11 @@ mod imaging; fn main() { let args = cli::Args::parse(); - capture_all_displays(args.out, args.filename_format.unwrap_or_default()); + capture_all_displays(args.out); } /// Captures a screenshot of all active displays and writes the resulting image to disk. -fn capture_all_displays(out_dir: String, filename_format: String) { +fn capture_all_displays(filename: String) { let captures: Vec<_> = graphics::Display::get_displays() .iter() .filter_map(|screen| screen.capture().ok()) @@ -25,10 +25,11 @@ fn capture_all_displays(out_dir: String, filename_format: String) { if !img.is_empty() { let timestamp: DateTime = Utc::now().with_timezone(&Local); + const FILENAME_FORMAT: &str = "screenshot-%Y-%m-%d_%H-%M-%S.png"; - let mut path = PathBuf::from(out_dir); + let mut path = PathBuf::from(timestamp.format(&filename).to_string()); if path.is_dir() { - path = path.join(timestamp.format(&filename_format).to_string()); + path = path.join(timestamp.format(FILENAME_FORMAT).to_string()); } match img.save(generate_filename(&path)) { -- libgit2 1.7.2