Krolyxon
Commit f2bd31a

Branches

Tags

Refs →

remagesrccli.rs

88 lines · 2.3 KB · Rust

1//! Command-line interface.
2
3use std::path::PathBuf;
4
5use clap::Parser;
6
7use crate::state::queue::SortKey;
8use crate::state::view::FitMode;
9
10#[derive(Parser, Debug)]
11#[command(name = "remage", version, about = "Keyboard-centric image viewer")]
12pub struct Cli {
13 /// Files or directories to open [default: .]
14 pub paths: Vec<PathBuf>,
15
16 /// Descend into directories
17 #[arg(short, long)]
18 pub recursive: bool,
19
20 /// Start at the Nth image (1-based)
21 #[arg(short = 'n', long, value_name = "N")]
22 pub start: Option<usize>,
23
24 /// Start in fullscreen
25 #[arg(short, long)]
26 pub fullscreen: bool,
27
28 /// Hide the status bar
29 #[arg(short = 'b', long)]
30 pub no_bar: bool,
31
32 /// How images fit the window
33 #[arg(short = 's', long, value_enum, value_name = "MODE")]
34 pub fit: Option<FitMode>,
35
36 /// Fixed zoom level in percent (overrides --fit)
37 #[arg(short = 'z', long, value_name = "PCT")]
38 pub zoom: Option<f32>,
39
40 /// Initial window size in logical pixels
41 #[arg(short = 'g', long, value_name = "WxH", value_parser = parse_geometry)]
42 pub geometry: Option<(u32, u32)>,
43
44 /// Wayland app_id / X11 class
45 #[arg(long, value_name = "NAME", default_value = "remage")]
46 pub class: String,
47
48 /// Sort order for directory contents
49 #[arg(long, value_enum, value_name = "KEY", default_value = "natural")]
50 pub sort: SortKey,
51
52 /// Reverse the sort order
53 #[arg(long)]
54 pub reverse: bool,
55
56 /// Open only the named file, not the rest of its directory
57 #[arg(long)]
58 pub no_siblings: bool,
59}
60
61fn parse_geometry(s: &str) -> Result<(u32, u32), String> {
62 let (w, h) = s
63 .split_once(['x', 'X'])
64 .ok_or("expected WxH, e.g. 1280x800")?;
65 let parse = |v: &str| v.trim().parse::<u32>().ok().filter(|n| *n > 0);
66 match (parse(w), parse(h)) {
67 (Some(w), Some(h)) => Ok((w, h)),
68 _ => Err("expected positive WxH, e.g. 1280x800".into()),
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75 use clap::CommandFactory;
76
77 #[test]
78 fn cli_is_consistent() {
79 Cli::command().debug_assert();
80 }
81
82 #[test]
83 fn geometry() {
84 assert_eq!(parse_geometry("800x600"), Ok((800, 600)));
85 assert!(parse_geometry("800").is_err());
86 assert!(parse_geometry("0x600").is_err());
87 }
88}