parselog/
parselog.rs

1//! Parse input from stdin and log actions on stdout
2use std::io::{self, Read};
3
4use vte_graphics::{Params, Parser, Perform};
5
6/// A type implementing Perform that just logs actions
7struct Log;
8
9impl Perform for Log {
10    fn print(&mut self, c: char) {
11        println!("[print] {:?}", c);
12    }
13
14    fn execute(&mut self, byte: u8) {
15        println!("[execute] {:02x}", byte);
16    }
17
18    fn hook(&mut self, params: &Params, intermediates: &[u8], ignore: bool, c: char) {
19        println!(
20            "[hook] params={:?}, intermediates={:?}, ignore={:?}, char={:?}",
21            params, intermediates, ignore, c
22        );
23    }
24
25    fn put(&mut self, byte: u8) {
26        println!("[put] {:02x}", byte);
27    }
28
29    fn unhook(&mut self) {
30        println!("[unhook]");
31    }
32
33    fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) {
34        println!("[osc_dispatch] params={:?} bell_terminated={}", params, bell_terminated);
35    }
36
37    fn csi_dispatch(&mut self, params: &Params, intermediates: &[u8], ignore: bool, c: char) {
38        println!(
39            "[csi_dispatch] params={:#?}, intermediates={:?}, ignore={:?}, char={:?}",
40            params, intermediates, ignore, c
41        );
42    }
43
44    fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8) {
45        println!(
46            "[esc_dispatch] intermediates={:?}, ignore={:?}, byte={:02x}",
47            intermediates, ignore, byte
48        );
49    }
50}
51
52fn main() {
53    let input = io::stdin();
54    let mut handle = input.lock();
55
56    let mut statemachine = Parser::new();
57    let mut performer = Log;
58
59    let mut buf = [0; 2048];
60
61    loop {
62        match handle.read(&mut buf) {
63            Ok(0) => break,
64            Ok(n) => statemachine.advance(&mut performer, &buf[..n]),
65            Err(err) => {
66                println!("err: {}", err);
67                break;
68            },
69        }
70    }
71}