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    /// Collapse runs of two or more consecutive blank lines into a
361    /// single blank line at display time. Real line numbers, search,
362    /// and tag jumps are unaffected (they reference the original
363    /// count). Mirrors `less -s`.
364    #[arg(short = 's', long = "squeeze-blank-lines")]
365    pub squeeze_blanks: bool,
366
367    /// Show a 1-column status gutter at the far left: a mark letter on marked
368    /// lines, else `*` on lines containing a current-search match. Stays fixed
369    /// under horizontal scroll. No-op in --hex/-r/image modes. Mirrors `less -J`.
370    #[arg(short = 'J', long = "status-column")]
371    pub status_column: bool,
372
373    /// Style for the status row. Comma-separated tokens: `bold`, `dim`,
374    /// `italic`, `underline`, `reverse`, `fg=COLOR`, `bg=COLOR`. COLOR is a
375    /// named color (`black`..`white`, optional `bright-` prefix), `#RRGGBB`,
376    /// or an indexed value (0–255). Empty string disables theming.
377    /// Default: `reverse`.
378    #[arg(long = "status-style", value_name = "SPEC", default_value = "reverse")]
379    pub status_style: String,
380
381    /// Synonym for `--output -`: write the batch-mode output to stdout.
382    #[arg(long = "stdout", conflicts_with = "output")]
383    pub stdout: bool,
384
385    /// Tab stop width (default 8).
386    #[arg(long = "tab-width", default_value_t = 8)]
387    pub tab_width: u8,
388
389    /// Tab stops as a comma-separated column list, e.g. `-x4,8,16`. A single
390    /// value is equivalent to `--tab-width`. With multiple values, tabs advance
391    /// to the next listed column; past the last stop the final interval
392    /// repeats. Overrides `--tab-width`. Mirrors `less -x`.
393    #[arg(short = 'x', long = "tabs", value_name = "LIST")]
394    pub tabs: Option<String>,
395
396    /// Jump to the tag NAME at startup (requires a tags file).
397    #[arg(short = 't', long = "tag", value_name = "NAME")]
398    pub tag: Option<String>,
399
400    /// Path to the tags file. Default: walk up from CWD looking for `tags`.
401    #[arg(short = 'T', long = "tag-file", value_name = "PATH")]
402    pub tag_file: Option<std::path::PathBuf>,
403
404    /// Show only the last N lines of the source. For files this skips most of
405    /// the index work — useful for huge logs. Combine with `-f` for `tail -f`.
406    /// Mutually exclusive with --head. Streaming stdin is not supported.
407    #[arg(long = "tail", value_name = "N", conflicts_with = "head")]
408    pub tail: Option<usize>,
409
410    /// Batch sink: apply filters/head/tail/prettify and copy the result to the
411    /// system clipboard, then exit. Mutually exclusive with -o/--stdout.
412    #[arg(long = "to-clipboard", conflicts_with_all = ["output", "stdout"])]
413    pub to_clipboard: bool,
414
415    /// Truecolor (24-bit RGB) handling. `auto` (default) checks `$COLORTERM`
416    /// and downsamples when truecolor isn't advertised; `never` always
417    /// downsamples to the 256-color palette; `always` passes RGB through
418    /// regardless of terminal capability.
419    #[arg(long = "truecolor", value_name = "MODE", default_value = "auto")]
420    pub truecolor: String,
421
422    /// Body lines scrolled per mouse-wheel notch under `--mouse` (default 3).
423    #[arg(long = "wheel-lines", value_name = "N")]
424    pub wheel_lines: Option<u16>,
425
426    /// PageDown / PageUp step size in lines. Default: full screen
427    /// height (body rows). Half-page commands always advance by half
428    /// the screen regardless. Mirrors `less -zn` / `--window=n`.
429    #[arg(short = 'z', long = "window", value_name = "N")]
430    pub window: Option<u16>,
431
432    /// In wrap mode, break lines on whitespace boundaries instead of
433    /// mid-character when possible. Falls back to mid-character break
434    /// when no whitespace fits in the row. Mirrors `less --wordwrap`.
435    #[arg(long = "wordwrap")]
436    pub word_wrap: bool,
437
438    /// Files to view (only the first is opened in MVP).
439    pub files: Vec<PathBuf>,
440}
441
442#[cfg(test)]
443mod tests {
444    use super::*;
445
446    #[test]
447    fn parses_no_flags_no_files() {
448        let a = Args::parse_from(["tess"]);
449        assert!(!a.line_numbers);
450        assert!(!a.chop);
451        assert_eq!(a.tab_width, 8);
452        assert!(a.files.is_empty());
453    }
454
455    #[test]
456    fn parses_raw_control_chars_alias() {
457        let a = Args::parse_from(["tess", "-R", "f"]);
458        assert!(a.RAW_CONTROL_CHARS);
459    }
460    #[test]
461    fn raw_control_chars_conflicts_with_raw_and_no_color() {
462        assert!(Args::try_parse_from(["tess", "-R", "-r", "f"]).is_err());
463        assert!(Args::try_parse_from(["tess", "-R", "--no-color", "f"]).is_err());
464    }
465    #[test]
466    fn parses_shift_and_wheel_lines() {
467        let a = Args::parse_from(["tess", "--shift", "12", "--wheel-lines", "5", "f"]);
468        assert_eq!(a.shift, Some(12));
469        assert_eq!(a.wheel_lines, Some(5));
470    }
471
472    #[test]
473    fn parses_short_flags_and_file() {
474        let a = Args::parse_from(["tess", "-N", "-S", "foo.txt"]);
475        assert!(a.line_numbers);
476        assert!(a.chop);
477        assert_eq!(a.files, vec![PathBuf::from("foo.txt")]);
478    }
479
480    #[test]
481    fn parses_tab_width() {
482        let a = Args::parse_from(["tess", "--tab-width", "4", "x"]);
483        assert_eq!(a.tab_width, 4);
484    }
485
486    #[test]
487    fn parses_tabs_list() {
488        let a = Args::parse_from(["tess", "--tabs", "4,8,16", "f"]);
489        assert_eq!(a.tabs.as_deref(), Some("4,8,16"));
490        let b = Args::parse_from(["tess", "-x", "4", "f"]);
491        assert_eq!(b.tabs.as_deref(), Some("4"));
492    }
493
494    #[test]
495    fn collects_multiple_files() {
496        let a = Args::parse_from(["tess", "a", "b", "c"]);
497        assert_eq!(a.files.len(), 3);
498    }
499
500    #[test]
501    fn parses_follow_short_flag() {
502        let a = Args::parse_from(["tess", "-f", "log.txt"]);
503        assert!(a.follow);
504        assert_eq!(a.files, vec![PathBuf::from("log.txt")]);
505    }
506
507    #[test]
508    fn parses_follow_long_flag() {
509        let a = Args::parse_from(["tess", "--follow"]);
510        assert!(a.follow);
511    }
512
513    #[test]
514    fn follow_defaults_off() {
515        let a = Args::parse_from(["tess", "x"]);
516        assert!(!a.follow);
517    }
518
519    #[test]
520    fn parses_head() {
521        let a = Args::parse_from(["tess", "--head", "100", "x"]);
522        assert_eq!(a.head, Some(100));
523        assert_eq!(a.tail, None);
524    }
525
526    #[test]
527    fn parses_tail() {
528        let a = Args::parse_from(["tess", "--tail", "50", "x"]);
529        assert_eq!(a.tail, Some(50));
530        assert_eq!(a.head, None);
531    }
532
533    #[test]
534    fn head_and_tail_are_mutually_exclusive() {
535        let r = Args::try_parse_from(["tess", "--head", "10", "--tail", "20", "x"]);
536        assert!(r.is_err(), "clap should reject combining --head and --tail");
537    }
538
539    #[test]
540    fn head_tail_default_to_none() {
541        let a = Args::parse_from(["tess", "x"]);
542        assert!(a.head.is_none());
543        assert!(a.tail.is_none());
544    }
545
546    #[test]
547    fn parses_grep_repeatable_and_no_format_required() {
548        let a = Args::parse_from([
549            "tess",
550            "--grep", "error",
551            "--grep", r"^\[",
552            "log",
553        ]);
554        assert_eq!(a.grep.len(), 2);
555        assert_eq!(a.grep[0], "error");
556        assert_eq!(a.grep[1], r"^\[");
557        assert_eq!(a.format, None);
558    }
559
560    #[test]
561    fn parses_format_and_filter() {
562        let a = Args::parse_from([
563            "tess", "--format", "apache-combined",
564            "--filter", "status=500",
565            "--filter", "ip~^10\\.",
566            "log",
567        ]);
568        assert_eq!(a.format.as_deref(), Some("apache-combined"));
569        assert_eq!(a.filter.len(), 2);
570        assert_eq!(a.filter[0], "status=500");
571    }
572
573    #[test]
574    fn parses_dim() {
575        let a = Args::parse_from(["tess", "--format", "x", "--filter", "y=z", "--dim", "f"]);
576        assert!(a.dim);
577    }
578
579    #[test]
580    fn parses_list_formats() {
581        let a = Args::parse_from(["tess", "--list-formats"]);
582        assert!(a.list_formats);
583    }
584
585    #[test]
586    fn parses_manual() {
587        let a = Args::parse_from(["tess", "--manual"]);
588        assert!(a.manual);
589    }
590
591    #[test]
592    fn parses_examples() {
593        let a = Args::parse_from(["tess", "--examples"]);
594        assert!(a.examples);
595    }
596
597    #[test]
598    fn parses_live() {
599        let a = Args::parse_from(["tess", "--live", "f"]);
600        assert!(a.live);
601        assert!(!a.follow);
602    }
603
604    #[test]
605    fn live_and_follow_are_mutually_exclusive() {
606        let r = Args::try_parse_from(["tess", "--live", "--follow", "f"]);
607        assert!(r.is_err(), "clap should reject combining --live and --follow");
608    }
609
610    #[test]
611    fn parses_prettify() {
612        let a = Args::parse_from(["tess", "--prettify", "f.json"]);
613        assert!(a.prettify);
614        assert_eq!(a.content_type, None);
615    }
616
617    #[test]
618    fn parses_content_type() {
619        let a = Args::parse_from(["tess", "--content-type", "json", "f"]);
620        assert_eq!(a.content_type.as_deref(), Some("json"));
621    }
622
623    #[test]
624    fn parses_output_long_and_short() {
625        let a = Args::parse_from(["tess", "-o", "/tmp/out.txt", "f"]);
626        assert_eq!(a.output.as_deref(), Some("/tmp/out.txt"));
627        let b = Args::parse_from(["tess", "--output", "/tmp/out.txt", "f"]);
628        assert_eq!(b.output.as_deref(), Some("/tmp/out.txt"));
629    }
630
631    #[test]
632    fn parses_status_column() {
633        let a = Args::parse_from(["tess", "-J", "f"]);
634        assert!(a.status_column);
635        let b = Args::parse_from(["tess", "--status-column", "f"]);
636        assert!(b.status_column);
637        let c = Args::parse_from(["tess", "f"]);
638        assert!(!c.status_column, "status_column defaults off");
639    }
640
641    #[test]
642    fn parses_stdout_flag() {
643        let a = Args::parse_from(["tess", "--stdout", "f"]);
644        assert!(a.stdout);
645        assert_eq!(a.output, None);
646    }
647
648    #[test]
649    fn output_and_stdout_are_mutually_exclusive() {
650        let r = Args::try_parse_from(["tess", "-o", "x", "--stdout", "f"]);
651        assert!(r.is_err(), "clap should reject combining --output and --stdout");
652    }
653
654    #[test]
655    fn parses_mouse_flag() {
656        let a = Args::parse_from(["tess", "--mouse", "f"]);
657        assert!(a.mouse);
658    }
659
660    #[test]
661    fn mouse_defaults_off() {
662        let a = Args::parse_from(["tess", "f"]);
663        assert!(!a.mouse);
664    }
665
666    #[test]
667    fn parses_no_animate() {
668        assert!(Args::parse_from(["tess", "--no-animate", "x.gif"]).no_animate);
669        assert!(!Args::parse_from(["tess", "x.gif"]).no_animate);
670    }
671
672    #[test]
673    fn parses_no_image_flag() {
674        let a = Args::parse_from(["tess", "--no-image", "cat.png"]);
675        assert!(a.no_image);
676    }
677
678    #[test]
679    fn parses_blocks_flag() {
680        let a = Args::parse_from(["tess", "--blocks", "cat.png"]);
681        assert!(a.blocks);
682    }
683
684    #[test]
685    fn parses_image_protocol() {
686        assert_eq!(Args::parse_from(["tess", "f"]).image_protocol, "auto");
687        assert_eq!(Args::parse_from(["tess", "--image-protocol", "kitty", "f"]).image_protocol, "kitty");
688    }
689
690    #[test]
691    fn parses_image_width() {
692        let a = Args::parse_from(["tess", "--image-width", "120", "cat.png"]);
693        assert_eq!(a.image_width, Some(120));
694    }
695
696    #[test]
697    fn parses_incsearch() {
698        let a = Args::parse_from(["tess", "--incsearch", "f"]);
699        assert!(a.incsearch);
700    }
701
702    #[test]
703    fn incsearch_defaults_off() {
704        let a = Args::parse_from(["tess", "f"]);
705        assert!(!a.incsearch);
706    }
707
708    #[test]
709    fn image_flags_default_off() {
710        let a = Args::parse_from(["tess", "f"]);
711        assert!(!a.no_image);
712        assert!(!a.blocks);
713        assert_eq!(a.image_width, None);
714    }
715
716    #[test]
717    fn repeated_scalar_flag_takes_last_value() {
718        // args_override_self: a group can inject `--display X` and a later CLI
719        // `--display Y` wins instead of clap erroring "cannot be used multiple
720        // times". Also covers plain `less`-style last-wins.
721        let a = Args::parse_from(["tess", "--display", "X", "--display", "Y", "--format", "f"]);
722        assert_eq!(a.display.as_deref(), Some("Y"));
723        let b = Args::parse_from(["tess", "--tail", "5", "--tail", "1", "x"]);
724        assert_eq!(b.tail, Some(1));
725    }
726
727    #[test]
728    fn repeatable_flags_still_accumulate_with_override_self() {
729        // args_override_self must not collapse Append-action Vec flags.
730        let a = Args::parse_from(["tess", "--grep", "a", "--grep", "b", "x"]);
731        assert_eq!(a.grep, vec!["a".to_string(), "b".to_string()]);
732    }
733
734    #[test]
735    fn parses_or_flags_repeatable() {
736        let a = Args::parse_from([
737            "tess",
738            "--or-grep", "failed",
739            "--or-group", "svc",
740            "--or-filter", "lvl=ERROR",
741            "x",
742        ]);
743        assert_eq!(a.or_grep, vec!["failed".to_string()]);
744        assert_eq!(a.or_group, vec!["svc".to_string()]);
745        assert_eq!(a.or_filter, vec!["lvl=ERROR".to_string()]);
746    }
747
748    #[test]
749    fn or_flags_conflict_with_hex() {
750        let r = Args::try_parse_from(["tess", "--hex", "--or-grep", "x", "f"]);
751        assert!(r.is_err(), "clap should reject --hex with --or-grep");
752    }
753
754    #[test]
755    fn parses_clipboard_flags() {
756        assert!(Args::parse_from(["tess", "--clipboard", "f"]).clipboard);
757        assert!(Args::parse_from(["tess", "--from-clipboard"]).from_clipboard);
758        assert!(Args::parse_from(["tess", "--to-clipboard", "f"]).to_clipboard);
759    }
760    #[test]
761    fn clipboard_sink_conflicts() {
762        assert!(Args::try_parse_from(["tess", "--to-clipboard", "--stdout", "f"]).is_err());
763        assert!(Args::try_parse_from(["tess", "--to-clipboard", "-o", "x", "f"]).is_err());
764    }
765    #[test]
766    fn from_clipboard_conflicts_with_files() {
767        assert!(Args::try_parse_from(["tess", "--from-clipboard", "f"]).is_err());
768    }
769
770    #[test]
771    fn help_lists_flags_in_alphabetical_order() {
772        use clap::CommandFactory;
773        let mut cmd = Args::command();
774        let help = cmd.render_help().to_string();
775
776        // The full set of long flags, in the order we expect `--help` to list
777        // them: alphabetical by long name, case-insensitive. clap's auto-added
778        // --help / --version are excluded (they're not in this list, so the
779        // first-token scan below skips their lines).
780        let expected = [
781            "--blocks",
782            "--chop-long-lines",
783            "--clipboard",
784            "--content-type",
785            "--dim",
786            "--display",
787            "--examples",
788            "--exit-follow-on-close",
789            "--filter",
790            "--follow",
791            "--follow-name",
792            "--follow-suspend-on-motion",
793            "--format",
794            "--from-clipboard",
795            "--grep",
796            "--head",
797            "--header",
798            "--hex",
799            "--hex-group",
800            "--ignore-case",
801            "--IGNORE-CASE",
802            "--image-protocol",
803            "--image-width",
804            "--incsearch",
805            "--LINE-NUMBERS",
806            "--list-formats",
807            "--live",
808            "--manual",
809            "--mouse",
810            "--no-animate",
811            "--no-color",
812            "--no-hilite-search",
813            "--no-image",
814            "--no-init",
815            "--no-preprocess",
816            "--or-filter",
817            "--or-grep",
818            "--or-group",
819            "--output",
820            "--preprocess",
821            "--prettify",
822            "--prompt",
823            "--prompt-style",
824            "--quit-at-eof",
825            "--QUIT-AT-EOF",
826            "--quit-if-one-screen",
827            "--quit-on-intr",
828            "--raw-control-chars",
829            "--RAW-CONTROL-CHARS",
830            "--record-start",
831            "--rscroll",
832            "--shift",
833            "--squeeze-blank-lines",
834            "--status-column",
835            "--status-style",
836            "--stdout",
837            "--tab-width",
838            "--tabs",
839            "--tag",
840            "--tag-file",
841            "--tail",
842            "--to-clipboard",
843            "--truecolor",
844            "--wheel-lines",
845            "--window",
846            "--wordwrap",
847        ];
848
849        // Confirm `expected` is itself sorted case-insensitively — this guards
850        // against a typo here masking a real ordering regression in the struct.
851        let mut sorted = expected.to_vec();
852        sorted.sort_by_key(|s| s.trim_start_matches('-').to_ascii_lowercase());
853        assert_eq!(
854            expected.to_vec(),
855            sorted,
856            "the `expected` list must itself be in case-insensitive alphabetical order"
857        );
858
859        // Walk the rendered help line by line. For each option line (after
860        // trimming, it starts with '-'), take the first `--long` token that is
861        // one of our flags. Because the flag name always precedes its own
862        // description on the line, embedded `--flag` references in descriptions
863        // are never matched first.
864        let listed: Vec<&str> = help
865            .lines()
866            .map(str::trim_start)
867            .filter(|l| l.starts_with('-'))
868            .filter_map(|l| {
869                l.split(|c: char| c.is_whitespace() || c == ',')
870                    .find(|tok| expected.contains(tok))
871            })
872            .collect();
873        assert_eq!(listed, expected, "help long-flag order should be alphabetical");
874    }
875}