tokmesh-cli 0.1.1

CLI and TUI for tokmesh — AI token usage analytics
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
use ratatui::prelude::*;
use ratatui::widgets::{Cell, ScrollbarState};
use tokmesh_core::ClientId;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

use crate::tui::client_ui;
use crate::tui::config::TokmeshConfig;
use crate::tui::themes::Theme;

pub fn format_tokens_compact(tokens: u64) -> String {
    if tokens >= 1_000_000_000 {
        format!("{:.1}B", tokens as f64 / 1_000_000_000.0)
    } else if tokens >= 1_000_000 {
        format!("{:.1}M", tokens as f64 / 1_000_000.0)
    } else if tokens >= 1_000 {
        format!("{}K", tokens / 1_000)
    } else {
        format_tokens_with_commas(tokens)
    }
}

pub fn format_tokens(tokens: u64) -> String {
    format_tokens_compact(tokens)
}

pub(crate) fn total_tokens_cell(total_tokens: u64, theme: &Theme) -> Cell<'static> {
    Cell::from(format_tokens(total_tokens)).style(theme.metric_total_style())
}

pub fn format_tokens_with_commas(n: u64) -> String {
    let s = n.to_string();
    let mut result = String::new();
    for (i, c) in s.chars().rev().enumerate() {
        if i > 0 && i % 3 == 0 {
            result.insert(0, ',');
        }
        result.insert(0, c);
    }
    result
}

pub fn format_cost(cost: f64) -> String {
    if !cost.is_finite() || cost < 0.0 {
        return "$0.00".to_string();
    }
    if cost >= 1000.0 {
        format!("${:.1}K", cost / 1000.0)
    } else {
        format!("${:.2}", cost)
    }
}

/// Cost per million tokens: useful for comparing model efficiency across sessions.
/// Returns "—" when there are no tokens to avoid division by zero.
pub fn format_cost_per_million(cost: f64, total_tokens: u64) -> String {
    if total_tokens == 0 || !cost.is_finite() || cost < 0.0 {
        return "\u{2014}".to_string(); //    }
    let per_m = cost / (total_tokens as f64) * 1_000_000.0;
    format!("${:.2}", per_m)
}

/// Cache reuse multiplier: cached reads per full-price input token.
/// `cache_read / (input + cache_write)` — how many low-cost reads you
/// got for every token you paid full price (fresh input or cache write).
pub fn format_cache_hit_rate(cache_read: u64, input: u64, cache_write: u64) -> String {
    let paid = input.saturating_add(cache_write);
    if paid == 0 {
        return if cache_read > 0 {
            "".to_string()
        } else {
            "".to_string()
        };
    }
    let ratio = cache_read as f64 / paid as f64;
    format!("{:.1}x", ratio)
}

pub fn format_ms_per_1k(ms_per_1k_tokens: Option<f64>) -> String {
    let Some(value) = ms_per_1k_tokens else {
        return "".to_string();
    };
    if !value.is_finite() || value <= 0.0 {
        "".to_string()
    } else if value >= 1000.0 {
        format!("{:.1}s", value / 1000.0)
    } else {
        format!("{:.0}ms", value)
    }
}

pub fn viewport_scrollbar_state(
    content_len: usize,
    scroll_offset: usize,
    viewport_len: usize,
) -> ScrollbarState {
    let viewport_len = viewport_len.max(1);
    ScrollbarState::new(content_len)
        .position(scrollbar_position(scroll_offset, content_len, viewport_len))
        .viewport_content_length(viewport_len)
}

/// Truncate a string to `max_chars` Unicode code points, appending "..." when
/// truncation occurs. Returns the original string when it fits, and an empty
/// string when `max_chars` is 0. Shared across all table-style tabs (Daily,
/// Monthly, Sessions, Models, Agents) so ellipsis behavior stays consistent.
///
/// Counts code points, not terminal cells, so a CJK or emoji string it calls
/// short is twice as wide as the column that asked for it and gets clipped by
/// the solver with no marker. Every caller passes a column width, so cells are
/// the unit they all actually want: prefer [`truncate_to_width`] for anything
/// that can hold a non-ASCII name. Kept as-is only because the Daily, Models
/// and Agents tables are pinned by golden renders that this commit does not
/// touch.
pub fn truncate_text(s: &str, max_chars: usize) -> String {
    if max_chars == 0 {
        return String::new();
    }
    let char_count = s.chars().count();
    if char_count <= max_chars {
        s.to_string()
    } else if max_chars <= 3 {
        s.chars().take(max_chars).collect()
    } else {
        let head: String = s.chars().take(max_chars - 3).collect();
        format!("{}...", head)
    }
}

/// Terminal cells `s` occupies, which is the unit ratatui lays tables out in.
pub fn display_width(s: &str) -> usize {
    UnicodeWidthStr::width(s)
}

/// Longest prefix of `s` that fits in `max_cells` terminal cells. Never splits
/// a grapheme, so the result can come in one cell short of the budget rather
/// than one over it.
pub fn prefix_to_width(s: &str, max_cells: usize) -> &str {
    let mut used = 0usize;
    // Grapheme clusters, not chars. A flag is two regional indicators and a
    // family emoji is a ZWJ sequence, so cutting on a char boundary can leave
    // half a flag or a dangling ZWJ -- and the width of the pieces does not
    // add up to the width of the whole, so the cut would not even respect the
    // budget it was measuring against.
    for (offset, cluster) in s.grapheme_indices(true) {
        let w = UnicodeWidthStr::width(cluster);
        if used + w > max_cells {
            return &s[..offset];
        }
        used += w;
    }
    s
}

/// [`truncate_text`] measured in terminal cells instead of code points: the
/// result never occupies more than `max_cells`, and carries a "..." whenever
/// anything was dropped. Use this wherever the text can hold a full-width
/// grapheme — a session title, a client display name — because those are the
/// cases where a code-point count and the ratatui solver disagree.
pub fn truncate_to_width(s: &str, max_cells: usize) -> String {
    if max_cells == 0 {
        return String::new();
    }
    if display_width(s) <= max_cells {
        return s.to_string();
    }
    if max_cells <= 3 {
        return prefix_to_width(s, max_cells).to_string();
    }
    format!("{}...", prefix_to_width(s, max_cells - 3))
}

fn scrollbar_position(scroll_offset: usize, content_len: usize, viewport_len: usize) -> usize {
    let max_scroll = content_len.saturating_sub(viewport_len);
    if max_scroll == 0 {
        0
    } else {
        ((scroll_offset.min(max_scroll) as u128) * (content_len.saturating_sub(1) as u128)
            / (max_scroll as u128)) as usize
    }
}

pub(crate) fn light_ratio_bar_spans(
    ratio: f64,
    width: usize,
    fill_style: Style,
    empty_style: Style,
) -> Vec<Span<'static>> {
    if width == 0 {
        return Vec::new();
    }

    let ratio = ratio.clamp(0.0, 1.0);
    let scaled = ratio * width as f64;
    let trace = ratio > 0.0 && ratio < 0.01 && scaled < 1.0;
    let filled = if ratio > 0.0 && !trace {
        (scaled.round() as usize).clamp(1, width)
    } else {
        0
    };
    let empty = width.saturating_sub(filled + usize::from(trace));

    let mut spans = Vec::with_capacity(3);
    if filled > 0 {
        spans.push(Span::styled("".repeat(filled), fill_style));
    }
    if trace {
        spans.push(Span::styled("", fill_style));
    }
    if empty > 0 {
        spans.push(Span::styled("·".repeat(empty), empty_style));
    }
    spans
}

pub(crate) fn truncate_ellipsis(s: &str, max_chars: usize) -> String {
    if max_chars == 0 {
        return String::new();
    }
    let char_count = s.chars().count();
    if char_count <= max_chars {
        s.to_string()
    } else if max_chars == 1 {
        "".to_string()
    } else {
        let head: String = s.chars().take(max_chars - 1).collect();
        format!("{head}")
    }
}

pub fn get_model_color(model: &str) -> Color {
    get_provider_shade(get_provider_from_model(model), 0)
}

/// Maps a lowercased provider id to its built-in vendor shade ramp, or `None`
/// when the provider has no known vendor palette. Kept as the single source of
/// truth so [`get_provider_shade`] and [`provider_has_palette`] never drift.
fn known_provider_palette(provider_lower: &str) -> Option<&'static [(u8, u8, u8)]> {
    let palette: &[(u8, u8, u8)] = match provider_lower {
        s if s.contains("anthropic") => &ANTHROPIC_SHADES,
        s if s.contains("openai") => &OPENAI_SHADES,
        s if s.contains("google") || s.contains("gemini") => &GOOGLE_SHADES,
        s if s.contains("deepseek") => &DEEPSEEK_SHADES,
        s if s.contains("xai") || s.contains("grok") => &XAI_SHADES,
        s if s.contains("zai") || s.contains("z_ai") || s.contains("zhipu") => &ZAI_SHADES,
        s if s.contains("moonshot") || s.contains("kimi") => &MOONSHOT_SHADES,
        s if s.contains("meta") || s.contains("llama") => &META_SHADES,
        s if s.contains("cursor") => &CURSOR_SHADES,
        s if s.contains("sakana") || s.contains("fugu") => &SAKANA_SHADES,
        _ => return None,
    };
    Some(palette)
}

/// Whether `provider` resolves to a known vendor color — either a built-in
/// shade ramp or a user `[colors.providers]` override. Gateway providers that
/// resell other vendors' models (e.g. `github-copilot`, `openrouter`) have no
/// vendor palette of their own and return `false`, so callers can color by the
/// model's own vendor instead of the neutral "unknown" gray.
pub fn provider_has_palette(provider: &str) -> bool {
    TokmeshConfig::load().get_provider_color(provider).is_some()
        || known_provider_palette(&provider.to_lowercase()).is_some()
}

/// Returns the shade for a given `(provider, rank)` pair.
/// Honors `[colors.providers]` config overrides at every rank by deriving
/// a 7-step lighten-to-white palette from the override base color.
pub fn get_provider_shade(provider: &str, rank: usize) -> Color {
    if let Some(base) = TokmeshConfig::load().get_provider_color(provider) {
        return shade_from_base(base, rank);
    }

    let p = provider.to_lowercase();
    let palette = known_provider_palette(&p).unwrap_or(&UNKNOWN_SHADES);

    let idx = rank.min(palette.len() - 1);
    let (r, g, b) = palette[idx];
    Color::Rgb(r, g, b)
}

/// Generates a 7-step monochromatic palette from `base` by interpolating
/// toward white. Factors roughly match the end-of-ramp lightness of the
/// hardcoded palettes so overrides feel visually consistent.
fn shade_from_base(base: Color, rank: usize) -> Color {
    const FACTORS: [f32; 7] = [0.00, 0.11, 0.22, 0.33, 0.44, 0.56, 0.67];
    let Color::Rgb(r, g, b) = base else {
        return base;
    };
    let idx = rank.min(FACTORS.len() - 1);
    let f = FACTORS[idx];
    let lerp = |c: u8| -> u8 {
        let c = c as f32;
        (c + (255.0 - c) * f).round().clamp(0.0, 255.0) as u8
    };
    Color::Rgb(lerp(r), lerp(g), lerp(b))
}

const ANTHROPIC_SHADES: [(u8, u8, u8); 7] = [
    (218, 119, 86),  // #DA7756
    (223, 136, 107), // #DF886B
    (227, 153, 128), // #E39980
    (232, 170, 149), // #E8AA95
    (236, 184, 166), // #ECB8A6
    (239, 197, 183), // #EFC5B7
    (243, 210, 199), // #F3D2C7
];

const OPENAI_SHADES: [(u8, u8, u8); 7] = [
    (16, 185, 129),  // #10B981
    (18, 208, 145),  // #12D091
    (20, 232, 162),  // #14E8A2
    (41, 236, 172),  // #29ECAC
    (61, 238, 179),  // #3DEEB3
    (97, 241, 193),  // #61F1C1
    (133, 244, 208), // #85F4D0
];

const GOOGLE_SHADES: [(u8, u8, u8); 7] = [
    (59, 130, 246),  // #3B82F6
    (83, 146, 247),  // #5392F7
    (108, 161, 248), // #6CA1F8
    (132, 177, 249), // #84B1F9
    (153, 190, 250), // #99BEFA
    (172, 202, 251), // #ACCAFB
    (190, 214, 252), // #BED6FC
];

const DEEPSEEK_SHADES: [(u8, u8, u8); 7] = [
    (6, 182, 212),   // #06B6D4
    (7, 203, 237),   // #07CBED
    (21, 215, 248),  // #15D7F8
    (45, 219, 249),  // #2DDBF9
    (66, 223, 250),  // #42DFFA
    (85, 226, 250),  // #55E2FA
    (105, 229, 251), // #69E5FB
];

const XAI_SHADES: [(u8, u8, u8); 7] = [
    (234, 179, 8),   // #EAB308
    (247, 192, 21),  // #F7C015
    (248, 199, 45),  // #F8C72D
    (249, 205, 70),  // #F9CD46
    (249, 211, 91),  // #F9D35B
    (250, 216, 110), // #FAD86E
    (251, 221, 129), // #FBDD81
];

const ZAI_SHADES: [(u8, u8, u8); 7] = [
    (168, 85, 247),  // #A855F7
    (181, 110, 249), // #B56EF9
    (193, 132, 250), // #C184FA
    (204, 153, 251), // #CC99FB
    (214, 172, 252), // #D6ACFC
    (224, 192, 253), // #E0C0FD
    (235, 213, 254), // #EBD5FE
];

const MOONSHOT_SHADES: [(u8, u8, u8); 7] = [
    (20, 184, 166),  // #14B8A6
    (35, 197, 178),  // #23C5B2
    (58, 207, 190),  // #3ACFBE
    (85, 216, 202),  // #55D8CA
    (112, 224, 212), // #70E0D4
    (143, 232, 222), // #8FE8DE
    (174, 240, 232), // #AEF0E8
];

const META_SHADES: [(u8, u8, u8); 7] = [
    (99, 102, 241),  // #6366F1
    (122, 125, 243), // #7A7DF3
    (146, 148, 245), // #9294F5
    (169, 171, 247), // #A9ABF7
    (189, 190, 249), // #BDBEF9
    (207, 208, 251), // #CFD0FB
    (225, 226, 252), // #E1E2FC
];

const CURSOR_SHADES: [(u8, u8, u8); 7] = [
    (139, 92, 246),  // #8B5CF6
    (154, 114, 247), // #9A72F7
    (169, 135, 248), // #A987F8
    (184, 156, 250), // #B89CFA
    (199, 177, 251), // #C7B1FB
    (215, 199, 252), // #D7C7FC
    (230, 220, 253), // #E6DCFD
];

/// Sakana (Fugu) red — the brand's "one red fish leading the school" accent.
const SAKANA_SHADES: [(u8, u8, u8); 7] = [
    (219, 43, 31),   // #DB2B1F
    (223, 66, 56),   // #DF4238
    (227, 90, 80),   // #E35A50
    (231, 113, 105), // #E77169
    (235, 136, 130), // #EB8882
    (239, 162, 156), // #EFA29C
    (243, 185, 181), // #F3B9B5
];

/// Neutral gray ramp for providers that don't match any known palette.
/// Still produces distinct shades per rank instead of collapsing to white.
const UNKNOWN_SHADES: [(u8, u8, u8); 7] = [
    (136, 136, 136), // #888888
    (156, 156, 156), // #9C9C9C
    (176, 176, 176), // #B0B0B0
    (196, 196, 196), // #C4C4C4
    (212, 212, 212), // #D4D4D4
    (228, 228, 228), // #E4E4E4
    (244, 244, 244), // #F4F4F4
];

fn is_delimited_model_family(model_lower: &str, family: &str) -> bool {
    model_lower
        .split(|c: char| !c.is_ascii_alphanumeric())
        .any(|token| {
            token == family
                || token
                    .strip_prefix(family)
                    .and_then(|suffix| suffix.chars().next())
                    .is_some_and(|c| c.is_ascii_digit())
        })
}

pub fn get_provider_from_model(model: &str) -> &'static str {
    let model_lower = model.to_lowercase();

    if model_lower.contains("claude")
        || model_lower.contains("sonnet")
        || model_lower.contains("opus")
        || model_lower.contains("haiku")
        // Match "fable" only as a delimited token (mirrors core's
        // provider_identity::contains_delimited) so unrelated names like
        // "unfabled-x" don't get misattributed to Anthropic.
        || model_lower
            .split(|c: char| !c.is_ascii_alphanumeric())
            .any(|token| token == "fable")
    {
        "anthropic"
    } else if model_lower.contains("gpt")
        || model_lower.starts_with("o1")
        || model_lower.starts_with("o3")
        || model_lower.contains("codex")
        || model_lower.contains("text-embedding")
        || model_lower.contains("dall-e")
        || model_lower.contains("whisper")
        || model_lower.contains("tts")
    {
        "openai"
    } else if model_lower.contains("gemini") {
        "google"
    } else if model_lower.contains("deepseek") {
        "deepseek"
    } else if model_lower.contains("grok") {
        "xai"
    } else if is_delimited_model_family(&model_lower, "glm") {
        "zai"
    } else if is_delimited_model_family(&model_lower, "kimi") {
        "moonshotai"
    } else if model_lower.contains("llama") {
        "meta"
    } else if model_lower.contains("mixtral") {
        "mistral"
    } else if model_lower == "auto" || model_lower.contains("cursor") {
        "cursor"
    } else {
        "unknown"
    }
}

pub fn get_client_color(client: &str) -> Color {
    let config = TokmeshConfig::load();
    if let Some(color) = config.get_client_color(client) {
        return color;
    }
    match client.to_lowercase().as_str() {
        "opencode" => Color::Rgb(34, 197, 94),     // #22c55e
        "claude" => Color::Rgb(218, 119, 86),      // #DA7756 Claude brand coral
        "codex" => Color::Rgb(59, 130, 246),       // #3b82f6
        "cursor" => Color::Rgb(168, 85, 247),      // #a855f7
        "gemini" => Color::Rgb(6, 182, 212),       // #06b6d4
        "amp" => Color::Rgb(236, 72, 153),         // #EC4899
        "droid" => Color::Rgb(16, 185, 129),       // #10b981
        "openclaw" => Color::Rgb(239, 68, 68),     // #ef4444
        "hermes" => Color::Rgb(255, 215, 0),       // #ffd700
        "goose" => Color::Rgb(100, 180, 220),      // #64b4dc
        "codebuff" => Color::Rgb(124, 58, 237),    // #7C3AED Codebuff brand purple
        "antigravity" => Color::Rgb(99, 102, 241), // #6366F1 Antigravity indigo
        "zed" => Color::Rgb(8, 76, 207),           // #084CCF Zed blue
        "warp" => Color::Rgb(1, 155, 150),         // #019B96 Warp teal
        "gjc" => Color::Rgb(220, 38, 38),          // #DC2626 gajae-code red-claw
        "jcode" => Color::Rgb(245, 158, 11),       // #F59E0B Jcode amber
        "junie" => Color::Rgb(123, 97, 255),       // #7B61FF Junie violet
        _ => Color::Rgb(136, 136, 136),            // #888888
    }
}

pub fn get_client_display_name(client: &str) -> String {
    let config = TokmeshConfig::load();
    if let Some(name) = config.get_client_display_name(client) {
        return name.to_string();
    }
    let client_lower = client.to_lowercase();
    if client_lower == ClientId::OpenClaw.as_str() {
        return "🦞 OpenClaw".to_string();
    }
    if let Some(client_id) = ClientId::from_str(&client_lower) {
        return client_ui::display_name(client_id).to_string();
    }
    client.to_string()
}

pub fn get_provider_display_name(provider: &str) -> String {
    let config = TokmeshConfig::load();
    if let Some(name) = config.get_provider_display_name(provider) {
        return name.to_string();
    }

    // Merged Models rows store multiple providers as a ", "-joined string
    // (aggregate_model_usage_entries sorts + dedups + joins). Map EACH segment
    // independently and rejoin, otherwise a prefix/brand branch below would
    // match the whole string and silently drop the rest — e.g.
    // "openai, openrouter" must render "OpenAI, OpenRouter", not just "OpenAI".
    if provider.contains(", ") {
        return provider
            .split(", ")
            .map(|segment| map_single_provider(segment, config))
            .collect::<Vec<_>>()
            .join(", ");
    }

    map_single_provider(provider, config)
}

/// Display name for a SINGLE provider id (no comma-joined lists — the public
/// `get_provider_display_name` splits those first).
fn map_single_provider(provider: &str, config: &TokmeshConfig) -> String {
    if let Some(name) = config.get_provider_display_name(provider) {
        return name.to_string();
    }
    let lower = provider.to_lowercase();
    match lower.as_str() {
        "anthropic" => return "Anthropic".to_string(),
        "google" => return "Google".to_string(),
        "cursor" => return "Cursor".to_string(),
        "deepseek" => return "DeepSeek".to_string(),
        "xai" => return "xAI".to_string(),
        "meta" => return "Meta".to_string(),
        "mistral" => return "Mistral".to_string(),
        "cohere" => return "Cohere".to_string(),
        "opencode" => return "OpenCode".to_string(),
        "openrouter" => return "OpenRouter".to_string(),
        // `canonical_provider` rewrites `google-vertex` → `google_vertex`, so
        // accept both spellings here.
        "google-vertex" | "google_vertex" => return "Google Vertex".to_string(),
        _ => {}
    }

    // Brand families: any provider id that starts with these stems collapses to
    // the brand name. Covers `openai`, `openai-codex`, `kimi`, `kimi-code`,
    // `kimi-for-coding`, etc. without enumerating every variant.
    if lower.starts_with("openai") {
        return "OpenAI".to_string();
    }
    if lower.starts_with("kimi") {
        return "Kimi".to_string();
    }
    if lower.starts_with("github-cop") || lower.contains("copilot") {
        return "GitHub Copilot".to_string();
    }

    // Smart fallback: split on `-`, `_`, and whitespace, title-case each word,
    // and map known acronyms/brands per word. So unknown multi-word providers
    // like `google-vertex` → "Google Vertex" and `some-new-provider` →
    // "Some New Provider".
    smart_titlecase(provider)
}

/// Title-cases a provider/brand identifier word-by-word, splitting on `-`, `_`,
/// and whitespace. Per-word acronym/brand overrides (e.g. `ai` → "AI",
/// `gpt` → "GPT") win over plain capitalization. Empty input yields an empty
/// string; runs of separators are collapsed.
fn smart_titlecase(s: &str) -> String {
    s.split(['-', '_', ' '])
        .filter(|word| !word.is_empty())
        .map(titlecase_word)
        .collect::<Vec<_>>()
        .join(" ")
}

fn titlecase_word(word: &str) -> String {
    match word.to_lowercase().as_str() {
        "ai" => "AI".to_string(),
        "gpt" => "GPT".to_string(),
        "openai" => "OpenAI".to_string(),
        "xai" => "xAI".to_string(),
        "vertex" => "Vertex".to_string(),
        "llm" => "LLM".to_string(),
        "api" => "API".to_string(),
        _ => capitalize_first(word),
    }
}

fn capitalize_first(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        None => String::new(),
        Some(first) => first.to_uppercase().chain(chars).collect(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn truncate_to_width_never_exceeds_its_budget() {
        // The case `truncate_text` gets wrong: 8 code points, 16 cells.
        let cjk = "세션제목한글로";
        assert_eq!(truncate_text(cjk, 10), cjk, "code points say it fits");
        assert!(
            display_width(&truncate_to_width(cjk, 10)) <= 10,
            "cells say it does not"
        );
        for budget in 0..=20 {
            for s in [cjk, "ascii-session-title", "🦞 OpenClaw", ""] {
                assert!(
                    display_width(&truncate_to_width(s, budget)) <= budget,
                    "{s:?} overflowed a {budget}-cell budget"
                );
            }
        }
    }

    #[test]
    fn truncate_to_width_marks_what_it_dropped() {
        assert_eq!(truncate_to_width("abcdefghij", 10), "abcdefghij");
        assert_eq!(truncate_to_width("abcdefghijk", 10), "abcdefg...");
        assert_eq!(truncate_to_width("abcdefghijk", 3), "abc");
        assert_eq!(truncate_to_width("abcdefghijk", 0), "");
        // 7 cells of budget, 3 for the marker: two syllables (4 cells) fit and
        // the third would overshoot, so the result is one cell short, not one
        // cell over.
        assert_eq!(truncate_to_width("세션제목", 7), "세션...");
    }

    #[test]
    fn prefix_to_width_never_splits_a_grapheme() {
        assert_eq!(prefix_to_width("세션제목", 5), "세션");
        assert_eq!(prefix_to_width("세션제목", 6), "세션제");
        assert_eq!(prefix_to_width("abc", 10), "abc");
        assert_eq!(prefix_to_width("abc", 0), "");

        // The cases the name actually promises. Hangul is one char per
        // grapheme, so the assertions above hold under char iteration too and
        // never exercised the claim. These are multi-char graphemes: a flag is
        // two regional indicators and a family is a ZWJ sequence, so char-wise
        // truncation yields half a flag or a dangling ZWJ.
        let flag = "\u{1F1F0}\u{1F1F7}"; // KR
        let family = "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}";

        for (label, s) in [("flag", flag), ("family", family)] {
            assert_eq!(
                prefix_to_width(s, 1),
                "",
                "{label}: a 2-cell grapheme must not be halved to fit 1 cell"
            );
            assert_eq!(prefix_to_width(s, 2), s, "{label}: fits whole at 2 cells");
            // 'a' and 'b' take 2 of the 3 cells, leaving 1 — not enough for the
            // 2-cell cluster, which must therefore be dropped whole rather than
            // contributing a leading component.
            assert_eq!(
                prefix_to_width(&format!("ab{s}"), 3),
                "ab",
                "{label}: a cluster that does not fit is dropped, not split"
            );
        }
    }

    #[test]
    fn display_width_counts_cells_not_code_points() {
        assert_eq!(display_width("OpenCode"), 8);
        assert_eq!(display_width("🦞 OpenClaw"), 11);
        assert_eq!("🦞 OpenClaw".chars().count(), 10, "the disagreement");
    }

    #[test]
    fn scrollbar_position_maps_bottom_offset_to_last_position() {
        assert_eq!(scrollbar_position(15, 20, 5), 19);
    }

    #[test]
    fn scrollbar_position_keeps_top_at_zero() {
        assert_eq!(scrollbar_position(0, 20, 5), 0);
    }

    #[test]
    fn scrollbar_position_clamps_overscroll_to_bottom() {
        assert_eq!(scrollbar_position(999, 20, 5), 19);
    }

    #[test]
    fn scrollbar_position_single_page_stays_at_zero() {
        assert_eq!(scrollbar_position(0, 5, 10), 0);
    }

    #[test]
    fn scrollbar_position_uses_wide_math_for_large_lengths() {
        // With usize math, max_scroll * (content_len - 1) would overflow and
        // panic (debug) or wrap (release). These would fail either way.
        let content_len = usize::MAX;
        let viewport_len = 2;
        let max_scroll = content_len - viewport_len; // usize::MAX - 2

        // Top of the scroll range maps to position 0.
        assert_eq!(scrollbar_position(0, content_len, viewport_len), 0);
        // Bottom of the scroll range maps to the last position:
        // max_scroll * (content_len - 1) / max_scroll == content_len - 1.
        assert_eq!(
            scrollbar_position(max_scroll, content_len, viewport_len),
            usize::MAX - 1
        );
        // Overscroll past max_scroll clamps to the same last position.
        assert_eq!(
            scrollbar_position(usize::MAX, content_len, viewport_len),
            usize::MAX - 1
        );
    }

    #[test]
    fn viewport_scrollbar_state_handles_zero_viewport() {
        // The helper clamps viewport_len to 1, so a zero-height viewport must
        // not panic and must still produce a usable state.
        let state = viewport_scrollbar_state(20, 5, 0);
        assert_eq!(
            state,
            ScrollbarState::new(20)
                .position(5)
                .viewport_content_length(1)
        );
    }

    #[test]
    fn shade_from_base_rank_0_equals_base() {
        let base = Color::Rgb(255, 0, 0);
        assert_eq!(shade_from_base(base, 0), base);
    }

    #[test]
    fn shade_from_base_lightens_monotonically_toward_white() {
        let base = Color::Rgb(0, 0, 0);
        let mut prev_r: u8 = 0;
        for rank in 0..7 {
            let Color::Rgb(r, _, _) = shade_from_base(base, rank) else {
                panic!("expected Rgb")
            };
            assert!(
                r >= prev_r,
                "shade at rank {} should not be darker than rank {}",
                rank,
                rank - 1
            );
            prev_r = r;
        }
    }

    #[test]
    fn shade_from_base_clamps_beyond_palette_length() {
        let base = Color::Rgb(100, 100, 100);
        // Rank beyond FACTORS.len() saturates to the lightest shade.
        assert_eq!(shade_from_base(base, 100), shade_from_base(base, 6));
    }

    #[test]
    fn shade_from_base_passes_through_non_rgb() {
        // Indexed terminal colors can't be lightened channel-wise — return as-is.
        assert_eq!(shade_from_base(Color::Indexed(42), 5), Color::Indexed(42));
    }

    #[test]
    fn unknown_provider_returns_gray_ramp_not_pure_white() {
        // Regression: the old fallback was [(255,255,255)], collapsing all
        // unknown-provider models to pure white. Now each rank is a distinct
        // gray so models stay distinguishable.
        let rank_0 = get_provider_shade("some-new-provider", 0);
        let rank_3 = get_provider_shade("some-new-provider", 3);
        assert_ne!(rank_0, rank_3);
        assert_ne!(rank_0, Color::Rgb(255, 255, 255));
    }

    #[test]
    fn cursor_provider_has_distinct_shades_per_rank() {
        // Regression: CURSOR_SHADES used to be a single-entry palette so all
        // Cursor models collapsed to one color.
        let rank_0 = get_provider_shade("cursor", 0);
        let rank_6 = get_provider_shade("cursor", 6);
        assert_ne!(rank_0, rank_6);
    }

    #[test]
    fn get_provider_shade_saturates_at_palette_end() {
        let last = get_provider_shade("anthropic", 6);
        let past_end = get_provider_shade("anthropic", 99);
        assert_eq!(last, past_end);
    }

    #[test]
    fn fable_is_recognized_as_anthropic() {
        assert_eq!(get_provider_from_model("fable-5"), "anthropic");
        assert_eq!(get_provider_from_model("claude-fable-5"), "anthropic");
        assert_eq!(get_provider_from_model("claude-fable-5[1m]"), "anthropic");
    }

    #[test]
    fn glm_and_kimi_use_their_vendor_color_ramps() {
        assert_eq!(get_provider_from_model("glm-5.2"), "zai");
        assert_eq!(get_provider_from_model("kimi-k2.7-code"), "moonshotai");
        assert!(provider_has_palette("zai"));
        assert!(provider_has_palette("moonshotai"));
        assert_eq!(get_provider_from_model("glmish-1"), "unknown");
        assert_eq!(get_provider_from_model("kimiko-1"), "unknown");
    }

    #[test]
    fn fable_gets_same_base_color_as_opus() {
        // Fable is a flagship Claude model and must render in the Anthropic
        // palette at the same base level as Opus — not the gray UNKNOWN shade.
        let fable = get_model_color("fable-5");
        let opus = get_model_color("claude-opus-4-1");
        assert_eq!(fable, opus);
        assert_eq!(fable, get_model_color("claude-fable-5"));
        // Don't assert default-palette inequality against an unknown model:
        // user color overrides from the Tokmesh config could make the two colors
        // equal at runtime, which would flake this test. Assert provider
        // classification instead, which is independent of config palettes.
        assert_eq!(get_provider_from_model("some-unknown-model"), "unknown");
    }

    #[test]
    fn fable_substring_does_not_misattribute_to_anthropic() {
        // Regression: raw substring matching for "fable" would misclassify
        // unrelated model names. Matching must be on delimited tokens only,
        // consistent with core provider_identity inference.
        assert_eq!(get_provider_from_model("unfabled-model"), "unknown");
        assert_eq!(get_provider_from_model("fableton-1"), "unknown");
        // But genuine fable tokens still resolve to Anthropic.
        assert_eq!(get_provider_from_model("fable-5"), "anthropic");
        assert_eq!(get_provider_from_model("claude-fable-5[1m]"), "anthropic");
    }

    #[test]
    fn provider_display_name_target_cases() {
        // The four cases the user reported as rendering wrong.
        assert_eq!(get_provider_display_name("openai"), "OpenAI");
        assert_eq!(get_provider_display_name("kimi-for-coding"), "Kimi");
        assert_eq!(get_provider_display_name("google-vertex"), "Google Vertex");
        assert_eq!(get_provider_display_name("opencode"), "OpenCode");
    }

    #[test]
    fn provider_display_name_openai_family() {
        // Any openai* id collapses to the brand name.
        assert_eq!(get_provider_display_name("openai"), "OpenAI");
        assert_eq!(get_provider_display_name("openai-codex"), "OpenAI");
        assert_eq!(get_provider_display_name("OpenAI"), "OpenAI");
    }

    #[test]
    fn provider_display_name_kimi_family() {
        assert_eq!(get_provider_display_name("kimi"), "Kimi");
        assert_eq!(get_provider_display_name("kimi-code"), "Kimi");
        assert_eq!(get_provider_display_name("kimi-for-coding"), "Kimi");
    }

    #[test]
    fn provider_display_name_google_vertex_both_spellings() {
        // `canonical_provider` rewrites the hyphen to an underscore, so both
        // spellings must map to the same clean label.
        assert_eq!(get_provider_display_name("google-vertex"), "Google Vertex");
        assert_eq!(get_provider_display_name("google_vertex"), "Google Vertex");
    }

    #[test]
    fn provider_display_name_smart_fallback_multiword() {
        // Unknown multi-word providers get split + title-cased instead of the
        // old naive capitalize-first ("Some-new-provider").
        assert_eq!(
            get_provider_display_name("some-new-provider"),
            "Some New Provider"
        );
        assert_eq!(
            get_provider_display_name("some_new_provider"),
            "Some New Provider"
        );
    }

    #[test]
    fn provider_display_name_known_regressions() {
        assert_eq!(get_provider_display_name("anthropic"), "Anthropic");
        assert_eq!(get_provider_display_name("google"), "Google");
        assert_eq!(get_provider_display_name("xai"), "xAI");
        assert_eq!(get_provider_display_name("deepseek"), "DeepSeek");
        assert_eq!(get_provider_display_name("meta"), "Meta");
        assert_eq!(get_provider_display_name("mistral"), "Mistral");
        assert_eq!(get_provider_display_name("cohere"), "Cohere");
        assert_eq!(get_provider_display_name("cursor"), "Cursor");
        assert_eq!(
            get_provider_display_name("github-copilot"),
            "GitHub Copilot"
        );
        assert_eq!(get_provider_display_name("copilot"), "GitHub Copilot");
    }

    #[test]
    fn provider_display_name_acronym_words_in_fallback() {
        // Per-word acronym map applies inside the smart fallback.
        assert_eq!(get_provider_display_name("acme-ai"), "Acme AI");
        assert_eq!(get_provider_display_name("foo-api"), "Foo API");
    }

    #[test]
    fn provider_display_name_merged_list_maps_each_segment() {
        // Merged Models rows store providers as a ", "-joined string
        // (aggregate_model_usage_entries). Each segment must be mapped
        // independently — a prefix/contains-family branch on the whole string
        // would otherwise silently drop the rest. Regression guard for that.
        assert_eq!(
            get_provider_display_name("openai, openrouter"),
            "OpenAI, OpenRouter"
        );
        assert_eq!(
            get_provider_display_name("kimi, anthropic"),
            "Kimi, Anthropic"
        );
        // A `copilot` segment must not swallow its siblings via the contains()
        // branch.
        assert_eq!(
            get_provider_display_name("anthropic, copilot"),
            "Anthropic, GitHub Copilot"
        );
        assert_eq!(
            get_provider_display_name("anthropic, openai"),
            "Anthropic, OpenAI"
        );
    }

    #[test]
    fn provider_display_name_empty_is_empty() {
        assert_eq!(get_provider_display_name(""), "");
        // A string of only separators collapses to empty rather than panicking.
        assert_eq!(get_provider_display_name("--_-"), "");
    }

    #[test]
    fn get_provider_shade_fuzzy_matching() {
        assert_eq!(
            get_provider_shade("test-anthropic", 0),
            get_provider_shade("anthropic", 0)
        );
        assert_eq!(
            get_provider_shade("company-google", 0),
            get_provider_shade("google", 0)
        );
        assert_eq!(
            get_provider_shade("openrouter-gemini-prod", 0),
            get_provider_shade("google", 0)
        );
        assert_eq!(
            get_provider_shade("deepseek-api", 0),
            get_provider_shade("deepseek", 0)
        );
        assert_eq!(
            get_provider_shade("meta-llama-endpoint", 0),
            get_provider_shade("meta", 0)
        );
    }

    #[test]
    fn provider_has_palette_flags_only_known_vendors() {
        // Known vendors (and fuzzy substrings of them) have their own ramp.
        assert!(provider_has_palette("anthropic"));
        assert!(provider_has_palette("openai"));
        assert!(provider_has_palette("openrouter-gemini-prod"));
        assert!(provider_has_palette("zai"));
        assert!(provider_has_palette("moonshotai"));
        // Gateway/reseller providers do not — callers must color by the
        // model's own vendor instead of the neutral "unknown" gray.
        assert!(!provider_has_palette("github-copilot"));
        assert!(!provider_has_palette("openrouter"));
        assert!(!provider_has_palette("unknown"));
        assert!(!provider_has_palette(""));
    }
}