vibe_coding_tracker/cli.rs
1//! clap command-line surface and the time-range filter it resolves to.
2//!
3//! The `///` comments on [`Commands`] and their arguments are what clap
4//! renders as `--help` text, so they read as user-facing prose. [`Cli`] is
5//! the parsed top-level structure; [`resolve_time_range`] collapses the
6//! mutually-exclusive `--daily` / `--weekly` / `--monthly` / `--all` flags
7//! into a single [`TimeRange`].
8
9use clap::{Parser, Subcommand, ValueEnum};
10use schemars::JsonSchema;
11use serde::{Deserialize, Serialize};
12use std::path::PathBuf;
13
14/// Time window applied when aggregating sessions.
15///
16/// Each variant maps to a cutoff date via [`TimeRange::cutoff_date`];
17/// [`TimeRange::All`] is the default and disables filtering. Serializes as a
18/// lowercase string (`"daily"` / `"weekly"` / `"monthly"` / `"all"`) so it can
19/// be reused verbatim as the `config.general.default_time_range` setting.
20#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
21#[serde(rename_all = "lowercase")]
22pub enum TimeRange {
23 /// Today only.
24 Daily,
25 /// The current week, starting Monday.
26 Weekly,
27 /// The current calendar month, from the 1st.
28 Monthly,
29 /// No filtering — every session.
30 #[default]
31 All,
32}
33
34impl TimeRange {
35 /// Returns the inclusive cutoff date for this range, or `None` for
36 /// [`TimeRange::All`].
37 ///
38 /// The cutoff is computed against today in the system local timezone:
39 /// `Weekly` anchors on the most recent Monday and `Monthly` on the first
40 /// of the month. Sessions on or after the returned date are kept.
41 ///
42 /// # Panics
43 ///
44 /// Does not panic: the `with_day(1)` used for `Monthly` is always valid
45 /// because day 1 exists in every month.
46 pub fn cutoff_date(&self) -> Option<chrono::NaiveDate> {
47 use chrono::{Datelike, Local};
48 let today = Local::now().date_naive();
49 match self {
50 TimeRange::All => None,
51 TimeRange::Daily => Some(today),
52 TimeRange::Weekly => {
53 let days_since_monday = today.weekday().num_days_from_monday() as i64;
54 Some(today - chrono::Duration::days(days_since_monday))
55 }
56 TimeRange::Monthly => Some(today.with_day(1).unwrap()),
57 }
58 }
59}
60
61/// Collapses the period flags into a single [`TimeRange`].
62///
63/// Checks `daily`, then `weekly`, then `monthly`, returning the first that is
64/// set; falls back to [`TimeRange::All`] when none are. The flags are mutually
65/// exclusive at the clap layer (shared `period` group), so at most one is ever
66/// true here.
67pub fn resolve_time_range(daily: bool, weekly: bool, monthly: bool) -> TimeRange {
68 resolve_time_range_with_default(daily, weekly, monthly, false, TimeRange::All)
69}
70
71/// Collapses the period flags into a [`TimeRange`], falling back to `default`
72/// when the user passed none of them.
73///
74/// An explicit flag always wins (`--all` maps to [`TimeRange::All`]); only when
75/// every flag is unset does `default` (from `config.general.default_time_range`)
76/// apply. The flags share clap's `period` group, so at most one is ever true.
77pub fn resolve_time_range_with_default(
78 daily: bool,
79 weekly: bool,
80 monthly: bool,
81 all: bool,
82 default: TimeRange,
83) -> TimeRange {
84 if daily {
85 TimeRange::Daily
86 } else if weekly {
87 TimeRange::Weekly
88 } else if monthly {
89 TimeRange::Monthly
90 } else if all {
91 TimeRange::All
92 } else {
93 default
94 }
95}
96
97/// A provider whose raw quota/usage API response `vct fetch` can print.
98#[derive(ValueEnum, Debug, Clone, Copy)]
99pub enum FetchProvider {
100 /// Claude Code (`GET /api/oauth/usage`).
101 Claude,
102 /// OpenAI Codex (ChatGPT `wham/usage`).
103 Codex,
104 /// GitHub Copilot CLI (`GET /copilot_internal/user`).
105 Copilot,
106 /// Cursor CLI (`GET /api/usage-summary`).
107 Cursor,
108}
109
110/// Vibe Coding Tracker - AI coding assistant usage analyzer.
111#[derive(Parser, Debug)]
112#[command(name = "vibe_coding_tracker")]
113#[command(author, version = crate::VERSION, about, long_about = None)]
114pub struct Cli {
115 /// The subcommand to run.
116 #[command(subcommand)]
117 pub command: Commands,
118}
119
120/// Top-level subcommands exposed by the CLI.
121#[derive(Subcommand, Debug)]
122pub enum Commands {
123 /// Analyze local session data (single file or all sessions).
124 Analysis {
125 /// JSONL or JSON session file to analyze; prints complete JSON by default.
126 #[arg(
127 value_name = "FILE",
128 conflicts_with_all = ["daily", "weekly", "monthly", "all"]
129 )]
130 file: Option<PathBuf>,
131
132 /// Output complete analysis data as JSON.
133 #[arg(long, group = "analysis_format")]
134 json: bool,
135
136 /// Output as plain text.
137 #[arg(long, group = "analysis_format")]
138 text: bool,
139
140 /// Output as static table (instead of interactive TUI).
141 #[arg(long, group = "analysis_format")]
142 table: bool,
143
144 /// Show only today's data.
145 #[arg(long, group = "period")]
146 daily: bool,
147
148 /// Show only this week's data.
149 #[arg(long, group = "period")]
150 weekly: bool,
151
152 /// Show only this month's data.
153 #[arg(long, group = "period")]
154 monthly: bool,
155
156 /// Show all data (default).
157 #[arg(short, long, group = "period")]
158 all: bool,
159 },
160
161 /// Display token usage statistics.
162 Usage {
163 /// Output usage data as JSON.
164 #[arg(long, group = "usage_format")]
165 json: bool,
166
167 /// Output as plain text.
168 #[arg(long, group = "usage_format")]
169 text: bool,
170
171 /// Output as static table.
172 #[arg(long, group = "usage_format")]
173 table: bool,
174
175 /// Merge models that share a base name across provider prefixes
176 /// (e.g. `openai/gpt-5.5` + `azure/gpt-5.5`). In the TUI this seeds the
177 /// initial state; press `m` to toggle. Ignored for `--json`.
178 #[arg(long)]
179 merge_providers: bool,
180
181 /// Show only today's data.
182 #[arg(long, group = "period")]
183 daily: bool,
184
185 /// Show only this week's data.
186 #[arg(long, group = "period")]
187 weekly: bool,
188
189 /// Show only this month's data.
190 #[arg(long, group = "period")]
191 monthly: bool,
192
193 /// Show all data (default).
194 #[arg(short, long, group = "period")]
195 all: bool,
196 },
197
198 /// Display version information.
199 Version {
200 /// Output as JSON.
201 #[arg(long)]
202 json: bool,
203
204 /// Output as plain text.
205 #[arg(long)]
206 text: bool,
207 },
208
209 /// Update to the latest version from GitHub releases.
210 Update {
211 /// Check for updates without installing.
212 #[arg(long)]
213 check: bool,
214
215 /// Force update without confirmation prompt.
216 #[arg(long, short)]
217 force: bool,
218 },
219
220 /// Fetch a provider's raw quota/usage API response.
221 Fetch {
222 /// Which provider to fetch (claude | codex | copilot | cursor).
223 provider: FetchProvider,
224
225 /// Output as pretty JSON (default).
226 #[arg(long, group = "fetch_format")]
227 json: bool,
228
229 /// Output as flattened plain text.
230 #[arg(long, group = "fetch_format")]
231 text: bool,
232
233 /// Output as a flattened key/value table.
234 #[arg(long, group = "fetch_format")]
235 table: bool,
236 },
237
238 /// Show or edit the persistent settings file (`~/.vct/config.toml`).
239 Config {
240 /// What to do; defaults to showing the current settings.
241 #[command(subcommand)]
242 action: Option<ConfigAction>,
243 },
244}
245
246/// Actions for the `config` subcommand.
247#[derive(Subcommand, Debug, Clone, Copy)]
248pub enum ConfigAction {
249 /// Print the config file path.
250 Path,
251 /// Print the current settings (default).
252 Show,
253 /// Open the config file in `$VISUAL` / `$EDITOR`.
254 Edit,
255 /// Print the JSON schema for the settings file.
256 ///
257 /// Redirect it to regenerate the committed schema:
258 /// `vct config schema > vct.schema.json`.
259 Schema,
260 /// Rewrite a legacy-format config to the current on-disk layout in place.
261 ///
262 /// Loading the settings already migrates the file automatically; this forces
263 /// the same pass so a schema-aware editor sees the upgraded file right away.
264 Migrate,
265}