aboutsummaryrefslogtreecommitdiffstats
path: root/src/parser.rs
blob: 77c95c32af91246c9eeae8cc7ceae3e303efb484 (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
/// A parser for terminal output which produces an in-memory representation of
/// the terminal contents.
pub struct Parser {
    parser: vte::Parser,
    screen: crate::perform::WrappedScreen,
}

impl Parser {
    /// Creates a new terminal parser of the given size and with the given
    /// amount of scrollback.
    #[must_use]
    pub fn new(rows: u16, cols: u16, scrollback_len: usize) -> Self {
        Self {
            parser: vte::Parser::new(),
            screen: crate::perform::WrappedScreen(crate::Screen::new(
                crate::grid::Size { rows, cols },
                scrollback_len,
            )),
        }
    }

    /// Processes the contents of the given byte string, and updates the
    /// in-memory terminal state.
    pub fn process(&mut self, bytes: &[u8]) {
        for byte in bytes {
            self.parser.advance(&mut self.screen, *byte);
        }
    }

    /// Processes the contents of the given byte string, and updates the
    /// in-memory terminal state. Calls methods on the given `Callbacks`
    /// object when relevant escape sequences are seen.
    pub fn process_cb(
        &mut self,
        bytes: &[u8],
        callbacks: &mut impl crate::callbacks::Callbacks,
    ) {
        let mut state = crate::state::State::new(&mut self.screen, callbacks);
        for byte in bytes {
            self.parser.advance(&mut state, *byte);
        }
    }

    /// Returns a reference to a `Screen` object containing the terminal
    /// state.
    #[must_use]
    pub fn screen(&self) -> &crate::Screen {
        &self.screen.0
    }

    /// Returns a mutable reference to a `Screen` object containing the
    /// terminal state.
    #[must_use]
    pub fn screen_mut(&mut self) -> &mut crate::Screen {
        &mut self.screen.0
    }
}

impl Default for Parser {
    /// Returns a parser with dimensions 80x24 and no scrollback.
    fn default() -> Self {
        Self::new(24, 80, 0)
    }
}

impl std::io::Write for Parser {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.process(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}