claude-code-status-line 1.0.0

A configurable status line for Claude Code with powerline arrows, context tracking, and quota monitoring
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
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
use unicode_width::UnicodeWidthChar;

/// Which sections to display (set to false to hide)
const SHOW_CWD: bool = true;
const CWD_FULL_PATH: bool = true; // Set to false to show only directory name (basename)
const SHOW_GIT: bool = true;
const SHOW_MODEL: bool = true;
const SHOW_CONTEXT: bool = true;
const CONTEXT_USE_FLOAT: bool = false; // Set to true to show floating point (e.g., 99.9%), false for integer floored (e.g., 99.9% → 99%)
const SHOW_QUOTA: bool = true; // 5h/7d quota (Pro/Max subscription users)
const SHOW_COST: bool = false; // API cost (API key users, auto-hidden if $0)
const QUOTA_CACHE_TTL: u64 = 0; // Cache TTL in seconds (0 = disabled, otherwise only queries fresh quota data if cached data is older than TTL)
const DEFAULT_TERMINAL_WIDTH: usize = 120; // Fallback width if detection fails
const RIGHT_MARGIN: usize = 20; // Reserve space for Claude Code's right-side messages (0 = no margin)

/// Theme selection - set to false for light terminal backgrounds
/// By default the only difference is the font color (light or dark), but you can customize it down below
const USE_DARK_THEME: bool = true;

// You can set your own colors if you want in (R, G, B) format:
// BG = background color (if enabled, you can simply ignore it otherwise)
// FG = foreground color (text)
// SEP = separator color
// MUTED = reset timer color, if you want slightly different color for better visibility

// Official Claude color codes for reference:
// (217, 119, 87); // Claude Burnt Orange #D97757
// (250, 249, 245); // Claude Cream #FAF9F5
// (38, 38, 36); // Claude Dark Gray #262624
// (65, 65, 62); // Claude Medium Gray #41413E
// (130, 129, 122); // Claude Light Gray #82817A

/// Powerline arrow configuration
const USE_POWERLINE: bool = true; // Set to false for classic pipe separator style
const POWERLINE_ARROW: char = '\u{E0B0}'; //  - requires powerline-compatible font (Nerd Fonts, etc.)

// Classic separator mode colors (used ONLY when USE_POWERLINE = false)
const DARK_BG: (u8, u8, u8) = (217, 119, 87); // Claude Burnt Orange #D97757
const DARK_FG: (u8, u8, u8) = (38, 38, 36); // Claude Dark Gray #262624
const DARK_SEP: (u8, u8, u8) = (38, 38, 36); // Claude Dark Gray #262624
const DARK_MUTED: (u8, u8, u8) = (65, 65, 62); // Claude Medium Gray #41413E

const LIGHT_BG: (u8, u8, u8) = (217, 119, 87); // Claude Burnt Orange #D97757
const LIGHT_FG: (u8, u8, u8) = (250, 249, 245); // Claude Cream #FAF9F5
const LIGHT_SEP: (u8, u8, u8) = (250, 249, 245); // Claude Cream #FAF9F5
const LIGHT_MUTED: (u8, u8, u8) = (130, 129, 122); // Claude Light Gray #82817A

const SHOW_BACKGROUND: bool = true; // Show background in classic mode (USE_POWERLINE = false)

/// Separator character between sections
const SEPARATOR_CHAR: char = '|';

// ============================================================================
// PER-SECTION COLORS
// ============================================================================

/// Color scheme for a single section (background, foreground, muted)
#[derive(Clone, Copy)]
struct SectionColors {
    bg: (u8, u8, u8),
    fg: (u8, u8, u8),
    muted: (u8, u8, u8),
}

// Per-section color constants (customize these for different section colors)
const CWD_COLORS: SectionColors = SectionColors {
    bg: (217, 119, 87),  // Claude Burnt Orange #D97757
    fg: (38, 38, 36),    // Claude Dark Gray #262624
    muted: (65, 65, 62), // Claude Medium Gray #41413E
};

const GIT_COLORS: SectionColors = SectionColors {
    bg: (38, 38, 36),       // Claude Dark Gray #262624
    fg: (217, 119, 87),     // Claude Burnt Orange #D97757
    muted: (130, 129, 122), // Claude Light Gray #82817A
};

const MODEL_COLORS: SectionColors = SectionColors {
    bg: (217, 119, 87),  // Claude Burnt Orange #D97757
    fg: (38, 38, 36),    // Claude Dark Gray #262624
    muted: (65, 65, 62), // Claude Medium Gray #41413E
};

const CONTEXT_COLORS: SectionColors = SectionColors {
    bg: (38, 38, 36),       // Claude Dark Gray #262624
    fg: (217, 119, 87),     // Claude Burnt Orange #D97757
    muted: (130, 129, 122), // Claude Light Gray #82817A
};

const QUOTA_5H_COLORS: SectionColors = SectionColors {
    bg: (217, 119, 87),  // Claude Burnt Orange #D97757
    fg: (38, 38, 36),    // Claude Dark Gray #262624
    muted: (65, 65, 62), // Claude Medium Gray #41413E
};

const QUOTA_7D_COLORS: SectionColors = SectionColors {
    bg: (38, 38, 36),       // Claude Dark Gray #262624
    fg: (217, 119, 87),     // Claude Burnt Orange #D97757
    muted: (130, 129, 122), // Claude Light Gray #82817A
};

const COST_COLORS: SectionColors = SectionColors {
    bg: (217, 119, 87),  // Claude Burnt Orange #D97757
    fg: (38, 38, 36),    // Claude Dark Gray #262624
    muted: (65, 65, 62), // Claude Medium Gray #41413E
};

/// Fallback context window size (only used if Claude Code doesn't provide it)
/// NOTE: Claude Code normally provides this dynamically via context_window.context_window_size
/// This fallback is for edge cases only (e.g., older Claude Code versions)
const DEFAULT_CONTEXT_WINDOW: u64 = 200_000;

// ============================================================================
// Constants (Internal - Don't edit these)
// ============================================================================

/// ANSI reset code
const RESET: &str = "\x1b[0m";

#[derive(Clone, PartialEq, Debug)]
enum SectionKind {
    Generic,
    QuotaShort(String),
    QuotaFull(String),
}

#[derive(Clone)]
struct Section {
    kind: SectionKind,
    content: String,
    priority: u16,         // Lower number = higher priority (0 = always show)
    colors: SectionColors, // Per-section colors
    width: usize,          // Cached visible width (excluding padding/separators)
}

impl Section {
    fn new(content: String, priority: u16, colors: SectionColors) -> Self {
        let width = visible_len(&content);
        Section {
            kind: SectionKind::Generic,
            content,
            priority,
            colors,
            width,
        }
    }

    fn new_quota_short(label: &str, content: String, priority: u16, colors: SectionColors) -> Self {
        let width = visible_len(&content);
        Section {
            kind: SectionKind::QuotaShort(label.to_string()),
            content,
            priority,
            colors,
            width,
        }
    }

    fn new_quota_full(label: &str, content: String, priority: u16, colors: SectionColors) -> Self {
        let width = visible_len(&content);
        Section {
            kind: SectionKind::QuotaFull(label.to_string()),
            content,
            priority,
            colors,
            width,
        }
    }
}

/// Calculate visible width in terminal columns (strips ANSI and measures Unicode width)
fn visible_len(s: &str) -> usize {
    let mut width = 0;
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\x1b' {
            if let Some(&next) = chars.peek() {
                if next == '[' {
                    // CSI: ESC [ ... <final byte 0x40–0x7E>
                    chars.next(); // consume '['
                    for c2 in chars.by_ref() {
                        if ('@'..='~').contains(&c2) {
                            break;
                        }
                    }
                }
            }
        } else {
            width += c.width().unwrap_or(0);
        }
    }
    width
}

/// Truncate string with ellipsis while preserving ANSI codes
fn truncate_with_ellipsis(s: &str, max_width: usize) -> String {
    if visible_len(s) <= max_width {
        return s.to_string();
    }

    // Use "…" (UTF-8) as ellipsis. Visual width is typically 1
    const ELLIPSIS: &str = "";
    const ELLIPSIS_WIDTH: usize = 1;

    if max_width <= ELLIPSIS_WIDTH {
        return ELLIPSIS.chars().take(max_width).collect();
    }

    let target_width = max_width - ELLIPSIS_WIDTH;
    let mut current_width = 0;
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '\x1b' {
            result.push(c);
            if let Some(&next) = chars.peek() {
                if next == '[' {
                    // CSI sequence - preserve but don't count toward width
                    result.push(chars.next().unwrap()); // '['
                    for c2 in chars.by_ref() {
                        result.push(c2);
                        if ('@'..='~').contains(&c2) {
                            break;
                        }
                    }
                }
            }
        } else {
            // Calculate width of this character (no allocation)
            let char_width = c.width().unwrap_or(0);
            if current_width + char_width > target_width {
                break;
            }
            result.push(c);
            current_width += char_width;
        }
    }

    result.push_str(ELLIPSIS);
    result.push_str(RESET);
    result
}

/// Get terminal width, returns DEFAULT_TERMINAL_WIDTH as fallback
fn get_terminal_width() -> usize {
    if let Ok(w_str) = std::env::var("CLAUDE_TERMINAL_WIDTH") {
        if let Ok(w) = w_str.parse::<usize>() {
            return w;
        }
    }

    if let Ok(w_str) = std::env::var("COLUMNS") {
        if let Ok(w) = w_str.parse::<usize>() {
            return w;
        }
    }

    if let Some((w, _)) = terminal_size::terminal_size() {
        return w.0 as usize;
    }

    #[cfg(unix)]
    {
        if let Ok(f) = std::fs::File::open("/dev/tty") {
            if let Some((w, _)) = terminal_size::terminal_size_of(&f) {
                return w.0 as usize;
            }
        }
    }

    DEFAULT_TERMINAL_WIDTH
}

/// Build statusline content, removing low-priority sections if too wide for terminal
fn build_statusline(sections: Vec<Section>, max_width: usize) -> String {
    if sections.is_empty() {
        return String::new();
    }

    let mut sorted = sections;
    sorted.sort_by_key(|s| s.priority);

    while sorted.len() > 1 {
        let mut deduped: Vec<&Section> = Vec::with_capacity(sorted.len());

        let full_labels: std::collections::HashSet<&str> = sorted
            .iter()
            .filter_map(|s| match &s.kind {
                SectionKind::QuotaFull(l) => Some(l.as_str()),
                _ => None,
            })
            .collect();

        for section in sorted.iter() {
            match &section.kind {
                SectionKind::QuotaShort(label) => {
                    if !full_labels.contains(label.as_str()) {
                        deduped.push(section);
                    }
                }
                _ => deduped.push(section),
            }
        }

        // Calculate total width without rendering
        let content_width: usize = deduped.iter().map(|s| s.width).sum();
        let count = deduped.len();

        let total_width = if USE_POWERLINE {
            // Powerline: (Content + 2 padding) + 1 arrow per section
            // Sum(width) + 2*N + N = Sum(width) + 3*N
            content_width + count * 3
        } else {
            // Classic: Start(1) + End(1) + (N-1) * Separator(3)
            // 2 + (N-1)*3 + Sum(width)
            // If N=0 (handled above), 0.
            if count > 0 {
                content_width + 2 + (count - 1) * 3
            } else {
                0
            }
        };

        if total_width <= max_width {
            return if USE_POWERLINE {
                render_with_powerline(&deduped)
            } else {
                render_with_separator(&deduped)
            };
        }

        sorted.pop();
    }

    if let Some(last) = sorted.first() {
        let rendered = if USE_POWERLINE {
            render_with_powerline(&[last])
        } else {
            render_with_separator(&[last])
        };

        let visible = visible_len(&rendered);
        if visible > max_width {
            return truncate_with_ellipsis(&rendered, max_width);
        }
        return rendered;
    }

    String::new()
}

/// Render sections with powerline arrows
fn render_with_powerline(sections: &[&Section]) -> String {
    let mut result = String::new();

    for (i, section) in sections.iter().enumerate() {
        result.push_str(&render_section(&section.content, &section.colors));

        if i < sections.len() - 1 {
            let next = sections[i + 1];
            result.push_str(&make_powerline_arrow(section.colors.bg, next.colors.bg));
        }
    }

    if let Some(last) = sections.last() {
        result.push_str(&format!(
            "\x1b[38;2;{};{};{}m\x1b[49m{}",
            last.colors.bg.0, last.colors.bg.1, last.colors.bg.2, POWERLINE_ARROW
        ));
    }

    result.push_str(RESET);
    result
}

/// Render sections with old pipe separator style (fallback)
fn render_with_separator(sections: &[&Section]) -> String {
    let separator = make_separator();
    let color_start = make_color_start();

    let mut result = String::with_capacity(sections.len() * 30 + 50);

    result.push_str(&color_start);
    result.push(' ');

    for (i, section) in sections.iter().enumerate() {
        if i > 0 {
            result.push_str(&separator);
        }
        result.push_str(&section.content);
    }

    result.push(' ');
    result.push_str(RESET);
    result
}

/// Render a single section with its colors
fn render_section(content: &str, colors: &SectionColors) -> String {
    format!(
        "\x1b[48;2;{};{};{}m\x1b[38;2;{};{};{}m {} ",
        colors.bg.0, colors.bg.1, colors.bg.2, colors.fg.0, colors.fg.1, colors.fg.2, content
    )
}

/// Generate powerline arrow between two sections
fn make_powerline_arrow(left_bg: (u8, u8, u8), right_bg: (u8, u8, u8)) -> String {
    // Arrow foreground = left section's bg
    // Arrow background = right section's bg
    format!(
        "\x1b[38;2;{};{};{}m\x1b[48;2;{};{};{}m{}",
        left_bg.0, left_bg.1, left_bg.2, right_bg.0, right_bg.1, right_bg.2, POWERLINE_ARROW
    )
}

/// Generate ANSI escape code for muted text using section's muted color
fn make_muted_from_section(colors: &SectionColors) -> String {
    format!(
        "\x1b[38;2;{};{};{}m",
        colors.muted.0, colors.muted.1, colors.muted.2
    )
}

/// Generate ANSI escape code to restore foreground color for a section
fn restore_fg_from_section(colors: &SectionColors) -> String {
    format!("\x1b[38;2;{};{};{}m", colors.fg.0, colors.fg.1, colors.fg.2)
}

/// Helper to get muted and foreground color codes (unified logic)
fn get_muted_and_fg_codes(colors: &SectionColors) -> (String, String) {
    if USE_POWERLINE {
        (
            make_muted_from_section(colors),
            restore_fg_from_section(colors),
        )
    } else {
        let muted = make_muted_color();
        let fg_color = if USE_DARK_THEME { DARK_FG } else { LIGHT_FG };
        let fg = format!("\x1b[38;2;{};{};{}m", fg_color.0, fg_color.1, fg_color.2);
        (muted, fg)
    }
}

/// Generate ANSI escape code for setting background and foreground colors
fn make_color_start() -> String {
    let (fg_r, fg_g, fg_b) = if USE_DARK_THEME { DARK_FG } else { LIGHT_FG };
    if SHOW_BACKGROUND {
        let (bg_r, bg_g, bg_b) = if USE_DARK_THEME { DARK_BG } else { LIGHT_BG };
        format!(
            "\x1b[48;2;{};{};{}m\x1b[38;2;{};{};{}m",
            bg_r, bg_g, bg_b, fg_r, fg_g, fg_b
        )
    } else {
        format!("\x1b[38;2;{};{};{}m", fg_r, fg_g, fg_b)
    }
}

/// Generate ANSI escape code for separator with appropriate colors
fn make_separator() -> String {
    let (sep_r, sep_g, sep_b) = if USE_DARK_THEME { DARK_SEP } else { LIGHT_SEP };
    let (fg_r, fg_g, fg_b) = if USE_DARK_THEME { DARK_FG } else { LIGHT_FG };
    format!(
        " \x1b[38;2;{};{};{}m{}\x1b[38;2;{};{};{}m ",
        sep_r, sep_g, sep_b, SEPARATOR_CHAR, fg_r, fg_g, fg_b
    )
}

/// Generate ANSI escape code for muted text (used for reset times)
fn make_muted_color() -> String {
    let (r, g, b) = if USE_DARK_THEME {
        DARK_MUTED
    } else {
        LIGHT_MUTED
    };
    format!("\x1b[38;2;{};{};{}m", r, g, b)
}

#[derive(Debug, Deserialize)]
struct ClaudeInput {
    workspace: Option<Workspace>,
    model: Option<Model>,
    context_window: Option<ContextWindow>,
    cost: Option<Cost>,
}

#[derive(Debug, Deserialize)]
struct Workspace {
    current_dir: Option<String>,
}

#[derive(Debug, Deserialize)]
struct Model {
    display_name: Option<String>,
}

#[derive(Debug, Deserialize)]
struct ContextWindow {
    context_window_size: Option<u64>,
    current_usage: Option<CurrentUsage>,
}

#[derive(Debug, Deserialize)]
struct CurrentUsage {
    input_tokens: Option<u64>,
    _output_tokens: Option<u64>,
    cache_creation_input_tokens: Option<u64>,
    cache_read_input_tokens: Option<u64>,
}

#[derive(Debug, Deserialize)]
struct Cost {
    total_cost_usd: Option<f64>,
}

#[derive(Debug, Deserialize)]
struct QuotaResponse {
    five_hour: Option<QuotaLimit>,
    seven_day: Option<QuotaLimit>,
}

#[derive(Debug, Deserialize)]
struct QuotaLimit {
    utilization: f64,
    resets_at: Option<String>,
}

#[derive(Debug, Deserialize)]
struct KeychainCredentials {
    #[serde(rename = "claudeAiOauth")]
    claude_ai_oauth: Option<OAuthCredentials>,
}

#[derive(Debug, Deserialize)]
struct OAuthCredentials {
    #[serde(rename = "accessToken")]
    access_token: String,
}

// Quota data structure
#[derive(Debug, Deserialize, Serialize, Clone)]
struct QuotaData {
    five_hour_pct: Option<f64>,
    five_hour_resets_at: Option<String>,
    seven_day_pct: Option<f64>,
    seven_day_resets_at: Option<String>,
}

#[derive(Serialize, Deserialize)]
struct CachedQuota {
    timestamp: u64,
    data: QuotaData,
}

fn get_oauth_token() -> Option<String> {
    if let Ok(token) = std::env::var("CLAUDE_OAUTH_TOKEN") {
        if token.contains('\r') || token.contains('\n') {
            return None;
        }
        return Some(token);
    }

    #[cfg(target_os = "macos")]
    {
        let output = Command::new("security")
            .args([
                "find-generic-password",
                "-s",
                "Claude Code-credentials",
                "-w",
            ])
            .output()
            .ok()?;

        if !output.status.success() {
            return None;
        }

        let creds_json = String::from_utf8(output.stdout).ok()?;
        let creds: KeychainCredentials = serde_json::from_str(&creds_json).ok()?;
        let token = creds.claude_ai_oauth?.access_token;
        if token.contains('\r') || token.contains('\n') {
            return None;
        }
        Some(token)
    }

    #[cfg(not(target_os = "macos"))]
    {
        // On Linux/Windows, try ~/.claude/.credentials.json first
        #[cfg(unix)]
        let home_dir = std::env::var_os("HOME");

        #[cfg(windows)]
        let home_dir = std::env::var_os("USERPROFILE");

        if let Some(home) = home_dir {
            let creds_path = std::path::Path::new(&home)
                .join(".claude")
                .join(".credentials.json");
            if let Ok(creds_json) = fs::read_to_string(creds_path) {
                if let Ok(creds) = serde_json::from_str::<KeychainCredentials>(&creds_json) {
                    if let Some(oauth) = creds.claude_ai_oauth {
                        let token = oauth.access_token;
                        if !token.contains('\r') && !token.contains('\n') {
                            return Some(token);
                        }
                    }
                }
            }
        }

        // Fallback: try keyring crate (for other credential storage systems)
        let username = std::env::var("USER")
            .or_else(|_| std::env::var("USERNAME"))
            .unwrap_or_else(|_| "default".to_string());

        let entry = keyring::Entry::new("Claude Code-credentials", &username).ok()?;
        let creds_json = entry.get_password().ok()?;
        let creds: KeychainCredentials = serde_json::from_str(&creds_json).ok()?;
        let token = creds.claude_ai_oauth?.access_token;
        if token.contains('\r') || token.contains('\n') {
            return None;
        }
        Some(token)
    }
}

fn fetch_quota_from_api_safe(token: &str) -> Option<QuotaData> {
    let output = match Command::new("curl")
        .args([
            "-s", // Silent
            "-f", // Fail on HTTP error (4xx/5xx) - CRITICAL for cache safety
            "-m",
            "1", // 1 second timeout
            "-H",
            "Accept: application/json",
            "-H",
            "Content-Type: application/json",
            "-H",
            "User-Agent: claude-code/2.0.31",
            "-H",
            &format!("Authorization: Bearer {}", token),
            "-H",
            "anthropic-beta: oauth-2025-04-20",
            "https://api.anthropic.com/api/oauth/usage",
        ])
        .output()
    {
        Ok(o) => o,
        Err(e) => {
            if std::env::var("STATUSLINE_DEBUG").is_ok() {
                eprintln!("statusline warning: curl not available: {}", e);
            }
            return None;
        }
    };

    if !output.status.success() {
        if std::env::var("STATUSLINE_DEBUG").is_ok() {
            eprintln!(
                "statusline warning: quota fetch failed with status {:?}",
                output.status
            );
        }
        return None;
    }

    let q: QuotaResponse = serde_json::from_slice(&output.stdout).ok()?;

    Some(QuotaData {
        five_hour_pct: q.five_hour.as_ref().map(|x| x.utilization),
        five_hour_resets_at: q.five_hour.and_then(|x| x.resets_at),
        seven_day_pct: q.seven_day.as_ref().map(|x| x.utilization),
        seven_day_resets_at: q.seven_day.and_then(|x| x.resets_at),
    })
}

fn get_cache_path() -> PathBuf {
    let temp_dir = std::env::temp_dir();
    #[cfg(unix)]
    let uid = unsafe { libc::getuid() };
    #[cfg(not(unix))]
    let uid = std::env::var("USERNAME")
        .or_else(|_| std::env::var("USER"))
        .unwrap_or_else(|_| "default".to_string());
    temp_dir.join(format!("claude-statusline-quota-{}.json", uid))
}

fn write_cache_atomic(path: &Path, content: &str) -> std::io::Result<()> {
    use std::io::Write;
    let dir = path.parent().unwrap_or_else(|| Path::new("."));
    let mut f = tempfile::NamedTempFile::new_in(dir)?;
    f.write_all(content.as_bytes())?;
    f.persist(path).map(|_| ()).map_err(|e| e.error)
}

#[allow(clippy::absurd_extreme_comparisons)]
fn get_quota() -> Option<QuotaData> {
    let cache_path = get_cache_path();
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    let cached_full = fs::read_to_string(&cache_path)
        .ok()
        .and_then(|content| serde_json::from_str::<CachedQuota>(&content).ok());

    let is_fresh = if QUOTA_CACHE_TTL == 0 {
        false
    } else if let Some(cache) = &cached_full {
        now.saturating_sub(cache.timestamp) < QUOTA_CACHE_TTL
    } else {
        false
    };

    if is_fresh {
        return cached_full.map(|c| c.data);
    }

    if let Some(token) = get_oauth_token() {
        if let Some(fresh_data) = fetch_quota_from_api_safe(&token) {
            let cache = CachedQuota {
                timestamp: now,
                data: fresh_data.clone(),
            };
            if let Ok(json) = serde_json::to_string(&cache) {
                let _ = write_cache_atomic(&cache_path, &json);
            }
            return Some(fresh_data);
        }
    }

    cached_full.map(|c| c.data)
}

struct GitInfo {
    branch: String,
    is_dirty: bool,
}

fn get_git_info(dir: &str) -> Option<GitInfo> {
    let output = match Command::new("git")
        .args(["-C", dir, "status", "--porcelain", "-b"])
        .output()
    {
        Ok(o) => o,
        Err(e) => {
            if std::env::var("STATUSLINE_DEBUG").is_ok() {
                eprintln!("statusline warning: git not available: {}", e);
            }
            return None;
        }
    };

    if !output.status.success() {
        return None;
    }

    let stdout = String::from_utf8(output.stdout).ok()?;
    let lines: Vec<&str> = stdout.lines().collect();

    if lines.is_empty() {
        return None;
    }

    let branch_line = lines[0];
    let branch = if let Some(raw) = branch_line.strip_prefix("## ") {
        if let Some(idx) = raw.find("...") {
            raw[..idx].to_string()
        } else if let Some(stripped) = raw.strip_prefix("No commits yet on ") {
            stripped.to_string()
        } else if raw.starts_with("HEAD (no branch)") {
            "HEAD".to_string()
        } else {
            raw.to_string()
        }
    } else {
        return None;
    };

    let is_dirty = lines.len() > 1;

    Some(GitInfo { branch, is_dirty })
}

fn format_quota_display(
    label: &str,
    pct: Option<f64>,
    reset: &str,
    colors: &SectionColors,
) -> String {
    let pct_str = match pct {
        Some(p) => format!("{}%", p),
        None => "-".to_string(),
    };

    if reset.is_empty() {
        format!("{}: {}", label, pct_str)
    } else {
        let (muted, fg) = get_muted_and_fg_codes(colors);
        format!("{}: {} {}({}){}", label, pct_str, muted, reset, fg)
    }
}

fn format_quota_short(label: &str, pct: Option<f64>) -> String {
    match pct {
        Some(p) => format!("{}: {}%", label, p),
        None => format!("{}: -", label),
    }
}

fn calculate_context(context_window: Option<&ContextWindow>) -> Option<f64> {
    let cw = context_window?;
    let total = cw.context_window_size.unwrap_or(DEFAULT_CONTEXT_WINDOW);
    if total == 0 {
        return None;
    }
    let usage = cw.current_usage.as_ref()?;

    // Get tokens from JSON (doesn't include buffer)
    let content_tokens = usage.input_tokens.unwrap_or(0)
        + usage.cache_creation_input_tokens.unwrap_or(0)
        + usage.cache_read_input_tokens.unwrap_or(0);

    // Add the autocompact buffer (22.5% of total, shown in /context)
    let buffer = (total as f64 * 0.225) as u64;
    let total_used = content_tokens + buffer;

    // Simple percentage of total (matches /context display)
    let percentage = (total_used as f64 / total as f64 * 100.0).min(100.0);
    Some(percentage)
}

fn format_ctx(pct: f64) -> String {
    if CONTEXT_USE_FLOAT {
        format!("ctx: {:.1}%", pct)
    } else {
        format!("ctx: {}%", pct as u64)
    }
}

fn format_cost_display(cost: Option<&Cost>) -> Option<String> {
    cost.and_then(|c| c.total_cost_usd)
        .filter(|&usd| usd > 0.0)
        .map(|usd| format!("${:.2}", usd))
}

fn format_time_remaining(resets_at: &str, now: DateTime<Utc>) -> String {
    let reset_time = match DateTime::parse_from_rfc3339(resets_at) {
        Ok(t) => t.with_timezone(&Utc),
        Err(_) => return String::new(),
    };

    let duration = reset_time.signed_duration_since(now);

    if duration <= Duration::zero() {
        return "now".to_string();
    }

    let total_minutes = duration.num_minutes();
    let total_hours = duration.num_hours();
    let total_days = duration.num_days();

    if total_days >= 1 {
        let hours = total_hours % 24;
        format!("{}d {}h", total_days, hours)
    } else if total_hours >= 1 {
        let minutes = total_minutes % 60;
        format!("{}h {}m", total_hours, minutes)
    } else {
        format!("{}m", total_minutes.max(1))
    }
}

fn main() {
    const VERSION: &str = env!("CARGO_PKG_VERSION");

    if std::env::args().any(|a| a == "--version" || a == "-V") {
        println!("claude-statusline {}", VERSION);
        return;
    }

    if std::env::args().any(|a| a == "--help" || a == "-h") {
        println!("Claude Code statusline binary");
        println!("Usage: Receives JSON on stdin, outputs statusline to stdout");
        println!("\nOptions:");
        println!("  --version, -V    Show version");
        println!("  --help, -h       Show this help");
        return;
    }

    let mut input_str = String::new();
    if io::stdin().read_to_string(&mut input_str).is_err() {
        eprintln!("statusline error: failed to read stdin");
        println!(); // Empty line to stdout to avoid corrupting prompt
        return;
    }

    let input: ClaudeInput = match serde_json::from_str(&input_str) {
        Ok(i) => i,
        Err(_) => {
            eprintln!("statusline error: invalid JSON");
            println!();
            return;
        }
    };

    let cwd = input
        .workspace
        .and_then(|w| w.current_dir)
        .unwrap_or_else(|| ".".to_string());

    let cwd_display = if CWD_FULL_PATH {
        if let Some(home) = std::env::var_os("HOME") {
            let home_str = home.to_string_lossy();
            if cwd.starts_with(home_str.as_ref()) {
                cwd.replacen(home_str.as_ref(), "~", 1)
            } else {
                cwd.clone()
            }
        } else {
            cwd.clone()
        }
    } else {
        std::path::Path::new(&cwd)
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| cwd.clone())
    };

    let git_info = get_git_info(&cwd);

    let make_git_display = |git_info: &Option<GitInfo>, colors: &SectionColors| -> String {
        match git_info {
            Some(info) => {
                if info.is_dirty {
                    let (muted, fg) = get_muted_and_fg_codes(colors);
                    format!("({}) {}*{}", info.branch, muted, fg)
                } else {
                    format!("({})", info.branch)
                }
            }
            None => "-".to_string(),
        }
    };

    let git_display = make_git_display(&git_info, &GIT_COLORS);

    let model = input
        .model
        .and_then(|m| m.display_name)
        .unwrap_or_else(|| "Sonnet".to_string());

    let ctx = calculate_context(input.context_window.as_ref());

    let quota = if SHOW_QUOTA { get_quota() } else { None };

    let cost_display = if SHOW_COST {
        format_cost_display(input.cost.as_ref())
    } else {
        None
    };

    let mut sections = Vec::new();
    let mut priority = 0u16;

    if SHOW_CWD {
        sections.push(Section::new(cwd_display, priority, CWD_COLORS));
        priority += 1;
    }

    if SHOW_GIT {
        sections.push(Section::new(git_display, priority, GIT_COLORS));
        priority += 1;
    }

    if SHOW_MODEL {
        sections.push(Section::new(model, priority, MODEL_COLORS));
        priority += 1;
    }

    if SHOW_CONTEXT {
        match ctx {
            Some(pct) => {
                sections.push(Section::new(format_ctx(pct), priority, CONTEXT_COLORS));
            }
            None => {
                sections.push(Section::new("ctx: -".to_string(), priority, CONTEXT_COLORS));
            }
        }
        priority += 1;
    }

    if SHOW_QUOTA {
        if let Some(q) = quota {
            let now = Utc::now();
            let five_hr_reset = q
                .five_hour_resets_at
                .as_ref()
                .map(|r| format_time_remaining(r, now))
                .unwrap_or_default();

            let seven_day_reset = q
                .seven_day_resets_at
                .as_ref()
                .map(|r| format_time_remaining(r, now))
                .unwrap_or_default();

            sections.push(Section::new_quota_short(
                "5h",
                format_quota_short("5h", q.five_hour_pct),
                priority,
                QUOTA_5H_COLORS,
            ));
            if !five_hr_reset.is_empty() {
                sections.push(Section::new_quota_full(
                    "5h",
                    format_quota_display("5h", q.five_hour_pct, &five_hr_reset, &QUOTA_5H_COLORS),
                    priority + 10,
                    QUOTA_5H_COLORS,
                ));
            }
            priority += 20;

            sections.push(Section::new_quota_short(
                "7d",
                format_quota_short("7d", q.seven_day_pct),
                priority,
                QUOTA_7D_COLORS,
            ));
            if !seven_day_reset.is_empty() {
                sections.push(Section::new_quota_full(
                    "7d",
                    format_quota_display("7d", q.seven_day_pct, &seven_day_reset, &QUOTA_7D_COLORS),
                    priority + 10,
                    QUOTA_7D_COLORS,
                ));
            }
            priority += 20;
        } else {
            sections.push(Section::new("5h: -".to_string(), priority, QUOTA_5H_COLORS));
            priority += 20;
            sections.push(Section::new("7d: -".to_string(), priority, QUOTA_7D_COLORS));
            priority += 20;
        }
    }

    if let Some(cost) = cost_display {
        sections.push(Section::new(cost, priority, COST_COLORS));
    }

    let raw_width = get_terminal_width();
    let term_width = raw_width.saturating_sub(RIGHT_MARGIN);

    if std::env::var("STATUSLINE_DEBUG").is_ok() {
        eprintln!(
            "Debug: term_width detected as {} (raw {} - margin {})",
            term_width, raw_width, RIGHT_MARGIN
        );
    }

    let content = build_statusline(sections, term_width);

    println!("{}", content);
}

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

    #[test]
    fn test_visible_len() {
        assert_eq!(visible_len("Hello"), 5);
        assert_eq!(visible_len("\x1b[31mHello\x1b[0m"), 5);
        assert_eq!(visible_len("\x1b[38;2;10;20;30mHello"), 5);
        assert_eq!(visible_len(""), 0);
        assert_eq!(visible_len("foo bar"), 7);
        assert_eq!(visible_len("\x1b[1;34mBoldBlue"), 8);
    }

    #[test]
    fn test_truncate_with_ellipsis() {
        assert_eq!(truncate_with_ellipsis("Hello World", 11), "Hello World");
        assert_eq!(truncate_with_ellipsis("Hello World", 5), "Hell…\x1b[0m");
        let s = "\x1b[31mHello World\x1b[0m";
        assert_eq!(truncate_with_ellipsis(s, 5), "\x1b[31mHell…\x1b[0m");
    }

    #[test]
    fn test_visible_len_wide_glyphs() {
        assert_eq!(visible_len("🧪"), 2);
        assert_eq!(visible_len("Test🧪"), 6);
        assert_eq!(visible_len("🧪🔬"), 4);
        assert_eq!(visible_len("日本語"), 6);
        assert_eq!(visible_len("Hello日本"), 9);
        assert_eq!(visible_len("Test🧪日本"), 10);
        assert_eq!(visible_len("\x1b[31m日本語\x1b[0m"), 6);
        assert_eq!(visible_len("\x1b[31m🧪Test\x1b[0m"), 6);
    }

    #[test]
    fn test_truncate_wide_glyphs() {
        let emoji_str = "Test🧪Data";
        assert_eq!(truncate_with_ellipsis(emoji_str, 7), "Test🧪…\x1b[0m");
        assert_eq!(truncate_with_ellipsis(emoji_str, 6), "Test…\x1b[0m");

        let cjk_str = "日本語";
        assert_eq!(truncate_with_ellipsis(cjk_str, 6), "日本語");
        assert_eq!(truncate_with_ellipsis(cjk_str, 5), "日本…\x1b[0m");
        assert_eq!(truncate_with_ellipsis(cjk_str, 3), "日…\x1b[0m");

        let mixed = "Test日本語";
        assert_eq!(truncate_with_ellipsis(mixed, 7), "Test日…\x1b[0m");
        assert_eq!(truncate_with_ellipsis(mixed, 10), "Test日本語");

        let path = "/home/user/日本語/test";
        let truncated = truncate_with_ellipsis(path, 15);
        assert!(visible_len(&truncated) <= 15);
    }

    #[test]
    fn test_truncate_wide_boundary() {
        let s = "abc日";
        assert_eq!(truncate_with_ellipsis(s, 5), "abc日");
        assert_eq!(truncate_with_ellipsis(s, 4), "abc…\x1b[0m");
    }

    #[test]
    fn test_format_time_remaining_now() {
        let now = Utc::now();
        let past = now - Duration::minutes(5);
        assert_eq!(format_time_remaining(&past.to_rfc3339(), now), "now");
    }

    #[test]
    fn test_format_time_remaining_minutes() {
        let now = Utc::now();
        let future = now + Duration::minutes(45);
        let result = format_time_remaining(&future.to_rfc3339(), now);
        assert!(result.ends_with('m'));
        assert!(!result.contains('h'));
        assert!(!result.contains('d'));
    }

    #[test]
    fn test_format_time_remaining_hours() {
        let now = Utc::now();
        let future = now + Duration::hours(5) + Duration::minutes(30);
        let result = format_time_remaining(&future.to_rfc3339(), now);
        assert!(result.contains('h'));
        assert!(result.contains('m'));
        assert!(!result.contains('d'));
    }

    #[test]
    fn test_format_time_remaining_days() {
        let now = Utc::now();
        let future = now + Duration::days(3) + Duration::hours(12);
        let result = format_time_remaining(&future.to_rfc3339(), now);
        assert!(result.contains('d'));
        assert!(result.contains('h'));
    }

    #[test]
    fn test_section_dedup() {
        let sections = vec![
            Section::new_quota_short("5h", "5h: 10%".to_string(), 1, QUOTA_5H_COLORS),
            Section::new_quota_full("5h", "5h: 10% (1h)".to_string(), 101, QUOTA_5H_COLORS),
        ];

        let result = build_statusline(sections.clone(), 100);
        assert!(result.contains("(1h)"));

        let result_narrow = build_statusline(sections, 5);
        assert!(result_narrow.contains("5h"));
    }
}