small software-rendered rust tty
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
use ctlfun::Parameter::*;
use ctlfun::TerminalInput::*;
use ctlfun::{ControlFunction, TerminalInputParser};

pub struct Terminal {
    pub style: Style,
    pub cursor: (u16, u16),
    pub size: (u16, u16),
    pub scrollback: Scrollback,
    pub cells: Vec<Cell>,
    pub p: TerminalInputParser,
    pub mode: Mode,
}
pub enum Mode {
    Normal,
    Raw,
}
#[derive(Default)]
pub struct Scrollback {
    // invariant: len() / t.size.w == height
    pub history: Vec<Cell>,
    pub height: u16,
}
impl Scrollback {}

#[derive(Clone, Copy)]
pub struct Style {
    pub bg: [u8; 3],
    pub color: [u8; 3],
    pub style: u8,
}
use crate::colors;
impl std::default::Default for Style {
    fn default() -> Self {
        Self {
            bg: colors::BACKGROUND,
            style: 0,
            color: colors::FOREGROUND,
        }
    }
}

#[derive(Clone, Copy, Default)]
pub struct Cell {
    pub style: Style,
    pub letter: Option<char>,
}

impl Terminal {
    #[implicit_fn::implicit_fn]
    pub fn rx(&mut self, x: u8) {
        match self.p.parse_byte(x) {
            Continue => {}
            Char(x) => {
                dbg!(x);
                self.cursor.0 += 1;
                if self.cursor.0 == self.size.0 {
                    println!("overflow");
                    self.cursor.0 = 1;
                    self.cursor.1 += 1;
                }
                let c = &mut self.cells[(self.cursor.1 * self.size.0
                    + self.cursor.0)
                    as usize];
                c.letter = Some(x);
                c.style = self.style;
            }
            Control(ControlFunction { start: 8, .. }) => {
                self.cursor.0 -= 1;
            }
            Control(ControlFunction {
                start: b'[',
                params,
                end: b'm',
                ..
            }) => match params.get(0).map(*_).unwrap() {
                Value(0) => self.style = Style::default(),
                Value(x @ (30..=37)) => {
                    self.style.color = colors::FOUR[x as usize - 30]
                }
                Value(39) => self.style.color = colors::FOREGROUND,
                Value(x @ (90..=97)) => {
                    self.style.color = colors::FOUR[x as usize - 72]
                }
                _ => {}
            },
            Control(ControlFunction {
                start: b'[',
                params,
                end: b'K',
                ..
            }) if params == &[Default] => {
                for cell in &mut self.cells[(self.cursor.1 * self.size.0
                    + self.cursor.0
                    + 1)
                    as usize
                    ..(self.cursor.1 * self.size.0 + self.size.0) as usize]
                // [self.cursor.1 as usize..self.size.0 as usize]
                // [self.cursor.0 as usize..]
                {
                    cell.letter = None;
                }
            }
            Control(ControlFunction { start: b'\r', .. }) => {
                self.cursor.0 = 1;
            }
            Control(ControlFunction { start: b'\n', .. }) => {
                self.cursor.1 += 1;
            }
            Control(x) => {
                dbg!(x);
                println!(
                    "{} {:?} {} {}",
                    x.start as char,
                    x.params
                        .iter()
                        .map(|x| match x {
                            ctlfun::Parameter::Default =>
                                "default".to_string(),
                            ctlfun::Parameter::Value(x) => x.to_string(),
                        })
                        .collect::<Vec<_>>(),
                    String::from_utf8_lossy(&x.bytes),
                    x.end as char,
                );
            }
            _ => unreachable!(),
        }
    }
}