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