Skip to main content

coding_tools/cli/
ct_view.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Jonathan Shook
3
4//! The `ct-view` command grammar (see [`crate::cli`]); the `ct-view` bin is a
5//! thin parse-and-dispatch wrapper over this `Cli`.
6
7use std::path::PathBuf;
8
9use clap::Parser;
10
11use crate::explain::Format;
12use crate::pattern;
13use crate::pulse::HeartbeatOpts;
14
15#[derive(Parser, Debug)]
16#[command(
17    name = "ct-view",
18    version,
19    about = "Show a file's lines by range, or the regions around a pattern with context.",
20    long_about = "ct-view is a focused, bounded reader for a single file (also reachable as \
21                  `ct view`): print a line range with --range, or the windows around a \
22                  --match pattern with --context lines, rather than dumping the whole file. \
23                  See `ct-view --explain` for agent-oriented documentation."
24)]
25pub struct Cli {
26    /// File to view.
27    pub path: PathBuf,
28
29    /// Line range A:B (1-based, inclusive); also A: (to end), :B (from start), or A (one line).
30    #[arg(long)]
31    pub range: Option<String>,
32
33    /// Show only lines matching this pattern (substring->glob->regex promoted), with --context around each. Accepts file:PATH / text:VALUE; a multi-line pattern matches as a line-anchored literal block.
34    #[arg(long = "match")]
35    pub pattern: Option<String>,
36
37    /// Pin how the pattern is interpreted (promotion off): literal, glob, or regex.
38    #[arg(long, value_enum)]
39    pub mode: Option<pattern::Mode>,
40
41    /// Lines of context shown around each --match hit.
42    #[arg(long, short = 'C', default_value_t = 2)]
43    pub context: usize,
44
45    /// Cap the number of lines emitted.
46    #[arg(long)]
47    pub limit: Option<usize>,
48
49    /// Abort with exit 2 if the view exceeds SECS seconds (fractional allowed).
50    #[arg(long, value_name = "SECS")]
51    pub timeout: Option<f64>,
52
53    #[command(flatten)]
54    pub heartbeat: HeartbeatOpts,
55
56    /// Suppress the line-number gutter in text output.
57    #[arg(long)]
58    pub plain: bool,
59
60    /// Emit a structured JSON result instead of text.
61    #[arg(long)]
62    pub json: bool,
63
64    /// Print agent usage docs (md or json) and exit.
65    #[arg(long, value_enum, num_args = 0..=1, default_missing_value = "md")]
66    pub explain: Option<Format>,
67}