cmd-usage 0.5.5

Live Command Code (commandcode.ai) usage dashboard: credits, 5-hour and weekly windows, plan limits. Watch mode or one-shot.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
mod api;
mod cli;
mod update_check;
mod config;
mod render;
mod report_render;
mod reports;
mod snapshot;

#[cfg(test)]
mod cli_tests;

#[cfg(test)]
mod config_tests;

#[cfg(test)]
mod render_tests;

#[cfg(test)]
mod main_tests;

use std::io::Write;

use crate::render::{BOLD, DIM, RESET};

fn main() {
    let args = cli::parse_args();
    if args.help {
        cli::usage();
        return;
    }

    if let Some(cs) = args.config_set {
        if let Err(e) = config::set(
            cs.interval,
            cs.width,
            cs.sl_template,
            cs.sl_colors,
            cs.sl_ascii,
            cs.burst_on,
            cs.bursts,
        ) {
            eprintln!("error: {e}");
            std::process::exit(1);
        }
        return;
    }

    // offline local reports — no API, no key needed
    match args.subcmd {
        Some(cli::SubCmd::Daily) => {
            // account-wide (all harnesses) via API when key available; local fallback
            let data_source = if args.local {
                None
            } else {
                api::api_key().ok().map(|k| reports::load_account_daily(args.last.unwrap_or(7), &k))
            };
            match data_source {
                Some(Ok(by_day)) => {
                    let total = reports::sum_days(&by_day);
                    print!("{}", report_render::table("account", "all harnesses", &by_day, &total, None, args.json));
                }
                Some(Err(e)) => {
                    eprintln!("error: {e}");
                    std::process::exit(1);
                }
                None => {
                    let d = reports::load_local();
                    print!("{}", report_render::table("local", "offline, ~/.commandcode/projects", &d.by_day, &d.total, args.last, args.json));
                }
            }
            return;
        }
        Some(cli::SubCmd::Hours) => {
            let rows = if args.local {
                reports::load_local_hourly(args.hours.unwrap_or(24))
            } else {
                let key = match api::api_key() {
                    Ok(k) => k,
                    Err(e) => {
                        eprintln!("error: {e}");
                        std::process::exit(1);
                    }
                };
                match reports::load_account_hourly(args.hours.unwrap_or(24), &key) {
                    Ok(r) => r,
                    Err(e) => {
                        eprintln!("error: {e}");
                        std::process::exit(1);
                    }
                }
            };
            print!(
                "{}",
                report_render::hourly_table(
                    &rows,
                    args.json,
                    if args.local { "local, CLI sessions" } else { "all harnesses, UTC" }
                )
            );
            return;
        }
        Some(cli::SubCmd::Model) => {
            let d = reports::load_local();
            print!("{}", report_render::model_table(&d.by_model, args.json));
            return;
        }
        Some(cli::SubCmd::Session) => {
            let d = reports::load_local();
            print!("{}", report_render::project_table(&d.by_project, args.json));
            return;
        }
        Some(cli::SubCmd::Statusline) => {
            statusline_cmd(&args);
            return;
        }
        Some(cli::SubCmd::Models) => {
            models_cmd(&args);
            return;
        }
        None => {}
    }

    let cfg = config::load();
    let interval = args
        .interval
        .or(Some(cfg.interval_secs))
        .unwrap_or(5)
        .clamp(1, 86_400);
    let bar_width = args.bar_width.or(Some(cfg.bar_width)).unwrap_or(20).max(5);
    // spend-burst sparkline is opt-in: -b/--bursts on the CLI turns it on for
    // this run; otherwise it follows the config flag (default off).
    let burst_on = args.bursts.is_some() || cfg.burst_enabled;
    let burst_cap = if burst_on {
        args.bursts.or(Some(cfg.burst_samples)).unwrap_or(40).clamp(5, 240)
    } else {
        0
    };

    if args.once {
        let s = snapshot::snapshot();
        if args.plain {
            print!("{}", render::render_plain(&s, bar_width));
        } else {
            print!("{}", render::render(&s, bar_width));
        }
        return;
    }

    // Watch mode: check update BEFORE first frame draw. check_sync() blocks
    // once per day (≤5s); async eprintln here could land mid-redraw and tear
    // the in-place frame.
    if let Some(msg) = update_check::check_sync() {
        eprintln!("{msg}");
    }

    // live mode: true in-place redraw. Frame's last line = status line,
    // drawn WITHOUT trailing newline so the cursor stays on it. Spinner
    // and countdown rewrite that line in place. No scroll, no drift.
    //
    // Terminal size is re-queried every refresh: on a window too small for
    // the full dashboard we swap to a compact single-line frame instead of
    // exiting, so shrinking/widening mid-run follows live (term_size is NOT
    // cached).
    let stdout = std::io::stdout();
    let mut out = stdout.lock();
    let mut prev_lines = 0usize;
    // deltas ($ per refresh) feed the spend-burst sparkline and are persisted
    // so the trend survives restarts (see trend_path below).
    let mut history: Vec<f64> = if burst_on { load_trend().unwrap_or_default() } else { Vec::new() };
    let mut history_used: Vec<f64> = Vec::new(); // raw cumulative 5h spend
    loop {
        let s = snapshot::snapshot();
        let (rows, cols) = term_size().unwrap_or((0, 0));
        // <8x4 is not a usable dashboard even in compact form: keep drawing
        // the compact line anyway (clip keeps it stable), no hard exit.
        let compact = cols > 0 && (rows < 16 || cols < 40);
        // track 5-hour spend DELTA between refreshes — cumulative spend is
        // monotonic (sparkline of it is all-full); deltas show burst vs idle
        let used = s
            .credits
            .window_limits
            .five_hour
            .as_ref()
            .map(|w| w.used)
            .unwrap_or(0.0);
        let delta = history_used
            .last()
            .map(|prev| (used - prev).max(0.0))
            .unwrap_or(0.0);
        history_used.push(used);
        if history_used.len() > 60 {
            history_used.remove(0);
        }
        if burst_on {
            history.push(delta);
            if history.len() > burst_cap {
                history.remove(0);
            }
            // persist on every refresh: tiny file, keeps the trend across runs.
            let _ = save_trend(&history);
        }
        let text = if compact {
            compact_dashboard(&s)
        } else if args.plain {
            render::render_plain(&s, bar_width)
        } else {
            render::render(&s, bar_width)
        };
        let spark = if compact || !burst_on {
            String::new()
        } else if history.len() >= 2 && history.iter().any(|v| *v > 0.0) {
            format!("{DIM}spend bursts ({}s samples){RESET} {}\n", interval, render::sparkline(&history))
        } else {
            String::new() // idle (all-zero deltas) → no row, no flat-line noise
        };
        let status_line = format!(
            "{DIM}refreshing every {interval}s · ctrl-c to quit{RESET}"
        );
        let frame = format!("{text}{spark}{status_line}");
        prev_lines = redraw_frame(&mut out, &frame, prev_lines, (cols > 0).then_some(cols));
        out.flush().ok();
        // countdown: rewrite just the status line each second (cursor already on it)
        for remaining in (1..interval).rev() {
            std::thread::sleep(std::time::Duration::from_secs(1));
            let msg = format!(
                "\r\x1b[2K{DIM}refreshing every {interval}s · next refresh in {remaining}s · ctrl-c to quit{RESET}"
            );
            write!(out, "{}", clip_to_width(&msg, (cols > 0).then_some(cols))).ok();
            out.flush().ok();
        }
    }
}

/// Minimal one-line dashboard for windows too small for the full frame:
/// plan + remaining monthly credits (clip handles the rest). Stays stable
/// where the full 14-row dashboard would scroll every refresh.
fn compact_dashboard(s: &crate::render::Snapshot) -> String {
    use crate::render::plan_name;
    let cap = cmduse_core::plan_monthly_cap(&s.sub.plan_id);
    let cap_txt = cap.map(cmduse_core::money).unwrap_or_else(|| "-".into());
    let rem_txt = cmduse_core::money(s.credits.credits.monthly_credits);
    let h5 = s
        .credits
        .window_limits
        .five_hour
        .as_ref()
        .map(|w| cmduse_core::money(w.used))
        .unwrap_or_else(|| "-".into());
    let status = if s.sub.status.is_empty() {
        String::new()
    } else {
        format!(" · {}", s.sub.status)
    };
    format!("{BOLD}{}{RESET} {rem_txt}/{cap_txt} · 5h {h5}{status}\n", plan_name(&s.sub.plan_id))
}

/// Spend-burst delta history is persisted to ~/.cache/cmd-usage/trend.json so
/// the sparkline survives restarts (same cache dir as the update check).
fn trend_path() -> std::path::PathBuf {
    let home = std::env::var("HOME").unwrap_or_else(|_| "/".into());
    std::path::PathBuf::from(home).join(".cache/cmd-usage/trend.json")
}

fn load_trend() -> Option<Vec<f64>> {
    let s = std::fs::read_to_string(trend_path()).ok()?;
    serde_json::from_str(&s).ok()
}

fn save_trend(history: &[f64]) -> std::io::Result<()> {
    let path = trend_path();
    if let Some(dir) = path.parent() {
        std::fs::create_dir_all(dir)?;
    }
    std::fs::write(path, serde_json::to_string(history).unwrap_or_else(|_| "[]".into()))
}

/// Terminal (rows, cols) from `stty size`. None when not a tty or the probe
/// fails — redraw then skips clipping (safe: non-tty can't wrap).
/// Deliberately UNcached: the watch loop re-queries every refresh so resizing
/// mid-run switches between the full and compact dashboards live.
fn term_size() -> Option<(usize, usize)> {
    let out = std::process::Command::new("sh")
        .arg("-c")
        .arg("stty size < /dev/tty")
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    let text = String::from_utf8_lossy(&out.stdout);
    let mut it = text.split_whitespace();
    let rows = it.next()?.parse().ok()?;
    let cols = it.next()?.parse().ok()?;
    Some((rows, cols))
}

/// Keep a frame line from auto-wrapping: count visible columns ignoring SGR
/// escapes and control chars, and drop trailing visible chars that would
/// push past `cols`. Appends a reset so a truncated color run can't leak.
pub fn clip_to_width(line: &str, cols: Option<usize>) -> String {
    let Some(cols) = cols else { return line.into() };
    if cols == 0 {
        return String::new();
    }
    let mut vis = 0usize;
    let mut cs = line.chars().peekable();
    while let Some(&c) = cs.peek() {
        match c {
            '\x1b' => {
                cs.next();
                for e in cs.by_ref() {
                    if e.is_ascii_alphabetic() {
                        break;
                    }
                }
            }
            c if (c as u32) < 0x20 => {
                cs.next(); // \r etc: zero width
            }
            _ => {
                vis += 1;
                cs.next();
            }
        }
    }
    if vis <= cols {
        return line.into();
    }
    let mut out = String::new();
    let mut kept = 0usize;
    let mut cs = line.chars();
    while let Some(c) = cs.next() {
        match c {
            '\x1b' => {
                out.push(c);
                for e in cs.by_ref() {
                    out.push(e);
                    if e.is_ascii_alphabetic() {
                        break;
                    }
                }
            }
            '\r' => out.push(c), // carriage return is meaningful: keep it
            c if (c as u32) < 0x20 => {}
            _ => {
                if kept < cols {
                    out.push(c);
                    kept += 1;
                }
            }
        }
    }
    if out.ends_with("\x1b[0m") {
        out
    } else {
        out + "\x1b[0m"
    }
}

/// In-place terminal redraw of one frame. Assumes the cursor parks on the
/// previous frame's last line (no trailing newline). Returns the new frame's
/// line count so the caller can pass it back as `prev_lines`.
fn redraw_frame(
    out: &mut impl Write,
    frame: &str,
    prev_lines: usize,
    cols: Option<usize>,
) -> usize {
    if prev_lines > 0 {
        // move cursor up to top of previous frame (we're ON its last line)
        write!(out, "\x1b[{}F", prev_lines - 1).ok();
    }
    let lines: Vec<&str> = frame.lines().collect();
    let n = lines.len();
    for (i, line) in lines.iter().enumerate() {
        let line = clip_to_width(line, cols);
        if i + 1 < n {
            write!(out, "\x1b[2K\r{line}\n").ok();
        } else {
            // last line: clear and write, NO newline — cursor stays here
            write!(out, "\x1b[2K\r{line}").ok();
        }
    }
    if n < prev_lines {
        // new frame shorter than old (error frame): clear the stale rows
        // below the new frame WITHOUT touching the frame we just wrote.
        // First move down one row (no clear) so the frame's last line
        // survives, then clear-and-advance each remaining stale row,
        // clear the old bottom row, and step back up to the new bottom.
        // Writing \n at the very bottom row would scroll and desync —
        // clear in place with \x1b[2K\x1b[1B instead.
        write!(out, "\x1b[1B").ok();
        for _ in (n + 1)..prev_lines {
            write!(out, "\x1b[2K\x1b[1B").ok();
        }
        write!(out, "\x1b[2K").ok();
        write!(out, "\x1b[{}F", prev_lines - n).ok();
    }
    n
}

fn models_cmd(args: &cli::Args) {
    let key = match api::api_key() {
        Ok(k) => k,
        Err(e) => {
            eprintln!("error: {e}");
            std::process::exit(1);
        }
    };
    match api::models(&key) {
        Ok(list) => {
            if args.json {
                for m in &list {
                    let line = serde_json::to_string(m).unwrap_or_else(|_| "{}".into());
                    println!("{line}");
                }
                return;
            }
            if list.is_empty() {
                println!("no models returned");
                return;
            }
            let mut rows: Vec<_> = list
                .iter()
                .map(|m| {
                    let ctx = m
                        .context_length
                        .map(|n| format!("{} ctx", render::compact(n)))
                        .unwrap_or_else(|| "-".into());
                    let name = m.name.as_deref().unwrap_or(&m.id);
                    (m.id.as_str(), name, ctx)
                })
                .collect();
            rows.sort_by(|a, b| a.1.cmp(b.1));
            for (id, name, ctx) in rows {
                println!("{name:<36} {ctx:<10} {id}");
            }
        }
        Err(e) => {
            eprintln!("error: {e}");
            std::process::exit(1);
        }
    }
}

fn statusline_cmd(args: &cli::Args) {
    let cfg = config::load();
    let s = snapshot::snapshot();
    let plan = render::plan_name(&s.sub.plan_id);
    let cap = render::plan_monthly_cap(&s.sub.plan_id).unwrap_or(0.0);
    if let Some(e) = &s.err {
        println!("cmduse: {e}");
        std::process::exit(1);
    }
    if args.json {
        print!(
            "{{\"plan\":\"{plan}\",\"monthlyRemaining\":{:.2},\"monthlyCap\":{:.2},\"fiveHourUsed\":{:.2},\"fiveHourCap\":{:.2},\"weeklyUsed\":{:.2},\"weeklyCap\":{:.2}}}",
            s.credits.credits.monthly_credits,
            cap,
            s.credits.window_limits.five_hour.as_ref().map(|w| w.used).unwrap_or(0.0),
            s.credits.window_limits.five_hour.as_ref().map(|w| w.cap).unwrap_or(0.0),
            s.credits.window_limits.weekly.as_ref().map(|w| w.used).unwrap_or(0.0),
            s.credits.window_limits.weekly.as_ref().map(|w| w.cap).unwrap_or(0.0),
        );
        return;
    }
    let h5 = s
        .credits
        .window_limits
        .five_hour
        .as_ref()
        .map(|w| (w.used, w.cap));
    let wk = s
        .credits
        .window_limits
        .weekly
        .as_ref()
        .map(|w| (w.used, w.cap));
    let d = report_render::StatusData {
        plan,
        monthly_remaining: s.credits.credits.monthly_credits,
        monthly_cap: cap,
        five_hour: &h5,
        weekly: &wk,
        bar_width: cfg.bar_width.min(30),
        colors: cfg.statusline_colors,
        ascii: cfg.statusline_ascii,
    };
    print!("{}", report_render::render_statusline(&cfg.statusline_template, &d));
}