Krolyxon
Commit dbc22bf

Branches

Tags

Refs →

remagesrccli.rs

108 lines · 2.8 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 in the thumbnail grid
21 #[arg(short = 't', long)]
22 pub grid: bool,
23
24 /// Read the shared thumbnail cache but never write to it
25 #[arg(short = 'p', long)]
26 pub private: bool,
27
28 /// Start at the Nth image (1-based)
29 #[arg(short = 'n', long, value_name = "N")]
30 pub start: Option<usize>,
31
32 /// Start in fullscreen
33 #[arg(short, long)]
34 pub fullscreen: bool,
35
36 /// Hide the status bar
37 #[arg(short = 'b', long)]
38 pub no_bar: bool,
39
40 /// How images fit the window
41 #[arg(short = 's', long, value_enum, value_name = "MODE")]
42 pub fit: Option<FitMode>,
43
44 /// Fixed zoom level in percent (overrides --fit)
45 #[arg(short = 'z', long, value_name = "PCT")]
46 pub zoom: Option<f32>,
47
48 /// Initial window size in logical pixels
49 #[arg(short = 'g', long, value_name = "WxH", value_parser = parse_geometry)]
50 pub geometry: Option<(u32, u32)>,
51
52 /// Wayland app_id / X11 class [default: remage]
53 #[arg(long, value_name = "NAME")]
54 pub class: Option<String>,
55
56 /// Sort order for directory contents [default: natural]
57 #[arg(long, value_enum, value_name = "KEY")]
58 pub sort: Option<SortKey>,
59
60 /// Reverse the sort order
61 #[arg(long)]
62 pub reverse: bool,
63
64 /// Open only the named file, not the rest of its directory
65 #[arg(long)]
66 pub no_siblings: bool,
67
68 /// Config file [default: $XDG_CONFIG_HOME/remage/config.toml]
69 #[arg(short = 'c', long, value_name = "PATH")]
70 pub config: Option<PathBuf>,
71
72 /// Print the full commented default config and exit
73 #[arg(long)]
74 pub dump_config: bool,
75
76 /// Validate the config and exit non-zero on errors
77 #[arg(long)]
78 pub check_config: bool,
79}
80
81fn parse_geometry(s: &str) -> Result<(u32, u32), String> {
82 let (w, h) = s
83 .split_once(['x', 'X'])
84 .ok_or("expected WxH, e.g. 1280x800")?;
85 let parse = |v: &str| v.trim().parse::<u32>().ok().filter(|n| *n > 0);
86 match (parse(w), parse(h)) {
87 (Some(w), Some(h)) => Ok((w, h)),
88 _ => Err("expected positive WxH, e.g. 1280x800".into()),
89 }
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95 use clap::CommandFactory;
96
97 #[test]
98 fn cli_is_consistent() {
99 Cli::command().debug_assert();
100 }
101
102 #[test]
103 fn geometry() {
104 assert_eq!(parse_geometry("800x600"), Ok((800, 600)));
105 assert!(parse_geometry("800").is_err());
106 assert!(parse_geometry("0x600").is_err());
107 }
108}