Skip to main content

tess/
cli.rs

1use std::path::PathBuf;
2use clap::Parser;
3use clap::builder::styling::{AnsiColor, Color, Style};
4use clap::builder::Styles;
5
6const HELP_STYLES: Styles = Styles::styled()
7    .header(Style::new().bold().fg_color(Some(Color::Ansi(AnsiColor::Yellow))))
8    .usage(Style::new().bold().fg_color(Some(Color::Ansi(AnsiColor::Yellow))))
9    .literal(Style::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan))))
10    .placeholder(Style::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan))));
11
12#[derive(Parser, Debug, Clone)]
13#[command(name = "tess", version, about = "A less-style terminal pager.", styles = HELP_STYLES)]
14// Repeated scalar flags take the last value rather than erroring. This makes
15// `less`-style "last flag wins" work and, crucially, lets a CLI flag override
16// the same flag injected by a group expansion (e.g. `--mygroup --display ...`
17// overriding the group's own `display`). Repeatable flags (`--filter`,
18// `--grep`) use Append actions and are unaffected — they still accumulate.
19#[command(args_override_self = true)]
20// Fields are ordered alphabetically by long-flag name (case-insensitive) so
21// that `--help` lists them in that order — clap's derive renders options in
22// field-declaration order. The `help_lists_flags_in_alphabetical_order` test
23// enforces this; keep new flags in their sorted slot.
24//
25// `IGNORE_CASE` (-I) and `QUIT_AT_EOF` (-E) are intentionally upper-case to
26// mirror less's case-distinguished flag pairs (`-i`/`-I`, `-e`/`-E`) and to
27// stay distinct from their lower-case `ignore_case`/`quit_at_eof` siblings.
28#[allow(non_snake_case)]
29pub struct Args {
30    /// Render images with Unicode half-blocks (▀, fg=top pixel, bg=bottom
31    /// pixel) for ~2× vertical detail instead of the default character ramp.
32    #[arg(long = "blocks")]
33    pub blocks: bool,
34
35    /// Chop long lines instead of wrapping.
36    #[arg(short = 'S', long = "chop-long-lines")]
37    pub chop: bool,
38
39    /// Enable interactive clipboard yank: `:yank` copies the current line to the
40    /// system clipboard. Bind a key via `clipboard-yank-line` in keys.toml
41    /// (unbound by default, so it never clobbers existing keys).
42    #[arg(long = "clipboard")]
43    pub clipboard: bool,
44
45    /// Force the content type for `--prettify` (otherwise auto-detected from
46    /// the filename extension and the first bytes). Values:
47    /// `auto`, `raw`, `json`, `yaml`, `toml`, `xml`, `html`, `csv`.
48    /// Setting this implies `--prettify` (unless the value is `raw`/`auto`).
49    #[arg(long = "content-type", value_name = "TYPE")]
50    pub content_type: Option<String>,
51
52    /// With `--filter`, dim non-matching lines instead of hiding them. Keeps
53    /// surrounding context visible.
54    #[arg(long = "dim")]
55    pub dim: bool,
56
57    /// Render each parsed line through this template instead of showing the
58    /// raw line. Syntax: `<fieldname>` placeholders, `\<` for literal `<`,
59    /// `\\` for literal `\`. Example: `--display '[<time>] <status> <msg>'`.
60    /// Overrides the format's `display` key (if set). Requires `--format`.
61    /// Search still matches against the raw line.
62    #[arg(long = "display", value_name = "TEMPLATE")]
63    pub display: Option<String>,
64
65    /// Print a curated list of usage examples and exit.
66    #[arg(long = "examples")]
67    pub examples: bool,
68
69    /// In follow mode with piped stdin, exit when the upstream writer
70    /// closes the pipe. Default behavior (off): tess remains open on
71    /// the captured content after stdin EOF. Mirrors
72    /// `less --exit-follow-on-close`.
73    #[arg(long = "exit-follow-on-close")]
74    pub exit_follow_on_close: bool,
75
76    /// Filter visible lines by parsed field. Repeatable; multiple filters AND.
77    /// Operators: `=` (exact), `!=` (exact ≠), `~` (regex), `!~` (regex ≠),
78    /// `<`, `<=`, `>`, `>=` (numeric if both sides parse as numbers, else
79    /// lexicographic). Examples: `--filter status=500`, `--filter ip~^10\.`,
80    /// `--filter 'status>=500'` (quote `<` and `>` to avoid shell redirection).
81    /// Requires `--format`.
82    #[arg(long = "filter", value_name = "FIELD<op>VALUE")]
83    pub filter: Vec<String>,
84
85    /// Follow mode: keep watching the source for new bytes (like `tail -f`).
86    /// Jumps to the bottom on startup. Toggle with Shift-F at runtime.
87    #[arg(short = 'f', long = "follow")]
88    pub follow: bool,
89
90    /// Follow the file by path rather than by descriptor (matches
91    /// `tail -F` / `less --follow-name`). `tess` already does this —
92    /// rotation and truncation are detected on every poll and the
93    /// source re-opens by path (since 0.25.0). This flag is accepted
94    /// for compatibility and currently has no behavioral effect.
95    #[arg(long = "follow-name")]
96    pub follow_name: bool,
97
98    /// In follow mode, any user motion (scroll, page, goto-line) suspends
99    /// following. Re-engage with Shift-F. Default off: today's behavior
100    /// (movement keeps follow on; auto-scroll suspended while the viewport
101    /// is not at bottom). Matches `less +F` semantics when enabled.
102    #[arg(long = "follow-suspend-on-motion")]
103    pub follow_suspend_on_motion: bool,
104
105    /// Apply a named log format (built-in or user-defined in
106    /// ~/.config/tess/formats.toml). Required by `--filter`.
107    #[arg(long = "format", value_name = "NAME")]
108    pub format: Option<String>,
109
110    /// Use the system clipboard contents as input (like `pbpaste | tess`).
111    /// Mutually exclusive with file arguments and piped stdin.
112    #[arg(long = "from-clipboard", conflicts_with = "files")]
113    pub from_clipboard: bool,
114
115    /// Filter visible lines by regex against the raw line. Repeatable;
116    /// multiple `--grep` arguments AND. Works on any input — no `--format`
117    /// required. Composes with `--filter` (both must match) and with
118    /// `--dim` (non-matches stay visible but faded).
119    /// Example: `--grep error --grep '^\['`.
120    #[arg(long = "grep", value_name = "PATTERN")]
121    pub grep: Vec<String>,
122
123    /// Show only the first N lines of the source. Mutually exclusive with --tail.
124    #[arg(long = "head", value_name = "N", conflicts_with = "tail")]
125    pub head: Option<usize>,
126
127    /// Pin the top L source lines (and the left C columns, when
128    /// horizontal scroll is supported) at the top of the viewport.
129    /// Form: `L` or `L,C`. Default `0,0` (off). Mirrors `less --header`.
130    /// Runtime adjustment: `:header L [C]`.
131    #[arg(long = "header", value_name = "L[,C]")]
132    pub header: Option<String>,
133
134    /// Render the source as an xxd-style hex dump instead of byte-faithful
135    /// text. 16 bytes per row, offset prefix, ASCII gutter. Mutually
136    /// exclusive with parsing- and rendering-oriented flags.
137    #[arg(
138        long = "hex",
139        conflicts_with_all = ["filter", "grep", "prettify", "format", "display", "record_start", "prompt", "preprocess", "or_filter", "or_grep", "or_group"],
140    )]
141    pub hex: bool,
142
143    /// Hex characters per group in `--hex` mode. One of 2, 4, 8, 16, 32
144    /// (default 4, matching `xxd`). 32 means the whole row as a single
145    /// group with no spacing between hex pairs. Requires `--hex`. Can be
146    /// changed at runtime with `:hex N`.
147    #[arg(
148        long = "hex-group",
149        value_name = "N",
150        default_value_t = 4,
151        requires = "hex",
152    )]
153    pub hex_group: usize,
154
155    /// Smart-case search. `/`, `?`, `--grep`, and `--filter`'s `~` / `!~`
156    /// operators match case-insensitively unless the pattern contains an
157    /// uppercase character. Mirrors `less -i` / ripgrep / vim smartcase.
158    /// Mutually exclusive with `-I`. Runtime toggle: `:case`.
159    #[arg(short = 'i', long = "ignore-case", conflicts_with = "IGNORE_CASE")]
160    pub ignore_case: bool,
161
162    /// Force case-insensitive search regardless of pattern case. Mirrors
163    /// `less -I`. Mutually exclusive with `-i`.
164    #[arg(short = 'I', long = "IGNORE-CASE")]
165    pub IGNORE_CASE: bool,
166
167    /// Image rendering protocol: `auto` (detect terminal graphics support and
168    /// fall back to ASCII), `kitty`, `sixel`, or `ascii`.
169    #[arg(long = "image-protocol", value_name = "MODE", default_value = "auto")]
170    pub image_protocol: String,
171
172    /// Target width in columns for image rendering. Defaults to the terminal
173    /// width interactively, or 80 when exporting to a file/stdout.
174    #[arg(long = "image-width", value_name = "N")]
175    pub image_width: Option<usize>,
176
177    /// Incremental search: in the `/`/`?` prompt, jump to and highlight the
178    /// first match as you type (from where the prompt opened). Esc restores the
179    /// original position; Enter commits. Default off. Mirrors `less --incsearch`.
180    /// Runtime toggle: `:incsearch`.
181    #[arg(long = "incsearch")]
182    pub incsearch: bool,
183
184    /// Show line numbers.
185    #[arg(short = 'N', long = "LINE-NUMBERS")]
186    pub line_numbers: bool,
187
188    /// Print available log formats and their named fields, then exit.
189    #[arg(long = "list-formats")]
190    pub list_formats: bool,
191
192    /// Live mode: re-read the file when its on-disk content changes (mtime,
193    /// size, or inode). Use this for files rewritten in place — source files
194    /// being edited, files saved by an editor or AI agent. Different from
195    /// `--follow` (which watches for *appended* bytes); the two are mutually
196    /// exclusive. Press `R` inside the pager to force a reload.
197    #[arg(long = "live", conflicts_with = "follow")]
198    pub live: bool,
199
200    /// Print the full user manual and exit.
201    #[arg(long = "manual")]
202    pub manual: bool,
203
204    /// Enable mouse capture: click rows in the file picker / help overlay,
205    /// and scrollwheel scrolls the body. Trade-off: most terminals disable
206    /// their native text selection while mouse capture is on.
207    #[arg(long = "mouse")]
208    pub mouse: bool,
209
210    /// Show only the first frame of an animated image (GIF/APNG/WebP) instead
211    /// of playing it. No effect on static images or non-image input.
212    #[arg(long = "no-animate")]
213    pub no_animate: bool,
214
215    /// Show raw control bytes as `^X` glyphs (pre-0.18 default). Disables
216    /// SGR / OSC interpretation. Honoured also by the `NO_COLOR` environment
217    /// variable (any non-empty value) and `CLICOLOR=0`.
218    #[arg(long = "no-color")]
219    pub no_color: bool,
220
221    /// Disable search-match highlighting by default. Search still
222    /// navigates (`n` / `N` jump to matches); the visual reverse-video
223    /// highlight is suppressed. Runtime toggle: `:hlsearch` / `:nohlsearch`.
224    /// Mirrors `less -G`.
225    #[arg(short = 'G', long = "no-hilite-search")]
226    pub no_hilite_search: bool,
227
228    /// Treat a detected image file as raw/normal text instead of rendering it
229    /// as ASCII art. Has no effect on non-image inputs.
230    #[arg(long = "no-image")]
231    pub no_image: bool,
232
233    /// Don't enter the alt-screen on startup. Content remains in
234    /// terminal scrollback after exit. Crucial for piped use and
235    /// debugging. Mirrors `less -X` / `--no-init`.
236    #[arg(short = 'X', long = "no-init")]
237    pub no_init: bool,
238
239    /// Ignore $LESSOPEN. Useful when LESSOPEN is exported but not wanted
240    /// for one invocation.
241    #[arg(long = "no-preprocess", conflicts_with = "preprocess")]
242    pub no_preprocess: bool,
243
244    /// OR-filter: a field condition where matching ANY condition in its
245    /// OR-group is enough (the group is satisfied). AND'd with the required
246    /// --filter/--grep. Joins the group set by the most recent --or-group, or
247    /// `default` if none. Requires --format. Repeatable.
248    #[arg(long = "or-filter", value_name = "FIELD<op>VALUE")]
249    pub or_filter: Vec<String>,
250
251    /// OR-grep: a raw-regex condition where matching ANY condition in its
252    /// OR-group is enough. Works on any input. Joins the group set by the most
253    /// recent --or-group, or `default` if none. Repeatable.
254    #[arg(long = "or-grep", value_name = "PATTERN")]
255    pub or_grep: Vec<String>,
256
257    /// Open an OR-group: subsequent --or-filter/--or-grep join NAME until the
258    /// next --or-group. Conditions before any marker form the `default` group.
259    /// Every non-empty group must have ≥1 match (groups are AND'd). Repeatable.
260    #[arg(long = "or-group", value_name = "NAME")]
261    pub or_group: Vec<String>,
262
263    /// Non-interactive batch mode: apply --filter / --grep / --head / --tail / --prettify
264    /// to the source and write the resulting raw bytes to FILE, then exit.
265    /// Use `-` for stdout (`--stdout` is a synonym). Skips the alt-screen and
266    /// raw mode entirely. With `--follow`, doesn't exit — keeps appending
267    /// matching new bytes to FILE as they arrive (Ctrl-C to stop). Not
268    /// compatible with `--live`.
269    #[arg(short = 'o', long = "output", value_name = "FILE")]
270    pub output: Option<String>,
271
272    /// Pipe the source file through this command before rendering.
273    /// Must start with `|`; `%s` is substituted with the file path.
274    /// Example: `--preprocess '|pdftotext %s -'`. Overrides $LESSOPEN.
275    #[arg(
276        long = "preprocess",
277        value_name = "CMD",
278        conflicts_with_all = ["no_preprocess", "hex", "follow", "live"],
279    )]
280    pub preprocess: Option<String>,
281
282    /// Pretty-print structured content (JSON, YAML, TOML, XML, HTML, CSV).
283    /// Detects the type from the filename extension or the first bytes; use
284    /// `--content-type=NAME` to override. Static files only — not allowed
285    /// with `--follow`, `--live`, or `--filter`. Toggle interactively with
286    /// `Shift-P`; force a type with `-P` then a letter (j/y/t/x/h/c).
287    #[arg(long = "prettify")]
288    pub prettify: bool,
289
290    /// Replace the hardcoded status format with a templated string.
291    /// Uses the same `<field>` syntax as `--display`. Available fields:
292    /// label, top, bottom, total, pct, rec-top, rec-bottom, rec-total,
293    /// rec-block, wrap-offset, format-tag, filter-tag, grep-tag,
294    /// hide-tag, search-tag, pretty-tag, live-tag, follow-tag.
295    /// Per-format default can be set via `prompt = '...'` in formats.toml.
296    /// Mutually exclusive with --hex.
297    #[arg(long = "prompt", value_name = "TEMPLATE", conflicts_with = "hex")]
298    pub prompt: Option<String>,
299
300    /// Style for `--prompt` output (and per-format `prompt_style`). Same
301    /// grammar as `--status-style`. Default: empty (no extra styling on top
302    /// of what the prompt template itself emits).
303    #[arg(long = "prompt-style", value_name = "SPEC", default_value = "")]
304    pub prompt_style: String,
305
306    /// Quit when the user tries to scroll forward past end-of-file for
307    /// the second time. Mirrors `less -e`. Mutually exclusive with `-E`.
308    #[arg(short = 'e', long = "quit-at-eof", conflicts_with = "QUIT_AT_EOF")]
309    pub quit_at_eof: bool,
310
311    /// Quit the first time end-of-file is reached. Mirrors `less -E`.
312    #[arg(short = 'E', long = "QUIT-AT-EOF")]
313    pub QUIT_AT_EOF: bool,
314
315    /// Exit immediately (without paging) if the entire source fits on
316    /// one screen. Ignored with piped stdin in follow mode. Mirrors
317    /// `less -F`.
318    #[arg(short = 'F', long = "quit-if-one-screen")]
319    pub quit_if_one_screen: bool,
320
321    /// Accepted for `less` compatibility. tess always exits on Ctrl-C
322    /// (Ctrl-C → Command::Quit in the input table), so this flag is a
323    /// no-op. Provided so existing `less` invocations work unchanged.
324    #[arg(short = 'K', long = "quit-on-intr")]
325    pub quit_on_intr: bool,
326
327    /// Pass every byte to the terminal raw, including cursor moves and
328    /// non-SGR escape sequences. Risky: scroll math may break on long lines.
329    /// Less-style -r. Mutually exclusive with --no-color.
330    #[arg(short = 'r', long = "raw-control-chars", conflicts_with = "no_color")]
331    pub raw_control_chars: bool,
332
333    /// Accept `less -R`: interpret SGR/OSC color escapes, strip other control
334    /// sequences — which is tess's default. Provided for drop-in `less -R`
335    /// compatibility. Distinct from `-r` (full raw) and `--no-color`.
336    #[arg(short = 'R', long = "RAW-CONTROL-CHARS",
337        conflicts_with_all = ["raw_control_chars", "no_color"])]
338    pub RAW_CONTROL_CHARS: bool,
339
340    /// Treat lines matching REGEX as record boundaries. Lines that don't
341    /// match are joined to the preceding record. Affects search, filter,
342    /// grep, and the status line — all operate on whole records when set.
343    /// Overrides the active --format's record_start if both are present.
344    /// Without --format, this is the only way to enable records mode for
345    /// plain text. Example: --record-start '^\['
346    #[arg(long = "record-start", value_name = "REGEX")]
347    pub record_start: Option<String>,
348
349    /// Character to show at the right edge of a chopped line (`-S` chop
350    /// mode) indicating "more content right". Default `>`. Pass an empty
351    /// string to disable. Mirrors `less --rscroll=c`.
352    #[arg(long = "rscroll", value_name = "CHAR", default_value = ">")]
353    pub rscroll: String,
354
355    /// Column count for the ←/→ horizontal-scroll commands (default: half
356    /// screen). `0` keeps the half-screen default. Mirrors `less -#`/`--shift`.
357    #[arg(short = '#', long = "shift", value_name = "N")]
358    pub shift: Option<u16>,
359
360    /// Open two files side by side in a vertical split (needs ≥2 file args; with
361    /// one file, opens a second view of it). Interactive only. Runtime: `:vsplit`.
362    #[arg(long = "split")]
363    pub split: bool,
364
365    /// Collapse runs of two or more consecutive blank lines into a
366    /// single blank line at display time. Real line numbers, search,
367    /// and tag jumps are unaffected (they reference the original
368    /// count). Mirrors `less -s`.
369    #[arg(short = 's', long = "squeeze-blank-lines")]
370    pub squeeze_blanks: bool,
371
372    /// Show a 1-column status gutter at the far left: a mark letter on marked
373    /// lines, else `*` on lines containing a current-search match. Stays fixed
374    /// under horizontal scroll. No-op in --hex/-r/image modes. Mirrors `less -J`.
375    #[arg(short = 'J', long = "status-column")]
376    pub status_column: bool,
377
378    /// Style for the status row. Comma-separated tokens: `bold`, `dim`,
379    /// `italic`, `underline`, `reverse`, `fg=COLOR`, `bg=COLOR`. COLOR is a
380    /// named color (`black`..`white`, optional `bright-` prefix), `#RRGGBB`,
381    /// or an indexed value (0–255). Empty string disables theming.
382    /// Default: `reverse`.
383    #[arg(long = "status-style", value_name = "SPEC", default_value = "reverse")]
384    pub status_style: String,
385
386    /// Synonym for `--output -`: write the batch-mode output to stdout.
387    #[arg(long = "stdout", conflicts_with = "output")]
388    pub stdout: bool,
389
390    /// Tab stop width (default 8).
391    #[arg(long = "tab-width", default_value_t = 8)]
392    pub tab_width: u8,
393
394    /// Tab stops as a comma-separated column list, e.g. `-x4,8,16`. A single
395    /// value is equivalent to `--tab-width`. With multiple values, tabs advance
396    /// to the next listed column; past the last stop the final interval
397    /// repeats. Overrides `--tab-width`. Mirrors `less -x`.
398    #[arg(short = 'x', long = "tabs", value_name = "LIST")]
399    pub tabs: Option<String>,
400
401    /// Jump to the tag NAME at startup (requires a tags file).
402    #[arg(short = 't', long = "tag", value_name = "NAME")]
403    pub tag: Option<String>,
404
405    /// Path to the tags file. Default: walk up from CWD looking for `tags`.
406    #[arg(short = 'T', long = "tag-file", value_name = "PATH")]
407    pub tag_file: Option<std::path::PathBuf>,
408
409    /// Show only the last N lines of the source. For files this skips most of
410    /// the index work — useful for huge logs. Combine with `-f` for `tail -f`.
411    /// Mutually exclusive with --head. Streaming stdin is not supported.
412    #[arg(long = "tail", value_name = "N", conflicts_with = "head")]
413    pub tail: Option<usize>,
414
415    /// Batch sink: apply filters/head/tail/prettify and copy the result to the
416    /// system clipboard, then exit. Mutually exclusive with -o/--stdout.
417    #[arg(long = "to-clipboard", conflicts_with_all = ["output", "stdout"])]
418    pub to_clipboard: bool,
419
420    /// Truecolor (24-bit RGB) handling. `auto` (default) checks `$COLORTERM`
421    /// and downsamples when truecolor isn't advertised; `never` always
422    /// downsamples to the 256-color palette; `always` passes RGB through
423    /// regardless of terminal capability.
424    #[arg(long = "truecolor", value_name = "MODE", default_value = "auto")]
425    pub truecolor: String,
426
427    /// Body lines scrolled per mouse-wheel notch under `--mouse` (default 3).
428    #[arg(long = "wheel-lines", value_name = "N")]
429    pub wheel_lines: Option<u16>,
430
431    /// PageDown / PageUp step size in lines. Default: full screen
432    /// height (body rows). Half-page commands always advance by half
433    /// the screen regardless. Mirrors `less -zn` / `--window=n`.
434    #[arg(short = 'z', long = "window", value_name = "N")]
435    pub window: Option<u16>,
436
437    /// In wrap mode, break lines on whitespace boundaries instead of
438    /// mid-character when possible. Falls back to mid-character break
439    /// when no whitespace fits in the row. Mirrors `less --wordwrap`.
440    #[arg(long = "wordwrap")]
441    pub word_wrap: bool,
442
443    /// Files to view (only the first is opened in MVP).
444    pub files: Vec<PathBuf>,
445}
446
447#[cfg(test)]
448mod tests {
449    use super::*;
450
451    #[test]
452    fn parses_no_flags_no_files() {
453        let a = Args::parse_from(["tess"]);
454        assert!(!a.line_numbers);
455        assert!(!a.chop);
456        assert_eq!(a.tab_width, 8);
457        assert!(a.files.is_empty());
458    }
459
460    #[test]
461    fn parses_raw_control_chars_alias() {
462        let a = Args::parse_from(["tess", "-R", "f"]);
463        assert!(a.RAW_CONTROL_CHARS);
464    }
465    #[test]
466    fn raw_control_chars_conflicts_with_raw_and_no_color() {
467        assert!(Args::try_parse_from(["tess", "-R", "-r", "f"]).is_err());
468        assert!(Args::try_parse_from(["tess", "-R", "--no-color", "f"]).is_err());
469    }
470    #[test]
471    fn parses_split() {
472        assert!(Args::parse_from(["tess", "--split", "a", "b"]).split);
473        assert!(!Args::parse_from(["tess", "a"]).split);
474    }
475
476    #[test]
477    fn parses_shift_and_wheel_lines() {
478        let a = Args::parse_from(["tess", "--shift", "12", "--wheel-lines", "5", "f"]);
479        assert_eq!(a.shift, Some(12));
480        assert_eq!(a.wheel_lines, Some(5));
481    }
482
483    #[test]
484    fn parses_short_flags_and_file() {
485        let a = Args::parse_from(["tess", "-N", "-S", "foo.txt"]);
486        assert!(a.line_numbers);
487        assert!(a.chop);
488        assert_eq!(a.files, vec![PathBuf::from("foo.txt")]);
489    }
490
491    #[test]
492    fn parses_tab_width() {
493        let a = Args::parse_from(["tess", "--tab-width", "4", "x"]);
494        assert_eq!(a.tab_width, 4);
495    }
496
497    #[test]
498    fn parses_tabs_list() {
499        let a = Args::parse_from(["tess", "--tabs", "4,8,16", "f"]);
500        assert_eq!(a.tabs.as_deref(), Some("4,8,16"));
501        let b = Args::parse_from(["tess", "-x", "4", "f"]);
502        assert_eq!(b.tabs.as_deref(), Some("4"));
503    }
504
505    #[test]
506    fn collects_multiple_files() {
507        let a = Args::parse_from(["tess", "a", "b", "c"]);
508        assert_eq!(a.files.len(), 3);
509    }
510
511    #[test]
512    fn parses_follow_short_flag() {
513        let a = Args::parse_from(["tess", "-f", "log.txt"]);
514        assert!(a.follow);
515        assert_eq!(a.files, vec![PathBuf::from("log.txt")]);
516    }
517
518    #[test]
519    fn parses_follow_long_flag() {
520        let a = Args::parse_from(["tess", "--follow"]);
521        assert!(a.follow);
522    }
523
524    #[test]
525    fn follow_defaults_off() {
526        let a = Args::parse_from(["tess", "x"]);
527        assert!(!a.follow);
528    }
529
530    #[test]
531    fn parses_head() {
532        let a = Args::parse_from(["tess", "--head", "100", "x"]);
533        assert_eq!(a.head, Some(100));
534        assert_eq!(a.tail, None);
535    }
536
537    #[test]
538    fn parses_tail() {
539        let a = Args::parse_from(["tess", "--tail", "50", "x"]);
540        assert_eq!(a.tail, Some(50));
541        assert_eq!(a.head, None);
542    }
543
544    #[test]
545    fn head_and_tail_are_mutually_exclusive() {
546        let r = Args::try_parse_from(["tess", "--head", "10", "--tail", "20", "x"]);
547        assert!(r.is_err(), "clap should reject combining --head and --tail");
548    }
549
550    #[test]
551    fn head_tail_default_to_none() {
552        let a = Args::parse_from(["tess", "x"]);
553        assert!(a.head.is_none());
554        assert!(a.tail.is_none());
555    }
556
557    #[test]
558    fn parses_grep_repeatable_and_no_format_required() {
559        let a = Args::parse_from([
560            "tess",
561            "--grep", "error",
562            "--grep", r"^\[",
563            "log",
564        ]);
565        assert_eq!(a.grep.len(), 2);
566        assert_eq!(a.grep[0], "error");
567        assert_eq!(a.grep[1], r"^\[");
568        assert_eq!(a.format, None);
569    }
570
571    #[test]
572    fn parses_format_and_filter() {
573        let a = Args::parse_from([
574            "tess", "--format", "apache-combined",
575            "--filter", "status=500",
576            "--filter", "ip~^10\\.",
577            "log",
578        ]);
579        assert_eq!(a.format.as_deref(), Some("apache-combined"));
580        assert_eq!(a.filter.len(), 2);
581        assert_eq!(a.filter[0], "status=500");
582    }
583
584    #[test]
585    fn parses_dim() {
586        let a = Args::parse_from(["tess", "--format", "x", "--filter", "y=z", "--dim", "f"]);
587        assert!(a.dim);
588    }
589
590    #[test]
591    fn parses_list_formats() {
592        let a = Args::parse_from(["tess", "--list-formats"]);
593        assert!(a.list_formats);
594    }
595
596    #[test]
597    fn parses_manual() {
598        let a = Args::parse_from(["tess", "--manual"]);
599        assert!(a.manual);
600    }
601
602    #[test]
603    fn parses_examples() {
604        let a = Args::parse_from(["tess", "--examples"]);
605        assert!(a.examples);
606    }
607
608    #[test]
609    fn parses_live() {
610        let a = Args::parse_from(["tess", "--live", "f"]);
611        assert!(a.live);
612        assert!(!a.follow);
613    }
614
615    #[test]
616    fn live_and_follow_are_mutually_exclusive() {
617        let r = Args::try_parse_from(["tess", "--live", "--follow", "f"]);
618        assert!(r.is_err(), "clap should reject combining --live and --follow");
619    }
620
621    #[test]
622    fn parses_prettify() {
623        let a = Args::parse_from(["tess", "--prettify", "f.json"]);
624        assert!(a.prettify);
625        assert_eq!(a.content_type, None);
626    }
627
628    #[test]
629    fn parses_content_type() {
630        let a = Args::parse_from(["tess", "--content-type", "json", "f"]);
631        assert_eq!(a.content_type.as_deref(), Some("json"));
632    }
633
634    #[test]
635    fn parses_output_long_and_short() {
636        let a = Args::parse_from(["tess", "-o", "/tmp/out.txt", "f"]);
637        assert_eq!(a.output.as_deref(), Some("/tmp/out.txt"));
638        let b = Args::parse_from(["tess", "--output", "/tmp/out.txt", "f"]);
639        assert_eq!(b.output.as_deref(), Some("/tmp/out.txt"));
640    }
641
642    #[test]
643    fn parses_status_column() {
644        let a = Args::parse_from(["tess", "-J", "f"]);
645        assert!(a.status_column);
646        let b = Args::parse_from(["tess", "--status-column", "f"]);
647        assert!(b.status_column);
648        let c = Args::parse_from(["tess", "f"]);
649        assert!(!c.status_column, "status_column defaults off");
650    }
651
652    #[test]
653    fn parses_stdout_flag() {
654        let a = Args::parse_from(["tess", "--stdout", "f"]);
655        assert!(a.stdout);
656        assert_eq!(a.output, None);
657    }
658
659    #[test]
660    fn output_and_stdout_are_mutually_exclusive() {
661        let r = Args::try_parse_from(["tess", "-o", "x", "--stdout", "f"]);
662        assert!(r.is_err(), "clap should reject combining --output and --stdout");
663    }
664
665    #[test]
666    fn parses_mouse_flag() {
667        let a = Args::parse_from(["tess", "--mouse", "f"]);
668        assert!(a.mouse);
669    }
670
671    #[test]
672    fn mouse_defaults_off() {
673        let a = Args::parse_from(["tess", "f"]);
674        assert!(!a.mouse);
675    }
676
677    #[test]
678    fn parses_no_animate() {
679        assert!(Args::parse_from(["tess", "--no-animate", "x.gif"]).no_animate);
680        assert!(!Args::parse_from(["tess", "x.gif"]).no_animate);
681    }
682
683    #[test]
684    fn parses_no_image_flag() {
685        let a = Args::parse_from(["tess", "--no-image", "cat.png"]);
686        assert!(a.no_image);
687    }
688
689    #[test]
690    fn parses_blocks_flag() {
691        let a = Args::parse_from(["tess", "--blocks", "cat.png"]);
692        assert!(a.blocks);
693    }
694
695    #[test]
696    fn parses_image_protocol() {
697        assert_eq!(Args::parse_from(["tess", "f"]).image_protocol, "auto");
698        assert_eq!(Args::parse_from(["tess", "--image-protocol", "kitty", "f"]).image_protocol, "kitty");
699    }
700
701    #[test]
702    fn parses_image_width() {
703        let a = Args::parse_from(["tess", "--image-width", "120", "cat.png"]);
704        assert_eq!(a.image_width, Some(120));
705    }
706
707    #[test]
708    fn parses_incsearch() {
709        let a = Args::parse_from(["tess", "--incsearch", "f"]);
710        assert!(a.incsearch);
711    }
712
713    #[test]
714    fn incsearch_defaults_off() {
715        let a = Args::parse_from(["tess", "f"]);
716        assert!(!a.incsearch);
717    }
718
719    #[test]
720    fn image_flags_default_off() {
721        let a = Args::parse_from(["tess", "f"]);
722        assert!(!a.no_image);
723        assert!(!a.blocks);
724        assert_eq!(a.image_width, None);
725    }
726
727    #[test]
728    fn repeated_scalar_flag_takes_last_value() {
729        // args_override_self: a group can inject `--display X` and a later CLI
730        // `--display Y` wins instead of clap erroring "cannot be used multiple
731        // times". Also covers plain `less`-style last-wins.
732        let a = Args::parse_from(["tess", "--display", "X", "--display", "Y", "--format", "f"]);
733        assert_eq!(a.display.as_deref(), Some("Y"));
734        let b = Args::parse_from(["tess", "--tail", "5", "--tail", "1", "x"]);
735        assert_eq!(b.tail, Some(1));
736    }
737
738    #[test]
739    fn repeatable_flags_still_accumulate_with_override_self() {
740        // args_override_self must not collapse Append-action Vec flags.
741        let a = Args::parse_from(["tess", "--grep", "a", "--grep", "b", "x"]);
742        assert_eq!(a.grep, vec!["a".to_string(), "b".to_string()]);
743    }
744
745    #[test]
746    fn parses_or_flags_repeatable() {
747        let a = Args::parse_from([
748            "tess",
749            "--or-grep", "failed",
750            "--or-group", "svc",
751            "--or-filter", "lvl=ERROR",
752            "x",
753        ]);
754        assert_eq!(a.or_grep, vec!["failed".to_string()]);
755        assert_eq!(a.or_group, vec!["svc".to_string()]);
756        assert_eq!(a.or_filter, vec!["lvl=ERROR".to_string()]);
757    }
758
759    #[test]
760    fn or_flags_conflict_with_hex() {
761        let r = Args::try_parse_from(["tess", "--hex", "--or-grep", "x", "f"]);
762        assert!(r.is_err(), "clap should reject --hex with --or-grep");
763    }
764
765    #[test]
766    fn parses_clipboard_flags() {
767        assert!(Args::parse_from(["tess", "--clipboard", "f"]).clipboard);
768        assert!(Args::parse_from(["tess", "--from-clipboard"]).from_clipboard);
769        assert!(Args::parse_from(["tess", "--to-clipboard", "f"]).to_clipboard);
770    }
771    #[test]
772    fn clipboard_sink_conflicts() {
773        assert!(Args::try_parse_from(["tess", "--to-clipboard", "--stdout", "f"]).is_err());
774        assert!(Args::try_parse_from(["tess", "--to-clipboard", "-o", "x", "f"]).is_err());
775    }
776    #[test]
777    fn from_clipboard_conflicts_with_files() {
778        assert!(Args::try_parse_from(["tess", "--from-clipboard", "f"]).is_err());
779    }
780
781    #[test]
782    fn help_lists_flags_in_alphabetical_order() {
783        use clap::CommandFactory;
784        let mut cmd = Args::command();
785        let help = cmd.render_help().to_string();
786
787        // The full set of long flags, in the order we expect `--help` to list
788        // them: alphabetical by long name, case-insensitive. clap's auto-added
789        // --help / --version are excluded (they're not in this list, so the
790        // first-token scan below skips their lines).
791        let expected = [
792            "--blocks",
793            "--chop-long-lines",
794            "--clipboard",
795            "--content-type",
796            "--dim",
797            "--display",
798            "--examples",
799            "--exit-follow-on-close",
800            "--filter",
801            "--follow",
802            "--follow-name",
803            "--follow-suspend-on-motion",
804            "--format",
805            "--from-clipboard",
806            "--grep",
807            "--head",
808            "--header",
809            "--hex",
810            "--hex-group",
811            "--ignore-case",
812            "--IGNORE-CASE",
813            "--image-protocol",
814            "--image-width",
815            "--incsearch",
816            "--LINE-NUMBERS",
817            "--list-formats",
818            "--live",
819            "--manual",
820            "--mouse",
821            "--no-animate",
822            "--no-color",
823            "--no-hilite-search",
824            "--no-image",
825            "--no-init",
826            "--no-preprocess",
827            "--or-filter",
828            "--or-grep",
829            "--or-group",
830            "--output",
831            "--preprocess",
832            "--prettify",
833            "--prompt",
834            "--prompt-style",
835            "--quit-at-eof",
836            "--QUIT-AT-EOF",
837            "--quit-if-one-screen",
838            "--quit-on-intr",
839            "--raw-control-chars",
840            "--RAW-CONTROL-CHARS",
841            "--record-start",
842            "--rscroll",
843            "--shift",
844            "--split",
845            "--squeeze-blank-lines",
846            "--status-column",
847            "--status-style",
848            "--stdout",
849            "--tab-width",
850            "--tabs",
851            "--tag",
852            "--tag-file",
853            "--tail",
854            "--to-clipboard",
855            "--truecolor",
856            "--wheel-lines",
857            "--window",
858            "--wordwrap",
859        ];
860
861        // Confirm `expected` is itself sorted case-insensitively — this guards
862        // against a typo here masking a real ordering regression in the struct.
863        let mut sorted = expected.to_vec();
864        sorted.sort_by_key(|s| s.trim_start_matches('-').to_ascii_lowercase());
865        assert_eq!(
866            expected.to_vec(),
867            sorted,
868            "the `expected` list must itself be in case-insensitive alphabetical order"
869        );
870
871        // Walk the rendered help line by line. For each option line (after
872        // trimming, it starts with '-'), take the first `--long` token that is
873        // one of our flags. Because the flag name always precedes its own
874        // description on the line, embedded `--flag` references in descriptions
875        // are never matched first.
876        let listed: Vec<&str> = help
877            .lines()
878            .map(str::trim_start)
879            .filter(|l| l.starts_with('-'))
880            .filter_map(|l| {
881                l.split(|c: char| c.is_whitespace() || c == ',')
882                    .find(|tok| expected.contains(tok))
883            })
884            .collect();
885        assert_eq!(listed, expected, "help long-flag order should be alphabetical");
886    }
887}