cmd-usage 0.7.28

Live Command Code (commandcode.ai) usage dashboard: credits, 5-hour and weekly windows, plan limits. Watch mode or one-shot.
Documentation
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
use crate::render::{color_for, compact, money, BOLD, CYAN, DIM, RESET};

/// Output format for the report tables. JSON and CSV are machine-readable
/// (never colored); `Table` honors the caller's color decision.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Fmt {
    Table,
    Json,
    Csv,
}

/// RFC 4180 field: quote when it holds a comma, quote, or newline. Shared with
/// ocuse's reports, so neither side hand-rolls quoting.
pub fn csv_field(s: &str) -> String {
    if s.contains([',', '"', '\n', '\r']) {
        format!("\"{}\"", s.replace('"', "\"\""))
    } else {
        s.to_string()
    }
}

/// All token columns incl. cache read + write — the displayed "Tokens" total
/// must not silently drop cache-write spend.
fn tokens(t: &crate::reports::Totals) -> u64 {
    t.usage.input_tokens
        + t.usage.output_tokens
        + t.usage.cache_read_tokens
        + t.usage.cache_write_tokens
}

pub fn bar(pct: f64, width: usize, ascii: bool, colors: bool) -> String {
    let filled = ((pct / 100.0).clamp(0.0, 1.0) * width as f64).round() as usize;
    let (full, empty) = if ascii { ("#", "-") } else { ("━", "╱") };
    if !colors {
        return format!(
            "{pct:>5.1}% {}{}",
            full.repeat(filled),
            empty.repeat(width - filled)
        );
    }
    let color = color_for(pct);
    format!(
        "{color}{pct:>5.1}%{} {}{}{}",
        RESET,
        full.repeat(filled),
        DIM,
        empty.repeat(width - filled)
    )
}

fn pct_of(w: &Option<(f64, f64)>) -> f64 {
    match w {
        Some((u, c)) if *c > 0.0 => u / c * 100.0,
        _ => 0.0,
    }
}

/// What a statusline can show. Fields map 1:1 to template placeholders.
pub struct StatusData<'a> {
    pub plan: &'a str,
    pub monthly_remaining: f64,
    pub monthly_cap: f64,
    pub five_hour: &'a Option<(f64, f64)>,
    pub weekly: &'a Option<(f64, f64)>,
    /// Pre-rendered "on pace to hit cap in …" text, if a pace warning applies.
    pub five_hour_eta: Option<String>,
    pub weekly_eta: Option<String>,
    pub bar_width: usize,
    pub colors: bool,
    pub ascii: bool,
}

/// Template placeholders (case-insensitive):
///   {plan}          plan name
///   {credits}       remaining monthly credits ($x.xx)
///   {cap}           monthly cap ($x.xx)
///   {credits_bar}   monthly usage bar (used%)
///   {5h_bar}        5-hour window bar
///   {5h_pct}        5-hour used %
///   {5h_used} {5h_cap}
///   {5h_eta}        5-hour pace ETA ("on pace to hit cap in 2h 3m") or empty
///   {wk_bar}        weekly bar
///   {wk_pct} {wk_used} {wk_cap}
///   {wk_eta}        weekly pace ETA or empty
///   | or newline    segment separators
/// Unknown placeholders are dropped. Plain text passes through.
pub fn render_statusline(tpl: &str, d: &StatusData) -> String {
    render_template(tpl, |key| placeholder(key, d))
}

/// Neutral `{key}` template engine, shared by cmduse's statusline and ocuse's:
/// literal text passes through, each `{key}` is replaced by `resolve(key)`
/// (lower-cased), a `{` with no closing brace is emitted verbatim, and `|` /
/// newlines are literal separators. An unknown key resolves to whatever
/// `resolve` returns (the statusline drops it by returning an empty string).
pub fn render_template(tpl: &str, resolve: impl Fn(&str) -> String) -> String {
    let mut out = String::new();
    let mut rest = tpl;
    while let Some(start) = rest.find('{') {
        out.push_str(&rest[..start]);
        let after = &rest[start + 1..];
        match after.find('}') {
            Some(end) => {
                let key = after[..end].to_lowercase();
                out.push_str(&resolve(&key));
                rest = &after[end + 1..];
            }
            None => {
                // no closing brace: emit rest verbatim
                out.push('{');
                out.push_str(after);
                rest = "";
            }
        }
    }
    out.push_str(rest);
    out
}

fn placeholder(key: &str, d: &StatusData) -> String {
    let bar = |p: f64| bar(p, d.bar_width, d.ascii, d.colors);
    match key {
        "plan" => d.plan.to_string(),
        "credits" => {
            if d.colors {
                format!("{CYAN}{}{RESET}", money(d.monthly_remaining))
            } else {
                money(d.monthly_remaining)
            }
        }
        "cap" => money(d.monthly_cap),
        "credits_bar" => {
            let pct = if d.monthly_cap > 0.0 {
                (d.monthly_cap - d.monthly_remaining) / d.monthly_cap * 100.0
            } else {
                0.0
            };
            bar(pct)
        }
        "5h_bar" => bar(pct_of(d.five_hour)),
        "5h_pct" => format!("{:.1}%", pct_of(d.five_hour)),
        "5h_used" => money(d.five_hour.map(|(u, _)| u).unwrap_or(0.0)),
        "5h_cap" => money(d.five_hour.map(|(_, c)| c).unwrap_or(0.0)),
        "5h_eta" => d.five_hour_eta.clone().unwrap_or_default(),
        "wk_bar" => bar(pct_of(d.weekly)),
        "wk_pct" => format!("{:.1}%", pct_of(d.weekly)),
        "wk_used" => money(d.weekly.map(|(u, _)| u).unwrap_or(0.0)),
        "wk_cap" => money(d.weekly.map(|(_, c)| c).unwrap_or(0.0)),
        "wk_eta" => d.weekly_eta.clone().unwrap_or_default(),
        _ => String::new(),
    }
}

// ---- tables ----

fn totals_json(t: &crate::reports::Totals) -> serde_json::Value {
    serde_json::json!({
        "requests": t.requests,
        "tokensIn": t.usage.input_tokens,
        "tokensOut": t.usage.output_tokens,
        "cacheRead": t.usage.cache_read_tokens,
        "cacheWrite": t.usage.cache_write_tokens,
        "costUsd": t.usage.cost_usd,
    })
}

/// serde_json Map from sorted (key, Totals) pairs — keys are escaped.
fn totals_map<'a, I>(items: I) -> serde_json::Map<String, serde_json::Value>
where
    I: Iterator<Item = (&'a String, &'a crate::reports::Totals)>,
{
    items.map(|(k, t)| (k.clone(), totals_json(t))).collect()
}

fn table_header(colors: bool) -> String {
    let (b, r) = if colors { (BOLD, RESET) } else { ("", "") };
    format!(
        " {b}{:<12}{r} {:>6} {:>10} {:>10} {:>10} {:>10} {:>10}\n",
        "Day", "Reqs", "In", "Out", "Cache rd", "Cache wr", "Cost"
    )
}

fn table_row(day: &str, t: &crate::reports::Totals, dim: bool) -> String {
    let (d_open, d_close): (&str, &str) = if dim { (DIM, RESET) } else { ("", "") };
    format!(
        " {d_open}{:<12}{d_close} {:>6} {:>10} {:>10} {:>10} {:>10} {:>10}\n",
        day,
        compact(t.requests),
        compact(t.usage.input_tokens),
        compact(t.usage.output_tokens),
        compact(t.usage.cache_read_tokens),
        compact(t.usage.cache_write_tokens),
        money(t.usage.cost_usd),
    )
}

/// Daily usage table (local or account scope). JSON key and heading differ.
pub fn table(
    scope: &str,
    subtitle: &str,
    by_day: &crate::reports::ByDay,
    total: &crate::reports::Totals,
    days: Option<usize>,
    fmt: Fmt,
    colors: bool,
) -> String {
    match fmt {
        Fmt::Json => serde_json::json!({
            "scope": scope,
            "total": totals_json(total),
            "days": totals_map(by_day.iter()),
        })
        .to_string(),
        Fmt::Csv => {
            let mut o =
                String::from("day,requests,tokens_in,tokens_out,cache_read,cache_write,cost_usd\n");
            for (day, t) in by_day {
                o.push_str(&format!(
                    "{},{},{},{},{},{},{:.2}\n",
                    csv_field(day),
                    t.requests,
                    t.usage.input_tokens,
                    t.usage.output_tokens,
                    t.usage.cache_read_tokens,
                    t.usage.cache_write_tokens,
                    t.usage.cost_usd,
                ));
            }
            o.push_str(&format!(
                "total,{},{},{},{},{},{:.2}\n",
                total.requests,
                total.usage.input_tokens,
                total.usage.output_tokens,
                total.usage.cache_read_tokens,
                total.usage.cache_write_tokens,
                total.usage.cost_usd,
            ));
            o
        }
        Fmt::Table => {
            let (b, r) = if colors { (BOLD, RESET) } else { ("", "") };
            let (d, dr) = if colors { (DIM, RESET) } else { ("", "") };
            let mut o = format!(
                "{b}{}{r} {d}({subtitle}){dr}\n\n",
                if scope == "account" {
                    "Account usage"
                } else {
                    "Local usage"
                }
            );
            o.push_str(&table_header(colors));
            let day_count = by_day.len();
            let last_n = days.unwrap_or(usize::MAX);
            for (day, t) in by_day.iter().skip(day_count.saturating_sub(last_n)) {
                o.push_str(&table_row(day, t, false));
            }
            o.push_str(&table_row("total", total, colors));
            o
        }
    }
}

/// Hourly account usage table
pub fn hourly_table(
    rows: &[(String, crate::reports::Totals)],
    fmt: Fmt,
    source: &str,
    colors: bool,
) -> String {
    match fmt {
        Fmt::Json => serde_json::json!({
            "scope": "hourly",
            "source": source.replace(' ', "-"),
            "hours": totals_map(rows.iter().map(|(h, t)| (h, t))),
        })
        .to_string(),
        Fmt::Csv => {
            let mut o = String::from("hour,requests,tokens_in,tokens_out,cost_usd\n");
            for (h, t) in rows {
                o.push_str(&format!(
                    "{},{},{},{},{:.2}\n",
                    csv_field(h),
                    t.requests,
                    t.usage.input_tokens,
                    t.usage.output_tokens,
                    t.usage.cost_usd,
                ));
            }
            o
        }
        Fmt::Table => {
            let (b, r) = if colors { (BOLD, RESET) } else { ("", "") };
            let (d, dr) = if colors { (DIM, RESET) } else { ("", "") };
            let mut o = format!("{b}Usage by hour{r} {d}({source}){dr}\n\n");
            o.push_str(&format!(
                " {b}{:<14}{r} {:>6} {:>10} {:>10} {:>10}\n",
                "Hour", "Reqs", "In", "Out", "Cost"
            ));
            for (h, t) in rows {
                o.push_str(&format!(
                    " {:<14} {:>6} {:>10} {:>10} {:>10}\n",
                    h,
                    compact(t.requests),
                    compact(t.usage.input_tokens),
                    compact(t.usage.output_tokens),
                    money(t.usage.cost_usd),
                ));
            }
            o
        }
    }
}

/// Rows in the order the other two binaries print them: biggest spend first,
/// name as the tiebreak. A `BTreeMap` iterates alphabetically, which buried the
/// top spender — `ocuse` and `arkuse` have always sorted by spend.
fn by_spend<'a, M>(map: &'a M) -> Vec<(&'a String, &'a crate::reports::Totals)>
where
    &'a M: IntoIterator<Item = (&'a String, &'a crate::reports::Totals)>,
{
    let mut rows: Vec<_> = map.into_iter().collect();
    rows.sort_by(|a, b| {
        b.1.usage
            .cost_usd
            .total_cmp(&a.1.usage.cost_usd)
            .then_with(|| a.0.cmp(b.0))
    });
    rows
}

/// `rows` caps the table only: the machine formats (`--json`/`--csv`) always
/// carry every row, because a report is data and truncating it would silently
/// change what a consumer reads. `usize::MAX` (and `--rows 0`) means all.
pub fn project_table(
    by_project: &crate::reports::ByProject,
    fmt: Fmt,
    colors: bool,
    rows: usize,
) -> String {
    match fmt {
        Fmt::Json => serde_json::json!({ "projects": totals_map(by_project.iter()) }).to_string(),
        Fmt::Csv => {
            let mut o = String::from("project,requests,tokens,cost_usd\n");
            for (p, t) in by_spend(by_project) {
                o.push_str(&format!(
                    "{},{},{},{:.2}\n",
                    csv_field(p),
                    t.requests,
                    tokens(t),
                    t.usage.cost_usd,
                ));
            }
            o
        }
        Fmt::Table => {
            let (b, r) = if colors { (BOLD, RESET) } else { ("", "") };
            let (d, dr) = if colors { (DIM, RESET) } else { ("", "") };
            let mut o = format!("\n{b}By project{r}\n");
            o.push_str(&format!(
                " {b}{:<32}{r} {:>6} {:>10} {:>10}\n",
                "Project", "Reqs", "Tokens", "Cost"
            ));
            for (p, t) in by_spend(by_project).into_iter().take(rows) {
                o.push_str(&format!(
                    " {:<32} {:>6} {:>10} {:>10}\n",
                    p,
                    compact(t.requests),
                    compact(tokens(t)),
                    money(t.usage.cost_usd),
                ));
            }
            if by_project.len() > rows {
                o.push_str(&format!(" {d}… {} more{dr}\n", by_project.len() - rows));
            }
            o
        }
    }
}

pub fn model_table(
    by_model: &crate::reports::ByModel,
    fmt: Fmt,
    colors: bool,
    since: Option<&str>,
    rows: usize,
) -> String {
    match fmt {
        Fmt::Json => serde_json::json!({
            "source": "local",
            "since": since,
            "models": totals_map(by_model.iter()),
        })
        .to_string(),
        Fmt::Csv => {
            let mut o = String::from("model,requests,tokens,cost_usd\n");
            for (m, t) in by_spend(by_model) {
                o.push_str(&format!(
                    "{},{},{},{:.2}\n",
                    csv_field(m),
                    t.requests,
                    tokens(t),
                    t.usage.cost_usd,
                ));
            }
            o
        }
        Fmt::Table => {
            let (b, r) = if colors { (BOLD, RESET) } else { ("", "") };
            let (d, dr) = if colors { (DIM, RESET) } else { ("", "") };
            let mut o = format!("\n{b}By model{r}\n");
            o.push_str(&format!(
                " {b}{:<32}{r} {:>6} {:>10} {:>10}\n",
                "Model", "Reqs", "Tokens", "Cost"
            ));
            let total_cost: f64 = by_model.values().map(|t| t.usage.cost_usd).sum();
            for (m, t) in by_spend(by_model).into_iter().take(rows) {
                let share = if total_cost > 0.0 {
                    format!(" {d}{:>5.1}%{dr}", t.usage.cost_usd / total_cost * 100.0)
                } else {
                    String::new()
                };
                o.push_str(&format!(
                    " {:<32} {:>6} {:>10} {:>10}{share}\n",
                    m,
                    compact(t.requests),
                    compact(tokens(t)),
                    money(t.usage.cost_usd),
                ));
            }
            if by_model.len() > rows {
                o.push_str(&format!(" {d}… {} more{dr}\n", by_model.len() - rows));
            }
            o
        }
    }
}

/// One model's figures from the local report. `ocuse model <id>` and
/// `arkuse model <id>` have had this since they gained a `model` positional;
/// without it `cmduse model <id>` was the family's one missing capability.
pub fn model_detail(
    by_model: &crate::reports::ByModel,
    id: &str,
    fmt: Fmt,
    colors: bool,
    since: Option<&str>,
) -> String {
    let hit = by_model.get_key_value(id);
    let total: f64 = by_model.values().map(|t| t.usage.cost_usd).sum();
    let share = |t: &crate::reports::Totals| {
        if total > 0.0 {
            t.usage.cost_usd / total * 100.0
        } else {
            0.0
        }
    };
    match fmt {
        Fmt::Json => match hit {
            None => {
                serde_json::json!({ "source": "local", "since": since, "model": null }).to_string()
            }
            Some((name, t)) => {
                let mut body = totals_json(t);
                body["id"] = serde_json::json!(name);
                body["share"] = serde_json::json!(share(t));
                serde_json::json!({ "source": "local", "since": since, "model": body }).to_string()
            }
        },
        Fmt::Csv => {
            let mut out = String::from("model,requests,tokens,cost_usd\n");
            if let Some((name, t)) = hit {
                out.push_str(&format!(
                    "{},{},{},{:.2}\n",
                    csv_field(name),
                    t.requests,
                    tokens(t),
                    t.usage.cost_usd,
                ));
            }
            out
        }
        Fmt::Table => {
            let (b, r) = if colors { (BOLD, RESET) } else { ("", "") };
            let (d, dr) = if colors { (DIM, RESET) } else { ("", "") };
            let Some((_, t)) = hit else {
                return format!("no local usage for {id} in this window\n");
            };
            let mut o = format!("\n{b}{id}{r} {d}(local logs){dr}\n");
            o.push_str(&format!("  window    {}\n", since_label(since)));
            o.push_str(&format!("  requests  {}\n", compact(t.requests)));
            o.push_str(&format!(
                "  tokens    {} ({} in / {} out)\n",
                compact(tokens(t)),
                compact(t.usage.input_tokens),
                compact(t.usage.output_tokens),
            ));
            if t.usage.cache_read_tokens + t.usage.cache_write_tokens > 0 {
                o.push_str(&format!(
                    "  cache     {} read / {} written\n",
                    compact(t.usage.cache_read_tokens),
                    compact(t.usage.cache_write_tokens),
                ));
            }
            o.push_str(&format!("  cost      {}\n", money(t.usage.cost_usd)));
            if total > 0.0 {
                o.push_str(&format!(
                    "  share     {:.1}% of the window's spend\n",
                    share(t)
                ));
            }
            o
        }
    }
}

/// The window line every detail view prints: the pinned instant when there is
/// one, the all-time default otherwise.
fn since_label(since: Option<&str>) -> String {
    match since {
        Some(s) => format!("from {s}"),
        None => "all local logs".into(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::reports::{ByModel, ByProject, Totals, Usage};

    #[test]
    fn model_detail_prints_one_row_its_share_and_the_window() {
        let mut by: ByModel = ByModel::new();
        by.insert(
            "zai-org/GLM-5.2".into(),
            Totals {
                requests: 4,
                usage: Usage {
                    input_tokens: 1_000,
                    output_tokens: 500,
                    cache_read_tokens: 2_000,
                    cache_write_tokens: 0,
                    cost_usd: 3.0,
                },
            },
        );
        by.insert(
            "other".into(),
            Totals {
                requests: 1,
                usage: Usage {
                    cost_usd: 1.0,
                    ..Default::default()
                },
            },
        );
        let text = model_detail(&by, "zai-org/GLM-5.2", Fmt::Table, false, None);
        assert!(text.contains("zai-org/GLM-5.2 (local logs)"), "{text}");
        assert!(text.contains("3.5K (1.0K in / 500 out)"), "{text}");
        assert!(text.contains("2.0K read / 0 written"), "{text}");
        assert!(text.contains("75.0% of the window's spend"), "{text}");
        let text = model_detail(
            &by,
            "zai-org/GLM-5.2",
            Fmt::Table,
            false,
            Some("2026-08-27T00:00:00Z"),
        );
        assert!(text.contains("from 2026-08-27T00:00:00Z"), "{text}");
        // An id the window never saw is a message, not an empty frame.
        let text = model_detail(&by, "nope", Fmt::Table, false, None);
        assert_eq!(text, "no local usage for nope in this window\n");
        let v: serde_json::Value =
            serde_json::from_str(&model_detail(&by, "other", Fmt::Json, false, None)).unwrap();
        assert_eq!(v["model"]["costUsd"], 1.0);
        assert_eq!(v["model"]["share"], 25.0);
        let csv = model_detail(&by, "other", Fmt::Csv, false, None);
        assert_eq!(csv.lines().count(), 2);
        assert_eq!(csv.lines().nth(1).unwrap(), "other,1,0,1.00");
    }

    #[test]
    fn tables_put_the_biggest_spender_first() {
        // ocuse and arkuse sort their reports by spend; cmduse's BTreeMap used
        // to iterate alphabetically, which buried the top spender under `z-ai`.
        let mut by: ByModel = ByModel::new();
        by.insert(
            "aaa-small".into(),
            Totals {
                requests: 1,
                usage: Usage {
                    cost_usd: 0.5,
                    ..Default::default()
                },
            },
        );
        by.insert(
            "zzz-big".into(),
            Totals {
                requests: 2,
                usage: Usage {
                    cost_usd: 9.5,
                    ..Default::default()
                },
            },
        );
        let text = model_table(&by, Fmt::Table, false, None, usize::MAX);
        let first_row = text.lines().nth(3).expect("header then a row");
        assert!(first_row.trim_start().starts_with("zzz-big"), "{text}");
        let csv = model_table(&by, Fmt::Csv, false, None, usize::MAX);
        assert!(csv.lines().nth(1).unwrap().starts_with("zzz-big"), "{csv}");
        // Same rule for the project table.
        let mut by_project = ByProject::new();
        by_project.insert(
            "a-project".into(),
            Totals {
                requests: 1,
                usage: Usage {
                    cost_usd: 0.5,
                    ..Default::default()
                },
            },
        );
        by_project.insert(
            "z-project".into(),
            Totals {
                requests: 2,
                usage: Usage {
                    cost_usd: 9.5,
                    ..Default::default()
                },
            },
        );
        let text = project_table(&by_project, Fmt::Table, false, usize::MAX);
        assert!(
            text.lines()
                .nth(3)
                .unwrap()
                .trim_start()
                .starts_with("z-project"),
            "{text}"
        );
    }

    #[test]
    fn project_json_escapes_keys() {
        let mut by: ByProject = ByProject::new();
        let t = Totals {
            requests: 1,
            usage: Usage {
                cost_usd: 0.5,
                ..Default::default()
            },
        };
        by.insert("we\"ird\\name".into(), t);
        let out = project_table(&by, Fmt::Json, false, usize::MAX);
        let v: serde_json::Value = serde_json::from_str(&out).unwrap();
        assert_eq!(v["projects"]["we\"ird\\name"]["requests"], 1);
    }

    #[test]
    fn csv_quotes_and_includes_cache_write() {
        let mut by: ByProject = ByProject::new();
        by.insert(
            "a,b".into(),
            Totals {
                requests: 2,
                usage: Usage {
                    input_tokens: 10,
                    output_tokens: 20,
                    cache_read_tokens: 30,
                    cache_write_tokens: 40,
                    cost_usd: 1.5,
                },
            },
        );
        let out = project_table(&by, Fmt::Csv, false, usize::MAX);
        assert_eq!(
            out,
            "project,requests,tokens,cost_usd\n\"a,b\",2,100,1.50\n"
        );
    }

    #[test]
    fn rows_cap_trims_the_table_and_never_the_machine_formats() {
        // `--rows` is a display cap: `cmduse model/session --rows N` must not
        // silently truncate `--json`, or a consumer reads a short report as the
        // whole period.
        let mut by: ByProject = ByProject::new();
        for name in ["a", "b", "c"] {
            by.insert(
                name.into(),
                Totals {
                    requests: 1,
                    ..Default::default()
                },
            );
        }
        let table = project_table(&by, Fmt::Table, false, 1);
        assert!(table.contains("… 2 more"), "{table}");
        let json = project_table(&by, Fmt::Json, false, 1);
        let v: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(v["projects"].as_object().unwrap().len(), 3);
        let csv = project_table(&by, Fmt::Csv, false, 1);
        assert_eq!(csv.lines().count(), 4, "header + every row: {csv}");
    }

    #[test]
    fn table_rows_include_cache_write_column() {
        let by: crate::reports::ByDay = [(
            "2026-09-27".to_string(),
            Totals {
                requests: 1,
                usage: Usage {
                    input_tokens: 1,
                    output_tokens: 2,
                    cache_read_tokens: 3,
                    cache_write_tokens: 4,
                    cost_usd: 0.1,
                },
            },
        )]
        .into_iter()
        .collect();
        let total = crate::reports::sum_days(&by);
        let out = table("local", "test", &by, &total, None, Fmt::Table, false);
        assert!(out.contains("Cache wr"));
        assert!(out.contains(" 4 "));
    }
}