master
Bel LaPointe 2023-03-23 15:02:29 -06:00
parent 36380004b5
commit 96ce4d1855
1 changed files with 6 additions and 18 deletions

View File

@ -4,7 +4,6 @@ use hidapi::HidApi;
use rusb::UsbContext;
use gilrs::{Gilrs, Button, Event};
use serde_json::json;
use handlebars::Handlebars;
pub trait InputStream {
fn get(&mut self) -> Vec<char>;
@ -197,34 +196,23 @@ pub fn build_output_stream(cfg: &Stream) -> Box<dyn OutputStream> {
return build_formatted_output_stream(cfg, _build_output_stream(cfg))
}
pub struct OutputStreamFormatted<'a> {
format: Option<Box<&'a dyn Fn(Vec<char>) -> Vec<char>>>,
pub struct OutputStreamFormatted {
format: Box<dyn Fn(Vec<char>) -> Vec<char>>,
stream: Box<dyn OutputStream>,
}
pub fn build_formatted_output_stream(cfg: &Stream, stream: Box<dyn OutputStream>) -> Box<dyn OutputStream> {
return Box::new(OutputStreamFormatted{
format: match cfg.format.clone() {
Some(x) => {
let x = x.clone();
Some(Box::new(
&|v: Vec<char>| {
let mut reg = Handlebars::new();
reg.register_template_string("x", x.clone());
reg.render("x", &json!({"x": v})).unwrap().chars().collect()
},
))
},
None => None,
},
format: Box::new(|v: Vec<char>| reg.render("x", &json!()).unwrap()),
stream: stream,
});
}
impl<'a> OutputStream for OutputStreamFormatted<'a> {
// TODO use https://crates.io/crates/handlebars instead
impl OutputStream for OutputStreamFormatted {
fn put(&mut self, v: Vec<char>) {
match self.format.as_ref() {
Some(x) => self.stream.put(x(v)),
Some(x) => self.stream.put(sprintf(x, v)),
None => self.stream.put(v),
};
}