Skip to content

Commit

Permalink
Move to clap_derive so CLI arguments are typed
Browse files Browse the repository at this point in the history
* Renames `clap.rs` to `cli.rs` because otherwise there's confusion between the `clap` crate and local module.
* `Cli` struct added that almost identically represents the current state of the CLI with no logical changes.

---

## `--help` Comparison

Before: https://gist.github.com/AndreasBackx/5945b366e989159f4669e7ba30c13239

After:  https://gist.github.com/AndreasBackx/8929c8bde080eac0cafd33128210b0cc

Diff (ignoring whitespace changes due to table alignment):
```diff
1c1
< Screenshot tool for compositors implementing zwlr_screencopy_v1.
---
> Screenshot tool for wlroots based compositors implementing the zwlr_screencopy_v1 protocol.
9d8
<   -c, --cursor                      Enable cursor in screenshots
10a10
>   -c, --cursor                      Enable cursor in screenshots
12c12
<   -l, --listoutputs                 List all valid outputs
---
>   -l, --list-outputs                List all valid outputs
14c14
<       --chooseoutput                Present a fuzzy selector for outputs
---
>       --choose-output               Present a fuzzy selector for outputs
16a17
>
```

You can see that the only changes are:
- About is longer, this is now using the value from Cargo.toml instead of a duplicate text that was shorter.
- Some have a dash where the English words would have a space, e.g: "list-outputs" instead of "listoutputs". I've also made the old still work via an alias: https://gist.github.com/AndreasBackx/6025e91844e3d766d4264a01ae4d1a71

This seems like a tiny improvement? I plan to make further changes later, but I want to keep PRs separate.
  • Loading branch information
AndreasBackx committed Feb 2, 2024
1 parent a023ff1 commit 87be331
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 98 deletions.
31 changes: 25 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wayshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
67 changes: 0 additions & 67 deletions wayshot/src/clap.rs

This file was deleted.

43 changes: 43 additions & 0 deletions wayshot/src/cli.rs
Original file line number Diff line number Diff line change
@@ -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<String>,

/// Mention a custom file path
#[arg(short, long, conflicts_with = "stdout", value_name = "FILE_PATH")]
pub file: Option<String>,

/// 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<String>,

/// 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<String>,

/// Present a fuzzy selector for outputs
#[arg(long, alias = "chooseoutput", conflicts_with_all = ["slurp", "output"], action = clap::ArgAction::SetTrue)]
pub choose_output: bool,
}
40 changes: 16 additions & 24 deletions wayshot/src/wayshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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::<String>("extension") {
let extension = if let Some(extension) = cli.extension {
let ext = extension.trim().to_lowercase();
tracing::debug!("Using custom extension: {:#?}", ext);

Expand All @@ -62,30 +59,25 @@ fn main() -> Result<()> {
let mut file_is_stdout: bool = false;
let mut file_path: Option<String> = None;

if args.get_flag("stdout") {
if cli.stdout {
file_is_stdout = true;
} else if let Some(filepath) = args.get_one::<String>("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));
}

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);
}
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::<String>("slurp") {
let image_buffer = if let Some(slurp_region) = cli.slurp {
let slurp_region = slurp_region.clone();
wayshot_conn.screenshot_freeze(
Box::new(move || {
Expand All @@ -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::<String>("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<String> = 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 {
Expand Down

0 comments on commit 87be331

Please sign in to comment.