aboutsummaryrefslogtreecommitdiffstats
path: root/src/ansi.rs
blob: e04f6d92e6afe68cf12a9f3c743b788d8538d7cd (plain) (blame)
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
use std::io::Read;

use crate::cursor::Direction;

pub enum KeyboardInput {
    Eof,
    Key(u8),
    CtrlA,
    CtrlB,
    CtrlC,
    CtrlE,
    CtrlD,
    CtrlL,
    CtrlR,
    Arrow(Direction),
    CtrlArrow(Direction),
    DeleteLeft,
    DeleteRight,
    CtrlDeleteRight,
    Home,
    End,
}

fn read1() -> Option<u8> {
    let mut buf = [0];
    match std::io::stdin().lock().read_exact(&mut buf) {
        Ok(_) => Some(buf[0]),
        Err(_) => None,
    }
}

fn byte_to_dir(b: u8) -> Option<Direction> {
    use Direction::*;
    match b {
        b'A' => Some(Up),
        b'B' => Some(Down),
        b'C' => Some(Right),
        b'D' => Some(Left),
        _ => None,
    }
}

fn read_escape(debug: bool) -> KeyboardInput {
    use Direction::*;
    use KeyboardInput::*;

    let mut seq = vec![match read1() {
        Some(x) => x,
        None => return Eof,
    }];

    if seq[0] == b'[' {
        // still more
        while {
            let last = seq[seq.len() - 1];
            !(0x40..=0x7E).contains(&last) || seq.len() == 1
        } {
            seq.push(match read1() {
                Some(x) => x,
                None => return Eof,
            });
        }

        if debug {
            println!("escape: {}", seq.escape_ascii());
        }

        match seq[1] {
            b'3' => {
                if seq.len() > 2 && seq[2] == b'~' {
                    DeleteRight
                } else {
                    todo!("unhandled: {}", seq.escape_ascii());
                }
            }
            b'H' => Home,
            b'F' => End,
            b'd' => CtrlDeleteRight,

            // Ctrl Arrow
            b'1' => {
                if seq[1..].starts_with(b"1;5") {
                    if seq.len() == 4 {
                        todo!("idk what this is.");
                    }
                    match seq[4] {
                        b'A' => CtrlArrow(Up),
                        b'B' => CtrlArrow(Down),
                        b'C' => CtrlArrow(Right),
                        b'D' => CtrlArrow(Left),
                        _ => todo!("unhandled {}", seq.escape_ascii()),
                    }
                } else {
                    todo!("unhandled {}", seq[1..].escape_ascii())
                }
            }

            x => {
                if let Some(dir) = byte_to_dir(x) {
                    Arrow(dir)
                } else {
                    todo!("escape characters {}", seq[1..].escape_ascii())
                }
            }
        }
    } else {
        if debug {
            println!("escape: {}", seq.escape_ascii());
        }
        match seq[0] {
            b'd' => CtrlDeleteRight,
            x => todo!("unhandled escape code: ESC {x}"),
        }
    }
}

pub fn read(debug: bool) -> KeyboardInput {
    use KeyboardInput::*;

    let Some(x) = read1() else {
        return KeyboardInput::Eof;
    };

    match x {
        1 => CtrlA,
        2 => CtrlB,
        3 => CtrlC,
        4 => CtrlD,
        8 | 127 => DeleteLeft,
        12 => CtrlL,
        18 => CtrlR,
        27 => read_escape(debug),
        b'\r' => Key(x),
        x if !x.is_ascii_control() => Key(x),
        x => todo!("unimplemented control code: {x}"),
    }
}