1 //! Parse input from stdin and log actions on stdout
2 use std::io::{self, Read};
3 
4 use vte::{Params, Parser, Perform};
5 
6 /// A type implementing Perform that just logs actions
7 struct Log;
8 
9 impl Perform for Log {
print(&mut self, c: char)10     fn print(&mut self, c: char) {
11         println!("[print] {:?}", c);
12     }
13 
execute(&mut self, byte: u8)14     fn execute(&mut self, byte: u8) {
15         println!("[execute] {:02x}", byte);
16     }
17 
hook(&mut self, params: &Params, intermediates: &[u8], ignore: bool, c: char)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 
put(&mut self, byte: u8)25     fn put(&mut self, byte: u8) {
26         println!("[put] {:02x}", byte);
27     }
28 
unhook(&mut self)29     fn unhook(&mut self) {
30         println!("[unhook]");
31     }
32 
osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool)33     fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) {
34         println!("[osc_dispatch] params={:?} bell_terminated={}", params, bell_terminated);
35     }
36 
csi_dispatch(&mut self, params: &Params, intermediates: &[u8], ignore: bool, c: char)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 
esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8)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 
main()52 fn 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) => {
65                 for byte in &buf[..n] {
66                     statemachine.advance(&mut performer, *byte);
67                 }
68             },
69             Err(err) => {
70                 println!("err: {}", err);
71                 break;
72             },
73         }
74     }
75 }
76