aboutsummaryrefslogtreecommitdiffstats
path: root/examples/vte.rs
blob: e6d0f554636f9059b6cb167c485bd980cb786a01 (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
use std::io::Read as _;

/// A type implementing Perform that just logs actions
struct Log;

impl vte::Perform for Log {
    fn print(&mut self, c: char) {
        println!("[print] U+{:04x}", c as u32);
    }

    fn execute(&mut self, byte: u8) {
        println!("[execute] {byte:02x}");
    }

    fn hook(
        &mut self,
        params: &vte::Params,
        intermediates: &[u8],
        ignore: bool,
        c: char,
    ) {
        println!(
            "[hook] params={:?}, intermediates={:?}, ignore={:?}, c=U+{:04x}",
            params, intermediates, ignore, c as u32
        );
    }

    fn put(&mut self, byte: u8) {
        println!("[put] {byte:02x}");
    }

    fn unhook(&mut self) {
        println!("[unhook]");
    }

    fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) {
        println!(
            "[osc_dispatch] params={params:?} bell_terminated={bell_terminated}"
        );
    }

    fn csi_dispatch(
        &mut self,
        params: &vte::Params,
        intermediates: &[u8],
        ignore: bool,
        c: char,
    ) {
        println!(
            "[csi_dispatch] \
            params={:#?}, intermediates={:?}, ignore={:?}, c=U+{:04x}",
            params, intermediates, ignore, c as u32
        );
    }

    fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8) {
        println!(
            "[esc_dispatch] intermediates={intermediates:?}, ignore={ignore:?}, byte={byte:02x}"
        );
    }
}

fn main() {
    let mut stdin = std::io::stdin();
    let mut parser = vte::Parser::new();
    let mut performer = Log;

    let mut buf = [0; 4096];
    loop {
        match stdin.read(&mut buf) {
            Ok(0) => break,
            Ok(n) => {
                for byte in &buf[..n] {
                    parser.advance(&mut performer, *byte);
                }
            }
            Err(err) => {
                eprintln!("err: {err}");
                std::process::exit(1);
            }
        }
    }
}