zsh 0.8.13

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
//! Termcap module - port of Modules/termcap.c
//!
//! Provides termcap manipulation through the echotc builtin and termcap hash.

use std::collections::HashMap;

/// Termcap boolean capability codes
pub static BOOL_CODES: &[&str] = &[
    "bw", "am", "ut", "cc", "xs", "YA", "YF", "YB", "xt", "xn", "eo", "gn", "hc", "HC", "km", "YC",
    "hs", "hl", "in", "YG", "da", "db", "mi", "ms", "nx", "xb", "NP", "ND", "NR", "os", "5i", "YD",
    "YE", "es", "hz", "ul", "xo",
];

/// Termcap numeric capability codes
pub static NUM_CODES: &[&str] = &[
    "co", "it", "lh", "lw", "li", "lm", "sg", "ma", "Co", "pa", "MW", "NC", "Nl", "pb", "vt", "ws",
    "Yo", "Yp", "Ya", "BT", "Yc", "Yb", "Yd", "Ye", "Yf", "Yg", "Yh", "Yi", "Yk", "Yj", "Yl", "Ym",
    "Yn",
];

/// Termcap string capability codes
pub static STR_CODES: &[&str] = &[
    "ac", "bt", "bl", "cr", "ZA", "ZB", "ZC", "ZD", "cs", "rP", "ct", "MC", "cl", "cb", "ce", "cd",
    "ch", "CC", "CW", "cm", "do", "ho", "vi", "le", "CM", "ve", "nd", "ll", "up", "vs", "ZE", "dc",
    "dl", "DI", "ds", "DK", "hd", "eA", "as", "SA", "mb", "md", "ti", "dm", "mh", "ZF", "ZG", "im",
    "ZH", "ZI", "ZJ", "ZK", "ZL", "mp", "mr", "mk", "ZM", "so", "ZN", "ZO", "us", "ZP", "SX", "ec",
    "ae", "RA", "me", "te", "ed", "ZQ", "ei", "ZR", "ZS", "ZT", "ZU", "se", "ZV", "ZW", "ue", "ZX",
    "RX", "PA", "fh", "vb", "ff", "fs", "WG", "HU", "i1", "is", "i3", "if", "iP", "Ic", "Ip", "ic",
    "al", "ip", "K1", "K3", "K2", "kb", "kB", "K4", "K5", "ka", "kC", "kt", "kD", "kL", "kd", "kM",
    "kE", "kS", "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7", "k8", "k9", "kh", "kI", "kA", "kl",
    "kH", "kN", "kP", "kr", "kF", "kR", "kT", "ku", "ke", "ks", "l0", "l1", "l2", "l3", "l4", "l5",
    "l6", "l7", "l8", "l9", "nw", "oc", "op", "pc", "DC", "DL", "DO", "IC", "SF", "AL", "LE", "RI",
    "SR", "UP", "pk", "pl", "px", "pn", "ps", "pO", "pf", "po", "rc", "cv", "sc", "sf", "sr", "sa",
    "st", "ta", "ts", "uc", "hu",
];

/// Termcap capability value
#[derive(Debug, Clone)]
pub enum TermcapValue {
    Boolean(bool),
    Number(i32),
    String(String),
}

/// Termcap interface using basic ANSI escape sequences
#[derive(Debug, Default)]
pub struct Termcap {
    initialized: bool,
    terminal: Option<String>,
    capabilities: HashMap<String, TermcapValue>,
}

impl Termcap {
    pub fn new() -> Self {
        Self::default()
    }

    /// Initialize termcap for the given terminal
    pub fn init(&mut self, term: Option<&str>) -> bool {
        let terminal = term
            .map(|s| s.to_string())
            .or_else(|| std::env::var("TERM").ok());

        if let Some(t) = terminal {
            self.terminal = Some(t.clone());
            self.load_capabilities(&t);
            self.initialized = true;
            return true;
        }

        false
    }

    fn load_capabilities(&mut self, term: &str) {
        let is_xterm =
            term.contains("xterm") || term.contains("256color") || term.contains("screen");
        let is_ansi = is_xterm || term.contains("ansi") || term.contains("vt100");

        self.capabilities
            .insert("am".to_string(), TermcapValue::Boolean(true));
        self.capabilities
            .insert("km".to_string(), TermcapValue::Boolean(true));
        self.capabilities
            .insert("mi".to_string(), TermcapValue::Boolean(true));
        self.capabilities
            .insert("ms".to_string(), TermcapValue::Boolean(true));
        self.capabilities
            .insert("xn".to_string(), TermcapValue::Boolean(true));
        self.capabilities
            .insert("ut".to_string(), TermcapValue::Boolean(is_xterm));

        let cols = std::env::var("COLUMNS")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(80);
        let lines = std::env::var("LINES")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(24);
        let colors = if term.contains("256") {
            256
        } else if is_xterm {
            8
        } else {
            2
        };

        self.capabilities
            .insert("co".to_string(), TermcapValue::Number(cols));
        self.capabilities
            .insert("li".to_string(), TermcapValue::Number(lines));
        self.capabilities
            .insert("Co".to_string(), TermcapValue::Number(colors));
        self.capabilities
            .insert("it".to_string(), TermcapValue::Number(8));

        if is_ansi {
            self.capabilities.insert(
                "cl".to_string(),
                TermcapValue::String("\x1b[H\x1b[2J".to_string()),
            );
            self.capabilities.insert(
                "cm".to_string(),
                TermcapValue::String("\x1b[%i%d;%dH".to_string()),
            );
            self.capabilities
                .insert("up".to_string(), TermcapValue::String("\x1b[A".to_string()));
            self.capabilities
                .insert("do".to_string(), TermcapValue::String("\x1b[B".to_string()));
            self.capabilities
                .insert("nd".to_string(), TermcapValue::String("\x1b[C".to_string()));
            self.capabilities
                .insert("le".to_string(), TermcapValue::String("\x1b[D".to_string()));
            self.capabilities
                .insert("ho".to_string(), TermcapValue::String("\x1b[H".to_string()));
            self.capabilities
                .insert("ce".to_string(), TermcapValue::String("\x1b[K".to_string()));
            self.capabilities
                .insert("cd".to_string(), TermcapValue::String("\x1b[J".to_string()));
            self.capabilities
                .insert("me".to_string(), TermcapValue::String("\x1b[m".to_string()));
            self.capabilities.insert(
                "md".to_string(),
                TermcapValue::String("\x1b[1m".to_string()),
            );
            self.capabilities.insert(
                "mr".to_string(),
                TermcapValue::String("\x1b[7m".to_string()),
            );
            self.capabilities.insert(
                "us".to_string(),
                TermcapValue::String("\x1b[4m".to_string()),
            );
            self.capabilities.insert(
                "ue".to_string(),
                TermcapValue::String("\x1b[24m".to_string()),
            );
            self.capabilities.insert(
                "so".to_string(),
                TermcapValue::String("\x1b[7m".to_string()),
            );
            self.capabilities.insert(
                "se".to_string(),
                TermcapValue::String("\x1b[27m".to_string()),
            );
            self.capabilities.insert(
                "vi".to_string(),
                TermcapValue::String("\x1b[?25l".to_string()),
            );
            self.capabilities.insert(
                "ve".to_string(),
                TermcapValue::String("\x1b[?25h".to_string()),
            );
            self.capabilities.insert(
                "ti".to_string(),
                TermcapValue::String("\x1b[?1049h".to_string()),
            );
            self.capabilities.insert(
                "te".to_string(),
                TermcapValue::String("\x1b[?1049l".to_string()),
            );
            self.capabilities
                .insert("bl".to_string(), TermcapValue::String("\x07".to_string()));
            self.capabilities
                .insert("cr".to_string(), TermcapValue::String("\r".to_string()));
        }
    }

    /// Get a boolean capability
    pub fn get_flag(&self, name: &str) -> Option<bool> {
        match self.capabilities.get(name)? {
            TermcapValue::Boolean(b) => Some(*b),
            _ => None,
        }
    }

    /// Get a numeric capability
    pub fn get_num(&self, name: &str) -> Option<i32> {
        match self.capabilities.get(name)? {
            TermcapValue::Number(n) => Some(*n),
            _ => None,
        }
    }

    /// Get a string capability
    pub fn get_str(&self, name: &str) -> Option<String> {
        match self.capabilities.get(name)? {
            TermcapValue::String(s) => Some(s.clone()),
            _ => None,
        }
    }

    /// Get any capability
    pub fn get(&self, name: &str) -> Option<&TermcapValue> {
        self.capabilities.get(name)
    }

    /// Is termcap initialized?
    pub fn is_initialized(&self) -> bool {
        self.initialized
    }

    /// Get all boolean capabilities
    pub fn booleans(&self) -> HashMap<String, bool> {
        self.capabilities
            .iter()
            .filter_map(|(k, v)| {
                if let TermcapValue::Boolean(b) = v {
                    Some((k.clone(), *b))
                } else {
                    None
                }
            })
            .collect()
    }

    /// Get all numeric capabilities
    pub fn numbers(&self) -> HashMap<String, i32> {
        self.capabilities
            .iter()
            .filter_map(|(k, v)| {
                if let TermcapValue::Number(n) = v {
                    Some((k.clone(), *n))
                } else {
                    None
                }
            })
            .collect()
    }

    /// Get all string capabilities
    pub fn strings(&self) -> HashMap<String, String> {
        self.capabilities
            .iter()
            .filter_map(|(k, v)| {
                if let TermcapValue::String(s) = v {
                    Some((k.clone(), s.clone()))
                } else {
                    None
                }
            })
            .collect()
    }
}

/// Apply tgoto-style parameter substitution
pub fn tgoto(cap: &str, col: i32, row: i32) -> String {
    let mut result = String::new();
    let mut chars = cap.chars().peekable();
    let mut use_row = true;

    while let Some(c) = chars.next() {
        if c == '%' {
            if let Some(&next) = chars.peek() {
                chars.next();
                match next {
                    'd' => {
                        let val = if use_row { row } else { col };
                        result.push_str(&val.to_string());
                        use_row = false;
                    }
                    '2' => {
                        let val = if use_row { row } else { col };
                        result.push_str(&format!("{:02}", val));
                        use_row = false;
                    }
                    '3' => {
                        let val = if use_row { row } else { col };
                        result.push_str(&format!("{:03}", val));
                        use_row = false;
                    }
                    '.' => {
                        let val = if use_row { row } else { col };
                        result.push((val as u8) as char);
                        use_row = false;
                    }
                    '+' => {
                        if let Some(offset) = chars.next() {
                            let val = if use_row { row } else { col };
                            result.push(((val + offset as i32) as u8) as char);
                            use_row = false;
                        }
                    }
                    'i' => {}
                    '%' => {
                        result.push('%');
                    }
                    _ => {
                        result.push('%');
                        result.push(next);
                    }
                }
            }
        } else {
            result.push(c);
        }
    }

    result
}

/// Execute echotc builtin
pub fn builtin_echotc(args: &[&str], tc: &Termcap) -> (i32, String) {
    if args.is_empty() {
        return (1, "echotc: capability name required\n".to_string());
    }

    if !tc.is_initialized() {
        return (1, "echotc: terminal not initialized\n".to_string());
    }

    let cap_name = args[0];

    if let Some(n) = tc.get_num(cap_name) {
        return (0, format!("{}\n", n));
    }

    if let Some(b) = tc.get_flag(cap_name) {
        return (0, format!("{}\n", if b { "yes" } else { "no" }));
    }

    if let Some(s) = tc.get_str(cap_name) {
        if args.len() == 1 {
            return (0, s);
        }

        let mut required_args = 0;
        for c in s.chars() {
            if c == '%' {
                required_args += 1;
            }
        }
        required_args /= 2;

        if args.len() - 1 != required_args {
            if args.len() - 1 < required_args {
                return (1, "echotc: not enough arguments\n".to_string());
            } else {
                return (1, "echotc: too many arguments\n".to_string());
            }
        }

        if required_args >= 2 {
            let row: i32 = args[1].parse().unwrap_or(0);
            let col: i32 = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(row);
            return (0, tgoto(&s, col, row));
        }

        return (0, s);
    }

    (1, format!("echotc: no such capability: {}\n", cap_name))
}

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

    #[test]
    fn test_termcap_new() {
        let tc = Termcap::new();
        assert!(!tc.is_initialized());
    }

    #[test]
    fn test_termcap_init() {
        let mut tc = Termcap::new();
        let result = tc.init(Some("xterm-256color"));
        assert!(result);
        assert!(tc.is_initialized());
    }

    #[test]
    fn test_termcap_get_num() {
        let mut tc = Termcap::new();
        tc.init(Some("xterm"));

        assert!(tc.get_num("co").is_some());
        assert!(tc.get_num("li").is_some());
    }

    #[test]
    fn test_termcap_get_flag() {
        let mut tc = Termcap::new();
        tc.init(Some("xterm"));

        assert_eq!(tc.get_flag("am"), Some(true));
    }

    #[test]
    fn test_termcap_get_str() {
        let mut tc = Termcap::new();
        tc.init(Some("xterm"));

        assert!(tc.get_str("cl").is_some());
        assert!(tc.get_str("cm").is_some());
    }

    #[test]
    fn test_tgoto() {
        let result = tgoto("\x1b[%d;%dH", 10, 5);
        assert!(result.contains("5") && result.contains("10"));
    }

    #[test]
    fn test_builtin_echotc_no_args() {
        let tc = Termcap::new();
        let (status, _) = builtin_echotc(&[], &tc);
        assert_eq!(status, 1);
    }

    #[test]
    fn test_builtin_echotc_not_initialized() {
        let tc = Termcap::new();
        let (status, output) = builtin_echotc(&["co"], &tc);
        assert_eq!(status, 1);
        assert!(output.contains("not initialized"));
    }

    #[test]
    fn test_builtin_echotc_numeric() {
        let mut tc = Termcap::new();
        tc.init(Some("xterm"));
        let (status, output) = builtin_echotc(&["co"], &tc);
        assert_eq!(status, 0);
        assert!(output.contains("80") || output.parse::<i32>().is_ok());
    }

    #[test]
    fn test_builtin_echotc_boolean() {
        let mut tc = Termcap::new();
        tc.init(Some("xterm"));
        let (status, output) = builtin_echotc(&["am"], &tc);
        assert_eq!(status, 0);
        assert!(output.contains("yes") || output.contains("no"));
    }

    #[test]
    fn test_bool_codes() {
        assert!(BOOL_CODES.contains(&"am"));
        assert!(BOOL_CODES.contains(&"bw"));
    }

    #[test]
    fn test_num_codes() {
        assert!(NUM_CODES.contains(&"co"));
        assert!(NUM_CODES.contains(&"li"));
    }

    #[test]
    fn test_str_codes() {
        assert!(STR_CODES.contains(&"cl"));
        assert!(STR_CODES.contains(&"cm"));
    }
}