crabterm 0.1.0

A terminal (UART) server and client
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
use log::info;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

use super::action::Action;
use super::key::{Key, KeyEvent, Modifiers};

#[derive(Debug, Clone, PartialEq)]
pub enum SettingValue {
    Bool(bool),
    String(String),
}

impl SettingValue {
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            SettingValue::Bool(b) => Some(*b),
            _ => None,
        }
    }

    pub fn as_str(&self) -> Option<&str> {
        match self {
            SettingValue::String(s) => Some(s),
            _ => None,
        }
    }
}

#[derive(Debug, Clone)]
pub struct KeybindConfig {
    pub prefix: Option<KeyEvent>,
    pub prefix_bindings: HashMap<KeyEvent, Action>,
    pub direct_bindings: HashMap<KeyEvent, Action>,
    pub settings: HashMap<String, SettingValue>,
}

impl Default for KeybindConfig {
    fn default() -> Self {
        let mut config = KeybindConfig {
            prefix: Some(KeyEvent::ctrl_char('a')),
            prefix_bindings: HashMap::new(),
            direct_bindings: HashMap::new(),
            settings: HashMap::new(),
        };

        // Default bindings
        config
            .direct_bindings
            .insert(KeyEvent::ctrl_char('q'), Action::Quit);
        config
            .prefix_bindings
            .insert(KeyEvent::char('q'), Action::Send(vec![0x11])); // Send Ctrl+Q
        config
            .prefix_bindings
            .insert(KeyEvent::ctrl_char('a'), Action::Send(vec![0x01])); // Send literal Ctrl+A
        config
            .prefix_bindings
            .insert(KeyEvent::char('t'), Action::FilterToggle("timestamp".to_string()));

        config
    }
}

impl KeybindConfig {
    pub fn new() -> Self {
        Self {
            prefix: None,
            prefix_bindings: HashMap::new(),
            direct_bindings: HashMap::new(),
            settings: HashMap::new(),
        }
    }

    pub fn load(path: Option<PathBuf>) -> Self {
        let mut config_path = dirs::home_dir().map(|home| home.join(".crabterm"));

        if path.is_some() {
            config_path = path;
        }

        if let Some(p) = config_path
            && p.exists()
        {
            match KeybindConfig::load_from_file(&p) {
                Ok(config) => {
                    info!("Loaded keybind config from {:?}", p);
                    config
                }
                Err(e) => {
                    println!("Warning: Failed to parse {}: {}", p.display(), e);
                    KeybindConfig::default()
                }
            }
        } else {
            KeybindConfig::default()
        }
    }

    pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self, String> {
        let content = fs::read_to_string(path).map_err(|e| e.to_string())?;
        Self::parse(&content)
    }

    pub fn parse(content: &str) -> Result<Self, String> {
        let mut config = KeybindConfig::new();

        for (line_num, line) in content.lines().enumerate() {
            let line = line.trim();

            // Skip empty lines and comments
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            config
                .parse_line(line)
                .map_err(|e| format!("Line {}: {}", line_num + 1, e))?;
        }

        Ok(config)
    }

    fn parse_line(&mut self, line: &str) -> Result<(), String> {
        let mut parts = LineParser::new(line);

        let directive = parts.next_word().ok_or("Empty directive")?;

        match directive {
            "prefix" => {
                let key_str = parts.next_word().ok_or("Missing key for prefix")?;
                self.prefix = Some(parse_key_event(key_str)?);
            }
            "map-prefix" => {
                let key_str = parts.next_word().ok_or("Missing key for map-prefix")?;
                let key = parse_key_event(key_str)?;
                let action = parse_action(&mut parts)?;
                self.prefix_bindings.insert(key, action);
            }
            "map" => {
                let key_str = parts.next_word().ok_or("Missing key for map")?;
                let key = parse_key_event(key_str)?;
                let action = parse_action(&mut parts)?;
                self.direct_bindings.insert(key, action);
            }
            "set" => {
                let name = parts.next_word().ok_or("Missing setting name")?;
                let value_str = parts.next_word().ok_or("Missing setting value")?;
                let value = match value_str.to_lowercase().as_str() {
                    "on" | "true" | "yes" | "1" => SettingValue::Bool(true),
                    "off" | "false" | "no" | "0" => SettingValue::Bool(false),
                    _ => SettingValue::String(value_str.to_string()),
                };
                self.settings.insert(name.to_string(), value);
            }
            _ => return Err(format!("Unknown directive: {}", directive)),
        }

        Ok(())
    }
}

struct LineParser<'a> {
    remaining: &'a str,
}

impl<'a> LineParser<'a> {
    fn new(line: &'a str) -> Self {
        Self { remaining: line }
    }

    fn next_word(&mut self) -> Option<&'a str> {
        self.remaining = self.remaining.trim_start();
        if self.remaining.is_empty() {
            return None;
        }

        let end = self
            .remaining
            .find(char::is_whitespace)
            .unwrap_or(self.remaining.len());
        let word = &self.remaining[..end];
        self.remaining = &self.remaining[end..];
        Some(word)
    }

    fn next_quoted_string(&mut self) -> Option<String> {
        self.remaining = self.remaining.trim_start();
        if !self.remaining.starts_with('"') {
            return None;
        }

        self.remaining = &self.remaining[1..]; // Skip opening quote

        let mut result = String::new();
        let mut chars = self.remaining.chars().peekable();
        let mut consumed = 0;

        while let Some(c) = chars.next() {
            consumed += c.len_utf8();
            if c == '"' {
                self.remaining = &self.remaining[consumed..];
                return Some(result);
            } else if c == '\\' {
                if let Some(&next) = chars.peek() {
                    consumed += next.len_utf8();
                    chars.next();
                    match next {
                        'n' => result.push('\n'),
                        'r' => result.push('\r'),
                        't' => result.push('\t'),
                        '\\' => result.push('\\'),
                        '"' => result.push('"'),
                        'x' => {
                            // Parse \xHH
                            let mut hex = String::new();
                            for _ in 0..2 {
                                if let Some(&h) = chars.peek()
                                    && h.is_ascii_hexdigit()
                                {
                                    hex.push(h);
                                    consumed += h.len_utf8();
                                    chars.next();
                                }
                            }
                            if hex.len() == 2
                                && let Ok(byte) = u8::from_str_radix(&hex, 16)
                            {
                                result.push(byte as char);
                            }
                        }
                        _ => {
                            result.push('\\');
                            result.push(next);
                        }
                    }
                }
            } else {
                result.push(c);
            }
        }

        None // Unterminated string
    }

    fn rest(&self) -> &'a str {
        self.remaining.trim()
    }
}

fn parse_key_event(s: &str) -> Result<KeyEvent, String> {
    let mut modifiers = Modifiers::none();
    let parts: Vec<&str> = s.split('+').collect();

    if parts.is_empty() {
        return Err("Empty key specification".to_string());
    }

    // Parse modifiers (all but the last part)
    for part in &parts[..parts.len() - 1] {
        match part.to_lowercase().as_str() {
            "ctrl" | "control" | "c" => modifiers.ctrl = true,
            "alt" | "meta" | "m" => modifiers.alt = true,
            "shift" | "s" => modifiers.shift = true,
            _ => return Err(format!("Unknown modifier: {}", part)),
        }
    }

    // Parse the key (last part)
    let key_str = parts.last().unwrap();
    let key = parse_key(key_str)?;

    Ok(KeyEvent::new(key, modifiers))
}

fn parse_key(s: &str) -> Result<Key, String> {
    let lower = s.to_lowercase();

    // Function keys
    if lower.starts_with('f')
        && lower.len() > 1
        && let Ok(n) = lower[1..].parse::<u8>()
        && (1..=12).contains(&n)
    {
        return Ok(Key::F(n));
    }

    // Named keys
    match lower.as_str() {
        "escape" | "esc" => return Ok(Key::Escape),
        "enter" | "return" | "cr" => return Ok(Key::Enter),
        "tab" => return Ok(Key::Tab),
        "backspace" | "bs" => return Ok(Key::Backspace),
        "up" => return Ok(Key::Up),
        "down" => return Ok(Key::Down),
        "left" => return Ok(Key::Left),
        "right" => return Ok(Key::Right),
        "home" => return Ok(Key::Home),
        "end" => return Ok(Key::End),
        "pageup" | "pgup" => return Ok(Key::PageUp),
        "pagedown" | "pgdn" => return Ok(Key::PageDown),
        "insert" | "ins" => return Ok(Key::Insert),
        "delete" | "del" => return Ok(Key::Delete),
        "space" => return Ok(Key::Char(' ')),
        _ => {}
    }

    // Single character
    let chars: Vec<char> = s.chars().collect();
    if chars.len() == 1 {
        return Ok(Key::Char(chars[0].to_ascii_lowercase()));
    }

    Err(format!("Unknown key: {}", s))
}

fn parse_action(parts: &mut LineParser) -> Result<Action, String> {
    let action_name = parts.next_word().ok_or("Missing action")?;

    match action_name {
        "quit" => Ok(Action::Quit),
        "filter-toggle" => {
            let filter_name = parts.next_word().ok_or("filter-toggle requires a filter name")?;
            Ok(Action::FilterToggle(filter_name.to_string()))
        }
        "send" => {
            let string = parts
                .next_quoted_string()
                .ok_or("send requires a quoted string")?;
            Ok(Action::Send(string.into_bytes()))
        }
        "send-bytes" => {
            let mut bytes = Vec::new();
            let rest = parts.rest();
            for part in rest.split_whitespace() {
                let byte = if part.starts_with("0x") || part.starts_with("0X") {
                    u8::from_str_radix(&part[2..], 16)
                        .map_err(|_| format!("Invalid hex byte: {}", part))?
                } else {
                    part.parse::<u8>()
                        .map_err(|_| format!("Invalid byte: {}", part))?
                };
                bytes.push(byte);
            }
            if bytes.is_empty() {
                return Err("send-bytes requires at least one byte".to_string());
            }
            Ok(Action::Send(bytes))
        }
        _ => Err(format!("Unknown action: {}", action_name)),
    }
}

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

    #[test]
    fn test_parse_simple_config() {
        let config = KeybindConfig::parse(
            r#"
            # This is a comment
            prefix Ctrl+a
            map-prefix q quit
            map Ctrl+q quit
        "#,
        )
        .unwrap();

        assert_eq!(config.prefix, Some(KeyEvent::ctrl_char('a')));
        assert_eq!(
            config.prefix_bindings.get(&KeyEvent::char('q')),
            Some(&Action::Quit)
        );
        assert_eq!(
            config.direct_bindings.get(&KeyEvent::ctrl_char('q')),
            Some(&Action::Quit)
        );
    }

    #[test]
    fn test_parse_send_action() {
        let config = KeybindConfig::parse(
            r#"
            map-prefix s send "hello\r\n"
        "#,
        )
        .unwrap();

        assert_eq!(
            config.prefix_bindings.get(&KeyEvent::char('s')),
            Some(&Action::Send(b"hello\r\n".to_vec()))
        );
    }

    #[test]
    fn test_parse_send_bytes() {
        let config = KeybindConfig::parse(
            r#"
            map-prefix e send-bytes 0x1b 0x4f
        "#,
        )
        .unwrap();

        assert_eq!(
            config.prefix_bindings.get(&KeyEvent::char('e')),
            Some(&Action::Send(vec![0x1b, 0x4f]))
        );
    }

    #[test]
    fn test_parse_key_with_modifiers() {
        let key = parse_key_event("Ctrl+Shift+a").unwrap();
        assert!(key.modifiers.ctrl);
        assert!(key.modifiers.shift);
        assert!(!key.modifiers.alt);
        assert_eq!(key.key, Key::Char('a'));
    }

    #[test]
    fn test_parse_function_key() {
        let key = parse_key_event("Alt+F1").unwrap();
        assert!(key.modifiers.alt);
        assert_eq!(key.key, Key::F(1));
    }
}