diff --git a/Cargo.lock b/Cargo.lock index 500045a8..9244fa4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,9 +10,9 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" [[package]] name = "anstream" -version = "0.6.5" +version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" dependencies = [ "anstyle", "anstyle-parse", @@ -103,18 +103,19 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.4.11" +version = "4.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfaff671f6b22ca62406885ece523383b9b64022e341e53e009a62ebc47a45f2" +checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] name = "clap_builder" -version = "4.4.11" +version = "4.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a216b506622bb1d316cd51328dce24e07bdff4a6128a47c7e7fad11878d5adbb" +checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" dependencies = [ "anstream", "anstyle", @@ -122,6 +123,18 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "clap_lex" version = "0.6.0" @@ -251,6 +264,12 @@ dependencies = [ "thread_local", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "image" version = "0.24.7" diff --git a/wayshot/Cargo.toml b/wayshot/Cargo.toml index e4ac7af3..344654f9 100644 --- a/wayshot/Cargo.toml +++ b/wayshot/Cargo.toml @@ -18,7 +18,7 @@ tracing.workspace = true libwayshot.workspace = true -clap = "4.4.6" +clap = { version = "4.4.18", features = ["derive"] } tracing-subscriber = "0.3.17" image = { version = "0.24", default-features = false, features = [ diff --git a/wayshot/src/clap.rs b/wayshot/src/clap.rs deleted file mode 100644 index dfed67b4..00000000 --- a/wayshot/src/clap.rs +++ /dev/null @@ -1,67 +0,0 @@ -use clap::{arg, ArgAction, Command}; - -pub fn set_flags() -> Command { - Command::new("wayshot") - .version(env!("CARGO_PKG_VERSION")) - .author(env!("CARGO_PKG_AUTHORS")) - .about("Screenshot tool for compositors implementing zwlr_screencopy_v1.") - .arg( - arg!(-d - -debug) - .required(false) - .action(ArgAction::SetTrue) - .help("Enable debug mode"), - ) - .arg( - arg!(-s --slurp ) - .required(false) - .action(ArgAction::Set) - .help("Arguments to call slurp with for selecting a region"), - ) - .arg( - arg!(-f - -file ) - .required(false) - .conflicts_with("stdout") - .action(ArgAction::Set) - .help("Mention a custom file path"), - ) - .arg( - arg!(-c - -cursor) - .required(false) - .action(ArgAction::SetTrue) - .help("Enable cursor in screenshots"), - ) - .arg( - arg!(--stdout) - .required(false) - .conflicts_with("file") - .action(ArgAction::SetTrue) - .help("Output the image data to standard out"), - ) - .arg( - arg!(-e --extension ) - .required(false) - .action(ArgAction::Set) - .help("Set image encoder (Png is default)"), - ) - .arg( - arg!(-l - -listoutputs) - .required(false) - .action(ArgAction::SetTrue) - .help("List all valid outputs"), - ) - .arg( - arg!(-o --output ) - .required(false) - .action(ArgAction::Set) - .conflicts_with("slurp") - .help("Choose a particular display to screenshot"), - ) - .arg( - arg!(--chooseoutput) - .required(false) - .action(ArgAction::SetTrue) - .conflicts_with("slurp") - .conflicts_with("output") - .help("Present a fuzzy selector for outputs"), - ) -} diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs new file mode 100644 index 00000000..182737b2 --- /dev/null +++ b/wayshot/src/cli.rs @@ -0,0 +1,43 @@ +use clap::arg; + +use clap::Parser; + +#[derive(Parser)] +#[command(version, about)] +pub struct Cli { + /// Enable debug mode + #[arg(short, long, action = clap::ArgAction::SetTrue)] + pub debug: bool, + + /// Arguments to call slurp with for selecting a region + #[arg(short, long, value_name = "SLURP_ARGS")] + pub slurp: Option, + + /// Mention a custom file path + #[arg(short, long, conflicts_with = "stdout", value_name = "FILE_PATH")] + pub file: Option, + + /// Output the image data to standard out + #[arg(long, conflicts_with = "file", action = clap::ArgAction::SetTrue)] + pub stdout: bool, + + /// Enable cursor in screenshots + #[arg(short, long, action = clap::ArgAction::SetTrue)] + pub cursor: bool, + + /// Set image encoder (Png is default) + #[arg(short, long, value_name = "FILE_EXTENSION")] + pub extension: Option, + + /// List all valid outputs + #[arg(short, long, alias = "listoutputs", action = clap::ArgAction::SetTrue)] + pub list_outputs: bool, + + /// Choose a particular display to screenshot + #[arg(short, long, conflicts_with = "slurp")] + pub output: Option, + + /// Present a fuzzy selector for outputs + #[arg(long, alias = "chooseoutput", conflicts_with_all = ["slurp", "output"], action = clap::ArgAction::SetTrue)] + pub choose_output: bool, +} diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 4353d579..8e3bc8af 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -3,10 +3,11 @@ use std::{ process::{exit, Command}, }; +use clap::Parser; use eyre::Result; use libwayshot::{region::LogicalRegion, WayshotConnection}; -mod clap; +mod cli; mod utils; use dialoguer::{theme::ColorfulTheme, FuzzySelect}; @@ -30,18 +31,14 @@ where } fn main() -> Result<()> { - let args = clap::set_flags().get_matches(); - let level = if args.get_flag("debug") { - Level::TRACE - } else { - Level::INFO - }; + let cli = cli::Cli::parse(); + let level = if cli.debug { Level::TRACE } else { Level::INFO }; tracing_subscriber::fmt() .with_max_level(level) .with_writer(std::io::stderr) .init(); - let extension = if let Some(extension) = args.get_one::("extension") { + let extension = if let Some(extension) = cli.extension { let ext = extension.trim().to_lowercase(); tracing::debug!("Using custom extension: {:#?}", ext); @@ -62,9 +59,9 @@ fn main() -> Result<()> { let mut file_is_stdout: bool = false; let mut file_path: Option = None; - if args.get_flag("stdout") { + if cli.stdout { file_is_stdout = true; - } else if let Some(filepath) = args.get_one::("file") { + } else if let Some(filepath) = cli.file { file_path = Some(filepath.trim().to_string()); } else { file_path = Some(utils::get_default_file_name(extension)); @@ -72,7 +69,7 @@ fn main() -> Result<()> { let wayshot_conn = WayshotConnection::new()?; - if args.get_flag("listoutputs") { + if cli.list_outputs { let valid_outputs = wayshot_conn.get_all_outputs(); for output in valid_outputs { tracing::info!("{:#?}", output.name); @@ -80,12 +77,7 @@ fn main() -> Result<()> { exit(1); } - let mut cursor_overlay = false; - if args.get_flag("cursor") { - cursor_overlay = true; - } - - let image_buffer = if let Some(slurp_region) = args.get_one::("slurp") { + let image_buffer = if let Some(slurp_region) = cli.slurp { let slurp_region = slurp_region.clone(); wayshot_conn.screenshot_freeze( Box::new(move || { @@ -99,30 +91,30 @@ fn main() -> Result<()> { }() .map_err(|_| libwayshot::Error::FreezeCallbackError) }), - cursor_overlay, + cli.cursor, )? - } else if let Some(output_name) = args.get_one::("output") { + } else if let Some(output_name) = cli.output { let outputs = wayshot_conn.get_all_outputs(); - if let Some(output) = outputs.iter().find(|output| &output.name == output_name) { - wayshot_conn.screenshot_single_output(output, cursor_overlay)? + if let Some(output) = outputs.iter().find(|output| output.name == output_name) { + wayshot_conn.screenshot_single_output(output, cli.cursor)? } else { tracing::error!("No output found!\n"); exit(1); } - } else if args.get_flag("chooseoutput") { + } else if cli.choose_output { let outputs = wayshot_conn.get_all_outputs(); let output_names: Vec = outputs .iter() .map(|display| display.name.to_string()) .collect(); if let Some(index) = select_ouput(&output_names) { - wayshot_conn.screenshot_single_output(&outputs[index], cursor_overlay)? + wayshot_conn.screenshot_single_output(&outputs[index], cli.cursor)? } else { tracing::error!("No output found!\n"); exit(1); } } else { - wayshot_conn.screenshot_all(cursor_overlay)? + wayshot_conn.screenshot_all(cli.cursor)? }; if file_is_stdout {