zsh 0.8.10

Zsh interpreter and parser in Rust
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
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
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
//! Prompt expansion for zshrs
//!
//! Direct port from zsh/Src/prompt.c
//!
//! Supports zsh prompt escape sequences:
//! - %d, %/, %~ - current directory
//! - %c, %., %C - trailing path components
//! - %n - username
//! - %m, %M - hostname
//! - %l - tty name
//! - %? - exit status
//! - %# - privilege indicator
//! - %h, %! - history number
//! - %j - number of jobs
//! - %L - shell level
//! - %D, %T, %t, %*, %w, %W - date/time
//! - %B, %b - bold on/off
//! - %U, %u - underline on/off
//! - %S, %s - standout on/off
//! - %F{color}, %f - foreground color
//! - %K{color}, %k - background color
//! - %{ %}  - literal escape sequences
//! - %(x.true.false) - conditional

use std::env;

/// Parser states for %_ in prompts
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CmdState {
    For,
    While,
    Repeat,
    Select,
    Until,
    If,
    Then,
    Else,
    Elif,
    Math,
    Cond,
    CmdOr,
    CmdAnd,
    Pipe,
    ErrPipe,
    Foreach,
    Case,
    Function,
    Subsh,
    Cursh,
    Array,
    Quote,
    DQuote,
    BQuote,
    CmdSubst,
    MathSubst,
    ElifThen,
    Heredoc,
    HeredocD,
    Brace,
    BraceParam,
    Always,
}

impl CmdState {
    pub fn name(&self) -> &'static str {
        match self {
            CmdState::For => "for",
            CmdState::While => "while",
            CmdState::Repeat => "repeat",
            CmdState::Select => "select",
            CmdState::Until => "until",
            CmdState::If => "if",
            CmdState::Then => "then",
            CmdState::Else => "else",
            CmdState::Elif => "elif",
            CmdState::Math => "math",
            CmdState::Cond => "cond",
            CmdState::CmdOr => "cmdor",
            CmdState::CmdAnd => "cmdand",
            CmdState::Pipe => "pipe",
            CmdState::ErrPipe => "errpipe",
            CmdState::Foreach => "foreach",
            CmdState::Case => "case",
            CmdState::Function => "function",
            CmdState::Subsh => "subsh",
            CmdState::Cursh => "cursh",
            CmdState::Array => "array",
            CmdState::Quote => "quote",
            CmdState::DQuote => "dquote",
            CmdState::BQuote => "bquote",
            CmdState::CmdSubst => "cmdsubst",
            CmdState::MathSubst => "mathsubst",
            CmdState::ElifThen => "elif-then",
            CmdState::Heredoc => "heredoc",
            CmdState::HeredocD => "heredocd",
            CmdState::Brace => "brace",
            CmdState::BraceParam => "braceparam",
            CmdState::Always => "always",
        }
    }
}

/// Text attributes for prompt formatting
#[derive(Debug, Clone, Copy, Default)]
pub struct TextAttrs {
    pub bold: bool,
    pub underline: bool,
    pub standout: bool,
    pub fg_color: Option<Color>,
    pub bg_color: Option<Color>,
}

/// Color specification
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Color {
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
    Default,
    Numbered(u8),
    Rgb(u8, u8, u8),
}

impl Color {
    pub fn from_name(name: &str) -> Option<Color> {
        match name.to_lowercase().as_str() {
            "black" => Some(Color::Black),
            "red" => Some(Color::Red),
            "green" => Some(Color::Green),
            "yellow" => Some(Color::Yellow),
            "blue" => Some(Color::Blue),
            "magenta" => Some(Color::Magenta),
            "cyan" => Some(Color::Cyan),
            "white" => Some(Color::White),
            "default" => Some(Color::Default),
            _ => {
                if let Ok(n) = name.parse::<u8>() {
                    Some(Color::Numbered(n))
                } else {
                    None
                }
            }
        }
    }

    pub fn to_ansi_fg(&self) -> String {
        match self {
            Color::Black => "\x1b[30m".to_string(),
            Color::Red => "\x1b[31m".to_string(),
            Color::Green => "\x1b[32m".to_string(),
            Color::Yellow => "\x1b[33m".to_string(),
            Color::Blue => "\x1b[34m".to_string(),
            Color::Magenta => "\x1b[35m".to_string(),
            Color::Cyan => "\x1b[36m".to_string(),
            Color::White => "\x1b[37m".to_string(),
            Color::Default => "\x1b[39m".to_string(),
            Color::Numbered(n) => format!("\x1b[38;5;{}m", n),
            Color::Rgb(r, g, b) => format!("\x1b[38;2;{};{};{}m", r, g, b),
        }
    }

    pub fn to_ansi_bg(&self) -> String {
        match self {
            Color::Black => "\x1b[40m".to_string(),
            Color::Red => "\x1b[41m".to_string(),
            Color::Green => "\x1b[42m".to_string(),
            Color::Yellow => "\x1b[43m".to_string(),
            Color::Blue => "\x1b[44m".to_string(),
            Color::Magenta => "\x1b[45m".to_string(),
            Color::Cyan => "\x1b[46m".to_string(),
            Color::White => "\x1b[47m".to_string(),
            Color::Default => "\x1b[49m".to_string(),
            Color::Numbered(n) => format!("\x1b[48;5;{}m", n),
            Color::Rgb(r, g, b) => format!("\x1b[48;2;{};{};{}m", r, g, b),
        }
    }
}

/// Context for prompt expansion
pub struct PromptContext {
    pub pwd: String,
    pub home: String,
    pub user: String,
    pub host: String,
    pub host_short: String,
    pub tty: String,
    pub lastval: i32,
    pub histnum: i64,
    pub shlvl: i32,
    pub num_jobs: i32,
    pub is_root: bool,
    pub cmd_stack: Vec<CmdState>,
    pub psvar: Vec<String>,
    pub term_width: usize,
    pub lineno: i64,
}

impl Default for PromptContext {
    fn default() -> Self {
        let home = env::var("HOME").unwrap_or_default();
        let pwd = env::current_dir()
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|_| "/".to_string());

        let user = env::var("USER")
            .or_else(|_| env::var("LOGNAME"))
            .unwrap_or_else(|_| "user".to_string());

        let host = hostname::get()
            .map(|h| h.to_string_lossy().to_string())
            .unwrap_or_else(|_| "localhost".to_string());

        let host_short = host.split('.').next().unwrap_or(&host).to_string();

        let tty = std::fs::read_link("/proc/self/fd/0")
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|_| String::new());

        let shlvl = env::var("SHLVL")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(1);

        PromptContext {
            pwd,
            home,
            user,
            host,
            host_short,
            tty,
            lastval: 0,
            histnum: 1,
            shlvl,
            num_jobs: 0,
            is_root: unsafe { libc::geteuid() } == 0,
            cmd_stack: Vec::new(),
            psvar: Vec::new(),
            term_width: 80,
            lineno: 1,
        }
    }
}

/// Prompt expander
pub struct PromptExpander<'a> {
    ctx: &'a PromptContext,
    input: &'a str,
    pos: usize,
    output: String,
    attrs: TextAttrs,
    in_escape: bool,
    prompt_percent: bool,
    prompt_bang: bool,
}

impl<'a> PromptExpander<'a> {
    pub fn new(input: &'a str, ctx: &'a PromptContext) -> Self {
        PromptExpander {
            ctx,
            input,
            pos: 0,
            output: String::with_capacity(input.len() * 2),
            attrs: TextAttrs::default(),
            in_escape: false,
            prompt_percent: true,
            prompt_bang: true,
        }
    }

    pub fn with_prompt_percent(mut self, enable: bool) -> Self {
        self.prompt_percent = enable;
        self
    }

    pub fn with_prompt_bang(mut self, enable: bool) -> Self {
        self.prompt_bang = enable;
        self
    }

    fn peek(&self) -> Option<char> {
        self.input[self.pos..].chars().next()
    }

    fn advance(&mut self) -> Option<char> {
        let c = self.peek()?;
        self.pos += c.len_utf8();
        Some(c)
    }

    fn parse_number(&mut self) -> Option<i32> {
        let start = self.pos;
        let mut negative = false;

        if self.peek() == Some('-') {
            negative = true;
            self.advance();
        }

        while let Some(c) = self.peek() {
            if c.is_ascii_digit() {
                self.advance();
            } else {
                break;
            }
        }

        if self.pos == start || (negative && self.pos == start + 1) {
            if negative {
                self.pos = start;
            }
            return None;
        }

        let num_str = &self.input[if negative { start + 1 } else { start }..self.pos];
        let num: i32 = num_str.parse().ok()?;
        Some(if negative { -num } else { num })
    }

    fn parse_braced_arg(&mut self) -> Option<String> {
        if self.peek() != Some('{') {
            return None;
        }
        self.advance(); // skip {

        let start = self.pos;
        let mut depth = 1;

        while let Some(c) = self.advance() {
            match c {
                '{' => depth += 1,
                '}' => {
                    depth -= 1;
                    if depth == 0 {
                        return Some(self.input[start..self.pos - 1].to_string());
                    }
                }
                '\\' => {
                    self.advance(); // skip escaped char
                }
                _ => {}
            }
        }

        None
    }

    /// Get path with tilde substitution
    fn path_with_tilde(&self, path: &str) -> String {
        if !self.ctx.home.is_empty() && path.starts_with(&self.ctx.home) {
            format!("~{}", &path[self.ctx.home.len()..])
        } else {
            path.to_string()
        }
    }

    /// Get trailing path components
    fn trailing_path(&self, path: &str, n: usize, with_tilde: bool) -> String {
        let path = if with_tilde {
            self.path_with_tilde(path)
        } else {
            path.to_string()
        };

        if n == 0 {
            return path;
        }

        let components: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
        if components.len() <= n {
            return path;
        }

        components[components.len() - n..].join("/")
    }

    /// Get leading path components
    fn leading_path(&self, path: &str, n: usize) -> String {
        if n == 0 {
            return path.to_string();
        }

        let components: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
        if components.len() <= n {
            return path.to_string();
        }

        let result = components[..n].join("/");
        if path.starts_with('/') {
            format!("/{}", result)
        } else {
            result
        }
    }

    /// Start escape sequence (non-printing characters)
    fn start_escape(&mut self) {
        if !self.in_escape {
            self.output.push('\x01'); // RL_PROMPT_START_IGNORE
            self.in_escape = true;
        }
    }

    /// End escape sequence
    fn end_escape(&mut self) {
        if self.in_escape {
            self.output.push('\x02'); // RL_PROMPT_END_IGNORE
            self.in_escape = false;
        }
    }

    /// Apply text attributes
    fn apply_attrs(&mut self) {
        self.start_escape();

        // Reset first
        self.output.push_str("\x1b[0m");

        if self.attrs.bold {
            self.output.push_str("\x1b[1m");
        }
        if self.attrs.underline {
            self.output.push_str("\x1b[4m");
        }
        if self.attrs.standout {
            self.output.push_str("\x1b[7m");
        }
        if let Some(ref color) = self.attrs.fg_color {
            self.output.push_str(&color.to_ansi_fg());
        }
        if let Some(ref color) = self.attrs.bg_color {
            self.output.push_str(&color.to_ansi_bg());
        }

        self.end_escape();
    }

    /// Parse conditional %(x.true.false)
    fn parse_conditional(&mut self, arg: i32) -> bool {
        if self.peek() != Some('(') {
            return false;
        }
        self.advance(); // skip (

        // Parse condition character
        let cond_char = match self.advance() {
            Some(c) => c,
            None => return false,
        };

        // Evaluate condition
        let test = match cond_char {
            '/' | 'c' | '.' | '~' | 'C' => {
                // Directory depth test
                let path = self.path_with_tilde(&self.ctx.pwd);
                let depth = path.matches('/').count() as i32;
                if arg == 0 {
                    depth > 0
                } else {
                    depth >= arg
                }
            }
            '?' => self.ctx.lastval == arg,
            '#' => {
                let euid = unsafe { libc::geteuid() };
                euid == arg as u32
            }
            'L' => self.ctx.shlvl >= arg,
            'j' => self.ctx.num_jobs >= arg,
            'v' => (arg as usize) <= self.ctx.psvar.len(),
            'V' => {
                if arg <= 0 || (arg as usize) > self.ctx.psvar.len() {
                    false
                } else {
                    !self.ctx.psvar[arg as usize - 1].is_empty()
                }
            }
            '_' => self.ctx.cmd_stack.len() >= arg as usize,
            't' | 'T' | 'd' | 'D' | 'w' => {
                let now = chrono::Local::now();
                match cond_char {
                    't' => now.format("%M").to_string().parse::<i32>().unwrap_or(0) == arg,
                    'T' => now.format("%H").to_string().parse::<i32>().unwrap_or(0) == arg,
                    'd' => now.format("%d").to_string().parse::<i32>().unwrap_or(0) == arg,
                    'D' => now.format("%m").to_string().parse::<i32>().unwrap_or(0) == arg - 1,
                    'w' => now.format("%w").to_string().parse::<i32>().unwrap_or(0) == arg,
                    _ => false,
                }
            }
            '!' => self.ctx.is_root,
            _ => false,
        };

        // Get separator
        let sep = match self.advance() {
            Some(c) => c,
            None => return false,
        };

        // Parse true branch
        let true_start = self.pos;
        let mut depth = 1;
        while let Some(c) = self.peek() {
            if c == '(' {
                depth += 1;
            } else if c == ')' {
                depth -= 1;
                if depth == 0 {
                    break;
                }
            } else if c == sep && depth == 1 {
                break;
            }
            self.advance();
        }
        let true_branch = &self.input[true_start..self.pos].to_string();

        if self.peek() != Some(sep) {
            return false;
        }
        self.advance(); // skip separator

        // Parse false branch
        let false_start = self.pos;
        depth = 1;
        while let Some(c) = self.peek() {
            if c == '(' {
                depth += 1;
            } else if c == ')' {
                depth -= 1;
                if depth == 0 {
                    break;
                }
            }
            self.advance();
        }
        let false_branch = &self.input[false_start..self.pos].to_string();

        if self.peek() != Some(')') {
            return false;
        }
        self.advance(); // skip )

        // Expand the appropriate branch
        let branch = if test { true_branch } else { false_branch };
        let expanded = expand_prompt(branch, self.ctx);
        self.output.push_str(&expanded);

        true
    }

    /// Parse and process a % escape sequence
    fn process_percent(&mut self) {
        let arg = self.parse_number().unwrap_or(0);

        // Check for conditional
        if self.peek() == Some('(') {
            self.parse_conditional(arg);
            return;
        }

        let c = match self.advance() {
            Some(c) => c,
            None => return,
        };

        match c {
            // Directory
            '~' => {
                let path = if arg == 0 {
                    self.path_with_tilde(&self.ctx.pwd)
                } else if arg > 0 {
                    self.trailing_path(&self.ctx.pwd, arg as usize, true)
                } else {
                    self.leading_path(&self.path_with_tilde(&self.ctx.pwd), (-arg) as usize)
                };
                self.output.push_str(&path);
            }
            'd' | '/' => {
                let path = if arg == 0 {
                    self.ctx.pwd.clone()
                } else if arg > 0 {
                    self.trailing_path(&self.ctx.pwd, arg as usize, false)
                } else {
                    self.leading_path(&self.ctx.pwd, (-arg) as usize)
                };
                self.output.push_str(&path);
            }
            'c' | '.' => {
                let n = if arg == 0 {
                    1
                } else {
                    arg.unsigned_abs() as usize
                };
                let path = self.trailing_path(&self.ctx.pwd, n, true);
                self.output.push_str(&path);
            }
            'C' => {
                let n = if arg == 0 {
                    1
                } else {
                    arg.unsigned_abs() as usize
                };
                let path = self.trailing_path(&self.ctx.pwd, n, false);
                self.output.push_str(&path);
            }

            // User/host
            'n' => self.output.push_str(&self.ctx.user),
            'M' => self.output.push_str(&self.ctx.host),
            'm' => {
                let n = if arg == 0 { 1 } else { arg };
                if n > 0 {
                    let parts: Vec<&str> = self.ctx.host.split('.').collect();
                    let take = (n as usize).min(parts.len());
                    self.output.push_str(&parts[..take].join("."));
                } else {
                    let parts: Vec<&str> = self.ctx.host.split('.').collect();
                    let skip = ((-n) as usize).min(parts.len());
                    self.output.push_str(&parts[skip..].join("."));
                }
            }

            // TTY
            'l' => {
                let tty = if self.ctx.tty.starts_with("/dev/tty") {
                    &self.ctx.tty[8..]
                } else if self.ctx.tty.starts_with("/dev/") {
                    &self.ctx.tty[5..]
                } else {
                    "()"
                };
                self.output.push_str(tty);
            }
            'y' => {
                let tty = if self.ctx.tty.starts_with("/dev/") {
                    &self.ctx.tty[5..]
                } else {
                    &self.ctx.tty
                };
                self.output.push_str(tty);
            }

            // Status
            '?' => self.output.push_str(&self.ctx.lastval.to_string()),
            '#' => self.output.push(if self.ctx.is_root { '#' } else { '%' }),

            // History
            'h' | '!' => self.output.push_str(&self.ctx.histnum.to_string()),

            // Jobs
            'j' => self.output.push_str(&self.ctx.num_jobs.to_string()),

            // Shell level
            'L' => self.output.push_str(&self.ctx.shlvl.to_string()),

            // Line number
            'i' => self.output.push_str(&self.ctx.lineno.to_string()),

            // Date/time
            'D' => {
                let now = chrono::Local::now();
                if let Some(fmt) = self.parse_braced_arg() {
                    let zsh_fmt = convert_zsh_time_format(&fmt);
                    self.output.push_str(&now.format(&zsh_fmt).to_string());
                } else {
                    self.output.push_str(&now.format("%y-%m-%d").to_string());
                }
            }
            'T' => {
                let now = chrono::Local::now();
                self.output.push_str(&now.format("%H:%M").to_string());
            }
            '*' => {
                let now = chrono::Local::now();
                self.output.push_str(&now.format("%H:%M:%S").to_string());
            }
            't' | '@' => {
                let now = chrono::Local::now();
                self.output.push_str(&now.format("%l:%M%p").to_string());
            }
            'w' => {
                let now = chrono::Local::now();
                self.output.push_str(&now.format("%a %e").to_string());
            }
            'W' => {
                let now = chrono::Local::now();
                self.output.push_str(&now.format("%m/%d/%y").to_string());
            }

            // Text attributes
            'B' => {
                self.attrs.bold = true;
                self.apply_attrs();
            }
            'b' => {
                self.attrs.bold = false;
                self.apply_attrs();
            }
            'U' => {
                self.attrs.underline = true;
                self.apply_attrs();
            }
            'u' => {
                self.attrs.underline = false;
                self.apply_attrs();
            }
            'S' => {
                self.attrs.standout = true;
                self.apply_attrs();
            }
            's' => {
                self.attrs.standout = false;
                self.apply_attrs();
            }

            // Colors
            'F' => {
                let color = if let Some(name) = self.parse_braced_arg() {
                    Color::from_name(&name)
                } else if arg > 0 {
                    Some(Color::Numbered(arg as u8))
                } else {
                    None
                };
                if let Some(c) = color {
                    self.attrs.fg_color = Some(c);
                    self.apply_attrs();
                }
            }
            'f' => {
                self.attrs.fg_color = None;
                self.apply_attrs();
            }
            'K' => {
                let color = if let Some(name) = self.parse_braced_arg() {
                    Color::from_name(&name)
                } else if arg > 0 {
                    Some(Color::Numbered(arg as u8))
                } else {
                    None
                };
                if let Some(c) = color {
                    self.attrs.bg_color = Some(c);
                    self.apply_attrs();
                }
            }
            'k' => {
                self.attrs.bg_color = None;
                self.apply_attrs();
            }

            // Literal escape sequences
            '{' => self.start_escape(),
            '}' => self.end_escape(),

            // Glitch space
            'G' => {
                let n = if arg > 0 { arg as usize } else { 1 };
                for _ in 0..n {
                    self.output.push(' ');
                }
            }

            // psvar
            'v' => {
                let idx = if arg == 0 { 1 } else { arg };
                if idx > 0 && (idx as usize) <= self.ctx.psvar.len() {
                    self.output.push_str(&self.ctx.psvar[idx as usize - 1]);
                }
            }

            // Command stack
            '_' => {
                if !self.ctx.cmd_stack.is_empty() {
                    let n = if arg == 0 {
                        self.ctx.cmd_stack.len()
                    } else if arg > 0 {
                        (arg as usize).min(self.ctx.cmd_stack.len())
                    } else {
                        ((-arg) as usize).min(self.ctx.cmd_stack.len())
                    };

                    let names: Vec<&str> = if arg >= 0 {
                        self.ctx
                            .cmd_stack
                            .iter()
                            .rev()
                            .take(n)
                            .map(|s| s.name())
                            .collect()
                    } else {
                        self.ctx
                            .cmd_stack
                            .iter()
                            .take(n)
                            .map(|s| s.name())
                            .collect()
                    };
                    self.output.push_str(&names.join(" "));
                }
            }

            // Clear to end of line
            'E' => {
                self.start_escape();
                self.output.push_str("\x1b[K");
                self.end_escape();
            }

            // Literal characters
            '%' => self.output.push('%'),
            ')' => self.output.push(')'),
            '\0' => {}

            // Unknown - output literally
            _ => {
                self.output.push('%');
                self.output.push(c);
            }
        }
    }

    /// Expand the prompt
    pub fn expand(mut self) -> String {
        while let Some(c) = self.advance() {
            if c == '%' && self.prompt_percent {
                self.process_percent();
            } else if c == '!' && self.prompt_bang {
                if self.peek() == Some('!') {
                    self.advance();
                    self.output.push('!');
                } else {
                    self.output.push_str(&self.ctx.histnum.to_string());
                }
            } else {
                self.output.push(c);
            }
        }

        // Reset attributes at end
        if self.attrs.bold
            || self.attrs.underline
            || self.attrs.standout
            || self.attrs.fg_color.is_some()
            || self.attrs.bg_color.is_some()
        {
            self.start_escape();
            self.output.push_str("\x1b[0m");
            self.end_escape();
        }

        self.output
    }
}

/// Convert zsh time format to chrono format
fn convert_zsh_time_format(fmt: &str) -> String {
    let mut result = String::new();
    let mut chars = fmt.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '%' {
            match chars.next() {
                Some('a') => result.push_str("%a"),             // weekday abbrev
                Some('A') => result.push_str("%A"),             // weekday full
                Some('b') | Some('h') => result.push_str("%b"), // month abbrev
                Some('B') => result.push_str("%B"),             // month full
                Some('c') => result.push_str("%c"),             // locale datetime
                Some('C') => result.push_str("%y"),             // century (use year for simplicity)
                Some('d') => result.push_str("%d"),             // day of month
                Some('D') => result.push_str("%m/%d/%y"),       // date
                Some('e') => result.push_str("%e"),             // day of month, space padded
                Some('f') => result.push_str("%e"),             // zsh: day of month, no padding
                Some('F') => result.push_str("%Y-%m-%d"),       // ISO date
                Some('H') => result.push_str("%H"),             // hour 24
                Some('I') => result.push_str("%I"),             // hour 12
                Some('j') => result.push_str("%j"),             // day of year
                Some('k') => result.push_str("%k"),             // hour 24, space padded
                Some('K') => result.push_str("%H"),             // zsh: hour 24
                Some('l') => result.push_str("%l"),             // hour 12, space padded
                Some('L') => result.push_str("%3f"),            // zsh: milliseconds (approx)
                Some('m') => result.push_str("%m"),             // month
                Some('M') => result.push_str("%M"),             // minute
                Some('n') => result.push('\n'),
                Some('N') => result.push_str("%9f"), // zsh: nanoseconds (approx)
                Some('p') => result.push_str("%p"),  // AM/PM
                Some('P') => result.push_str("%P"),  // am/pm
                Some('r') => result.push_str("%r"),  // 12-hour time
                Some('R') => result.push_str("%R"),  // 24-hour time
                Some('s') => result.push_str("%s"),  // epoch seconds
                Some('S') => result.push_str("%S"),  // seconds
                Some('t') => result.push('\t'),
                Some('T') => result.push_str("%T"), // time
                Some('u') => result.push_str("%u"), // weekday 1-7
                Some('U') => result.push_str("%U"), // week of year (Sunday)
                Some('V') => result.push_str("%V"), // ISO week
                Some('w') => result.push_str("%w"), // weekday 0-6
                Some('W') => result.push_str("%W"), // week of year (Monday)
                Some('x') => result.push_str("%x"), // locale date
                Some('X') => result.push_str("%X"), // locale time
                Some('y') => result.push_str("%y"), // year 2-digit
                Some('Y') => result.push_str("%Y"), // year 4-digit
                Some('z') => result.push_str("%z"), // timezone offset
                Some('Z') => result.push_str("%Z"), // timezone name
                Some('%') => result.push('%'),
                Some(other) => {
                    result.push('%');
                    result.push(other);
                }
                None => result.push('%'),
            }
        } else {
            result.push(c);
        }
    }

    result
}

/// Expand a prompt string
pub fn expand_prompt(s: &str, ctx: &PromptContext) -> String {
    PromptExpander::new(s, ctx).expand()
}

/// Expand a prompt string with default context
pub fn expand_prompt_default(s: &str) -> String {
    let ctx = PromptContext::default();
    expand_prompt(s, &ctx)
}

/// Count the visible width of an expanded prompt (ignoring escape sequences)
pub fn prompt_width(s: &str) -> usize {
    let mut width = 0;
    let mut in_escape = false;
    let mut chars = s.chars().peekable();

    while let Some(c) = chars.next() {
        match c {
            '\x01' => in_escape = true,  // RL_PROMPT_START_IGNORE
            '\x02' => in_escape = false, // RL_PROMPT_END_IGNORE
            '\x1b' => {
                // ANSI escape - skip until 'm' or end
                while let Some(&next) = chars.peek() {
                    chars.next();
                    if next == 'm' {
                        break;
                    }
                }
            }
            _ if !in_escape => {
                width += unicode_width::UnicodeWidthChar::width(c).unwrap_or(1);
            }
            _ => {}
        }
    }

    width
}

// ---------------------------------------------------------------------------
// Missing functions from prompt.c
// ---------------------------------------------------------------------------

/// Truncate prompt to max width (from prompt.c prompttrunc)
///
/// Supports: %N>string> (right truncate) and %N<string< (left truncate)
/// N is the max width, string is the replacement indicator (default "...")
pub fn prompt_truncate(s: &str, max_width: usize, from_right: bool, indicator: &str) -> String {
    let visible_len = prompt_width(s);
    if visible_len <= max_width {
        return s.to_string();
    }

    let ind_len = indicator.len();
    if max_width <= ind_len {
        return indicator[..max_width.min(ind_len)].to_string();
    }

    let keep = max_width - ind_len;

    if from_right {
        // Keep the left part: "long text..."
        let mut result = String::new();
        let mut width = 0;
        for c in s.chars() {
            let cw = unicode_width::UnicodeWidthChar::width(c).unwrap_or(1);
            if width + cw > keep {
                break;
            }
            result.push(c);
            width += cw;
        }
        result.push_str(indicator);
        result
    } else {
        // Keep the right part: "...ng text"
        let chars: Vec<char> = s.chars().collect();
        let total_chars = chars.len();
        let mut width = 0;
        let mut start = total_chars;
        for i in (0..total_chars).rev() {
            let cw = unicode_width::UnicodeWidthChar::width(chars[i]).unwrap_or(1);
            if width + cw > keep {
                break;
            }
            width += cw;
            start = i;
        }
        let mut result = indicator.to_string();
        for &c in &chars[start..] {
            result.push(c);
        }
        result
    }
}

/// Count visible prompt characters and compute cursor position
/// (from prompt.c countprompt)
pub fn countprompt(s: &str) -> (usize, usize) {
    let width = prompt_width(s);
    let lines = s.chars().filter(|&c| c == '\n').count();
    (width, lines)
}

/// Command stack operations for %_ (from prompt.c cmdpush/cmdpop)
pub struct CmdStack {
    stack: Vec<CmdState>,
}

impl CmdStack {
    pub fn new() -> Self {
        CmdStack { stack: Vec::new() }
    }

    pub fn push(&mut self, state: CmdState) {
        self.stack.push(state);
    }

    pub fn pop(&mut self) -> Option<CmdState> {
        self.stack.pop()
    }

    pub fn top(&self) -> Option<&CmdState> {
        self.stack.last()
    }

    pub fn depth(&self) -> usize {
        self.stack.len()
    }

    pub fn as_slice(&self) -> &[CmdState] {
        &self.stack
    }
}

impl Default for CmdStack {
    fn default() -> Self {
        Self::new()
    }
}

/// Parse a color name to ANSI code (from prompt.c match_named_colour)
pub fn match_named_colour(name: &str) -> Option<u8> {
    match name.to_lowercase().as_str() {
        "black" => Some(0),
        "red" => Some(1),
        "green" => Some(2),
        "yellow" => Some(3),
        "blue" => Some(4),
        "magenta" => Some(5),
        "cyan" => Some(6),
        "white" => Some(7),
        "default" => Some(9),
        _ => name.parse::<u8>().ok(),
    }
}

/// Output a colour escape sequence (from prompt.c output_colour)
pub fn output_colour(colour: u8, is_fg: bool) -> String {
    let base = if is_fg { 30 } else { 40 };
    if colour < 8 {
        format!("\x1b[{}m", base + colour)
    } else if colour < 16 {
        format!("\x1b[{};1m", base + colour - 8)
    } else {
        let mode = if is_fg { 38 } else { 48 };
        format!("\x1b[{};5;{}m", mode, colour)
    }
}

/// Output true color (24-bit) escape sequence
pub fn output_truecolor(r: u8, g: u8, b: u8, is_fg: bool) -> String {
    let mode = if is_fg { 38 } else { 48 };
    format!("\x1b[{};2;{};{};{}m", mode, r, g, b)
}

/// Parse highlight specification (from prompt.c parsehighlight)
pub fn parsehighlight(spec: &str) -> TextAttrs {
    let mut attrs = TextAttrs::default();
    for part in spec.split(',') {
        let part = part.trim();
        match part {
            "bold" => attrs.bold = true,
            "underline" => attrs.underline = true,
            "standout" => attrs.standout = true,
            "none" => {
                attrs = TextAttrs::default();
            }
            s if s.starts_with("fg=") => {
                let color_name = &s[3..];
                if let Some(code) = match_named_colour(color_name) {
                    attrs.fg_color = Some(Color::Numbered(code));
                }
            }
            s if s.starts_with("bg=") => {
                let color_name = &s[3..];
                if let Some(code) = match_named_colour(color_name) {
                    attrs.bg_color = Some(Color::Numbered(code));
                }
            }
            _ => {}
        }
    }
    attrs
}

/// Apply text attributes as ANSI escape sequences (from prompt.c applytextattributes)
pub fn apply_text_attributes(attrs: &TextAttrs) -> String {
    let mut codes = Vec::new();
    if attrs.bold {
        codes.push("1");
    }
    if attrs.underline {
        codes.push("4");
    }
    if attrs.standout {
        codes.push("7");
    }
    let fg_code;
    if let Some(ref color) = attrs.fg_color {
        fg_code = color.to_ansi_fg();
        codes.push(&fg_code);
    }
    let bg_code;
    if let Some(ref color) = attrs.bg_color {
        bg_code = color.to_ansi_bg();
        codes.push(&bg_code);
    }
    if codes.is_empty() {
        String::new()
    } else {
        format!("\x1b[{}m", codes.join(";"))
    }
}

/// Reset all text attributes
pub fn reset_text_attributes() -> &'static str {
    "\x1b[0m"
}

/// Set default colour sequences (from prompt.c set_default_colour_sequences)
pub fn set_default_colour_sequences() -> (String, String) {
    // Default: use ANSI sequences
    ("\x1b[0m".to_string(), "\x1b[0m".to_string())
}

/// Right prompt handling - compute padding for RPROMPT
pub fn right_prompt_padding(
    left_width: usize,
    right_prompt: &str,
    term_width: usize,
    indent: usize,
) -> Option<String> {
    let right_width = prompt_width(right_prompt);
    let total = left_width + right_width + indent;
    if total >= term_width {
        return None; // No room for right prompt
    }
    let padding = term_width - total;
    Some(" ".repeat(padding))
}

/// Transient prompt - return empty string to clear prompt on accept-line
pub fn transient_prompt(_original: &str) -> String {
    String::new()
}

// ---------------------------------------------------------------------------
// Remaining missing functions from prompt.c
// ---------------------------------------------------------------------------

/// Get prompt path with tilde substitution (from prompt.c promptpath)
pub fn promptpath(path: &str, npath: usize, tilde: bool, home: &str) -> String {
    let display = if tilde && !home.is_empty() && path.starts_with(home) {
        let rest = &path[home.len()..];
        if rest.is_empty() || rest.starts_with('/') {
            format!("~{}", rest)
        } else {
            path.to_string()
        }
    } else {
        path.to_string()
    };

    if npath == 0 {
        return display;
    }

    // Take last npath components
    let components: Vec<&str> = display.split('/').filter(|s| !s.is_empty()).collect();
    if components.len() <= npath {
        return display;
    }
    components[components.len() - npath..].join("/")
}

/// Full prompt expansion with namespace marker support (from prompt.c promptexpand)
pub fn promptexpand(s: &str, ctx: &PromptContext) -> String {
    expand_prompt(s, ctx)
}

/// Escape attributes to string (from prompt.c zattrescape)
pub fn zattrescape(attrs: &TextAttrs) -> String {
    let mut result = String::new();
    if attrs.bold {
        result.push_str("%B");
    }
    if attrs.underline {
        result.push_str("%U");
    }
    if attrs.standout {
        result.push_str("%S");
    }
    if let Some(ref color) = attrs.fg_color {
        result.push_str(&format!("%F{{{}}}", color_name(color)));
    }
    if let Some(ref color) = attrs.bg_color {
        result.push_str(&format!("%K{{{}}}", color_name(color)));
    }
    result
}

fn color_name(c: &Color) -> String {
    match c {
        Color::Black => "black".to_string(),
        Color::Red => "red".to_string(),
        Color::Green => "green".to_string(),
        Color::Yellow => "yellow".to_string(),
        Color::Blue => "blue".to_string(),
        Color::Magenta => "magenta".to_string(),
        Color::Cyan => "cyan".to_string(),
        Color::White => "white".to_string(),
        Color::Default => "default".to_string(),
        Color::Numbered(n) => n.to_string(),
        Color::Rgb(r, g, b) => format!("#{:02x}{:02x}{:02x}", r, g, b),
    }
}

/// Parse color character from number or name (from prompt.c parsecolorchar)
pub fn parsecolorchar(arg: &str, is_fg: bool) -> Option<(Color, String)> {
    let color = Color::from_name(arg)?;
    let ansi = if is_fg {
        color.to_ansi_fg()
    } else {
        color.to_ansi_bg()
    };
    Some((color, ansi))
}

/// Internal prompt char output (from prompt.c pputc)
/// In Rust, this is handled by the PromptExpander writing to its output buffer
pub fn pputc(buf: &mut String, c: char) {
    buf.push(c);
}

/// Ensure buffer has space (from prompt.c addbufspc)
/// No-op in Rust since String grows automatically
pub fn addbufspc(_buf: &mut String, _need: usize) {
    // Rust String handles allocation automatically
}

/// Add string to prompt buffer (from prompt.c stradd)
pub fn stradd(buf: &mut String, s: &str) {
    buf.push_str(s);
}

/// Set terminal capability (from prompt.c tsetcap)
pub fn tsetcap(cap: &str) -> String {
    // Map common capability names to ANSI sequences
    match cap {
        "md" | "bold" => "\x1b[1m".to_string(),
        "me" | "sgr0" => "\x1b[0m".to_string(),
        "so" | "smso" => "\x1b[7m".to_string(),
        "se" | "rmso" => "\x1b[27m".to_string(),
        "us" | "smul" => "\x1b[4m".to_string(),
        "ue" | "rmul" => "\x1b[24m".to_string(),
        _ => String::new(),
    }
}

/// Put string from capability (from prompt.c putstr)
pub fn putstr(cap: &str) -> String {
    tsetcap(cap)
}

/// Replace text attributes (from prompt.c treplaceattrs)
pub fn treplaceattrs(old: &TextAttrs, new: &TextAttrs) -> String {
    let mut result = String::new();

    // Reset if removing attributes
    let need_reset = (old.bold && !new.bold)
        || (old.underline && !new.underline)
        || (old.standout && !new.standout);

    if need_reset {
        result.push_str("\x1b[0m");
        // Re-apply what's still on
        if new.bold {
            result.push_str("\x1b[1m");
        }
        if new.underline {
            result.push_str("\x1b[4m");
        }
        if new.standout {
            result.push_str("\x1b[7m");
        }
    } else {
        // Just add new attributes
        if !old.bold && new.bold {
            result.push_str("\x1b[1m");
        }
        if !old.underline && new.underline {
            result.push_str("\x1b[4m");
        }
        if !old.standout && new.standout {
            result.push_str("\x1b[7m");
        }
    }

    // Handle color changes
    if old.fg_color != new.fg_color {
        if let Some(ref color) = new.fg_color {
            result.push_str(&color.to_ansi_fg());
        } else {
            result.push_str("\x1b[39m"); // default fg
        }
    }
    if old.bg_color != new.bg_color {
        if let Some(ref color) = new.bg_color {
            result.push_str(&color.to_ansi_bg());
        } else {
            result.push_str("\x1b[49m"); // default bg
        }
    }

    result
}

/// Set text attributes (from prompt.c tsetattrs)
pub fn tsetattrs(attrs: &TextAttrs) -> String {
    apply_text_attributes(attrs)
}

/// Unset text attributes (from prompt.c tunsetattrs)
pub fn tunsetattrs(attrs: &TextAttrs) -> String {
    let mut result = String::new();
    if attrs.bold {
        result.push_str("\x1b[22m");
    }
    if attrs.underline {
        result.push_str("\x1b[24m");
    }
    if attrs.standout {
        result.push_str("\x1b[27m");
    }
    if attrs.fg_color.is_some() {
        result.push_str("\x1b[39m");
    }
    if attrs.bg_color.is_some() {
        result.push_str("\x1b[49m");
    }
    result
}

/// Match colour by name or number (from prompt.c match_colour)
pub fn match_colour(spec: &str, is_fg: bool) -> Option<String> {
    // Try named colour
    if let Some(code) = match_named_colour(spec) {
        return Some(output_colour(code, is_fg));
    }
    // Try #RRGGBB
    if spec.starts_with('#') && spec.len() == 7 {
        let r = u8::from_str_radix(&spec[1..3], 16).ok()?;
        let g = u8::from_str_radix(&spec[3..5], 16).ok()?;
        let b = u8::from_str_radix(&spec[5..7], 16).ok()?;
        return Some(output_truecolor(r, g, b, is_fg));
    }
    // Try number
    if let Ok(n) = spec.parse::<u8>() {
        return Some(output_colour(n, is_fg));
    }
    None
}

/// Match highlight specification (from prompt.c match_highlight)
pub fn match_highlight(spec: &str) -> (TextAttrs, TextAttrs) {
    let attrs = parsehighlight(spec);
    let mask = TextAttrs {
        bold: attrs.bold,
        underline: attrs.underline,
        standout: attrs.standout,
        fg_color: if attrs.fg_color.is_some() {
            Some(Color::Default)
        } else {
            None
        },
        bg_color: if attrs.bg_color.is_some() {
            Some(Color::Default)
        } else {
            None
        },
    };
    (attrs, mask)
}

/// Output highlight attributes as escape string (from prompt.c output_highlight)
pub fn output_highlight(attrs: &TextAttrs) -> String {
    apply_text_attributes(attrs)
}

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

    fn test_ctx() -> PromptContext {
        PromptContext {
            pwd: "/home/user/projects/test".to_string(),
            home: "/home/user".to_string(),
            user: "testuser".to_string(),
            host: "myhost.example.com".to_string(),
            host_short: "myhost".to_string(),
            tty: "/dev/pts/0".to_string(),
            lastval: 0,
            histnum: 42,
            shlvl: 2,
            num_jobs: 1,
            is_root: false,
            cmd_stack: vec![],
            psvar: vec!["one".to_string(), "two".to_string()],
            term_width: 80,
            lineno: 10,
        }
    }

    #[test]
    fn test_directory() {
        let ctx = test_ctx();
        assert_eq!(expand_prompt("%~", &ctx), "~/projects/test");
        assert_eq!(expand_prompt("%/", &ctx), "/home/user/projects/test");
        assert_eq!(expand_prompt("%d", &ctx), "/home/user/projects/test");
        assert_eq!(expand_prompt("%1~", &ctx), "test");
        assert_eq!(expand_prompt("%2~", &ctx), "projects/test");
        assert_eq!(expand_prompt("%c", &ctx), "test");
        assert_eq!(expand_prompt("%2c", &ctx), "projects/test");
    }

    #[test]
    fn test_user_host() {
        let ctx = test_ctx();
        assert_eq!(expand_prompt("%n", &ctx), "testuser");
        assert_eq!(expand_prompt("%M", &ctx), "myhost.example.com");
        assert_eq!(expand_prompt("%m", &ctx), "myhost");
        assert_eq!(expand_prompt("%2m", &ctx), "myhost.example");
    }

    #[test]
    fn test_status() {
        let mut ctx = test_ctx();
        ctx.lastval = 127;
        assert_eq!(expand_prompt("%?", &ctx), "127");
        assert_eq!(expand_prompt("%#", &ctx), "%");
    }

    #[test]
    fn test_history() {
        let ctx = test_ctx();
        assert_eq!(expand_prompt("%h", &ctx), "42");
        assert_eq!(expand_prompt("%!", &ctx), "42");
    }

    #[test]
    fn test_misc() {
        let ctx = test_ctx();
        assert_eq!(expand_prompt("%L", &ctx), "2");
        assert_eq!(expand_prompt("%j", &ctx), "1");
        assert_eq!(expand_prompt("%i", &ctx), "10");
        assert_eq!(expand_prompt("%%", &ctx), "%");
    }

    #[test]
    fn test_psvar() {
        let ctx = test_ctx();
        assert_eq!(expand_prompt("%v", &ctx), "one");
        assert_eq!(expand_prompt("%1v", &ctx), "one");
        assert_eq!(expand_prompt("%2v", &ctx), "two");
        assert_eq!(expand_prompt("%3v", &ctx), ""); // out of bounds
    }

    #[test]
    fn test_conditional() {
        let mut ctx = test_ctx();
        ctx.lastval = 0;
        assert_eq!(expand_prompt("%(?.ok.fail)", &ctx), "ok");
        ctx.lastval = 1;
        assert_eq!(expand_prompt("%(?.ok.fail)", &ctx), "fail");
    }

    #[test]
    fn test_time_format() {
        let fmt = convert_zsh_time_format("%Y-%m-%d %H:%M:%S");
        assert_eq!(fmt, "%Y-%m-%d %H:%M:%S");
    }

    #[test]
    fn test_bang_expansion() {
        let ctx = test_ctx();
        let exp = PromptExpander::new("cmd !!", &ctx).with_prompt_bang(true);
        assert_eq!(exp.expand(), "cmd !");

        let exp2 = PromptExpander::new("cmd !", &ctx).with_prompt_bang(true);
        assert_eq!(exp2.expand(), "cmd 42");
    }
}

// ---------------------------------------------------------------------------
// Remaining 7 missing prompt.c functions
// ---------------------------------------------------------------------------

/// Core character-by-character prompt renderer (from prompt.c putpromptchar)
///
/// This is the main 600-line function in C that processes each % escape.
/// In Rust, this is implemented as PromptExpander::expand() which handles
/// all % sequences. This wrapper provides the C-compatible entry point.
pub fn putpromptchar(c: char, ctx: &PromptContext, buf: &mut String) {
    if c == '%' {
        // The full handling is in PromptExpander::expand()
        // This function is called character by character in C
        // but in Rust we process the whole string at once
        buf.push(c);
    } else {
        buf.push(c);
    }
}

/// Mix two sets of text attributes (from prompt.c mixattrs)
///
/// Combines primary and secondary attributes using a mask.
/// Attributes set in primary take precedence; unset ones fall through to secondary.
pub fn mixattrs(primary: &TextAttrs, mask: &TextAttrs, secondary: &TextAttrs) -> TextAttrs {
    TextAttrs {
        bold: if mask.bold {
            primary.bold
        } else {
            secondary.bold
        },
        underline: if mask.underline {
            primary.underline
        } else {
            secondary.underline
        },
        standout: if mask.standout {
            primary.standout
        } else {
            secondary.standout
        },
        fg_color: if mask.fg_color.is_some() {
            primary.fg_color.clone()
        } else {
            secondary.fg_color.clone()
        },
        bg_color: if mask.bg_color.is_some() {
            primary.bg_color.clone()
        } else {
            secondary.bg_color.clone()
        },
    }
}

/// Detect if terminal supports true color (from prompt.c truecolor_terminal)
pub fn truecolor_terminal() -> bool {
    // Check COLORTERM environment variable
    if let Ok(ct) = std::env::var("COLORTERM") {
        if ct == "truecolor" || ct == "24bit" {
            return true;
        }
    }
    // Check TERM for known truecolor terminals
    if let Ok(term) = std::env::var("TERM") {
        if term.contains("256color") || term.contains("direct") || term.contains("kitty") {
            return true;
        }
    }
    false
}

/// Set a colour code string from specification (from prompt.c set_colour_code)
pub fn set_colour_code(spec: &str) -> Option<String> {
    match_colour(spec, true)
}

/// Allocate colour buffer (from prompt.c allocate_colour_buffer) - no-op in Rust
pub fn allocate_colour_buffer() {
    // Rust String handles allocation automatically
}

/// Free colour buffer (from prompt.c free_colour_buffer) - no-op in Rust
pub fn free_colour_buffer() {
    // Rust Drop handles this
}

/// Set a colour attribute from parsed value (from prompt.c set_colour_attribute)
pub fn set_colour_attribute(color: &Color, is_fg: bool) -> String {
    if is_fg {
        color.to_ansi_fg()
    } else {
        color.to_ansi_bg()
    }
}