From 6ce7cd0b4655b4044e53617aba8fbdfdeddb9a57 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 23 Mar 2023 15:15:50 -0600 Subject: [PATCH] if not CONFIG_PATH then more env --- src/config.rs | 22 +++++++++++++++++----- src/gui.rs | 8 ++++---- src/stream.rs | 17 +++++++++++------ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/config.rs b/src/config.rs index 7357973..050610b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -68,21 +68,33 @@ fn build_config_std() -> Config { return Config { streams: Streams{ input: Stream { - format: None, + format: match env::var("INPUT_FORMAT") { + Ok(x) => Some(x), + Err(_) => None, + }, engine: Engine{ name: String::from("gui"), kafka: None, device: None, - udp: None, + udp: Some(UDP{ + host: Some(env::var("INPUT_UDP_HOST").unwrap_or(String::from("localhost"))), + port: env::var("INPUT_UDP_PORT").unwrap_or(String::from("17070")).parse().unwrap(), + }), }, }, output: Stream { - format: Some(String::from("%s")), + format: match env::var("OUTPUT_FORMAT") { + Ok(x) => Some(x), + Err(_) => None, + }, engine: Engine{ - name: String::from("stdout"), + name: env::var("OUTPUT_NAME").unwrap_or(String::from("stdout")), kafka: None, device: None, - udp: None, + udp: Some(UDP{ + host: Some(env::var("OUTPUT_UDP_HOST").unwrap_or(String::from("localhost"))), + port: env::var("OUTPUT_UDP_PORT").unwrap_or(String::from("17070")).parse().unwrap(), + }), }, }, }, diff --git a/src/gui.rs b/src/gui.rs index 485a23c..d95e414 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -7,11 +7,11 @@ use iced::{Alignment, Element, Application, Settings, Subscription, Theme, Comma use crate::stream::OutputStream; -pub fn main(outputStream: Box) -> iced::Result { +pub fn main(output_stream: Box) -> iced::Result { let def: iced::Settings<()> = Settings::default(); let settings = Settings{ flags: Flags { - outputStream: outputStream, + output_stream: output_stream, }, antialiasing: def.antialiasing, default_font: def.default_font, @@ -33,7 +33,7 @@ struct Main { } struct Flags { - outputStream: Box, + output_stream: Box, } struct Inputs { @@ -153,7 +153,7 @@ impl Application for Main { _ => String::from(""), }; if s.len() > 0 { - self.flags.outputStream.put(s.chars().collect()); + self.flags.output_stream.put(s.chars().collect()); } }, _ => {}, diff --git a/src/stream.rs b/src/stream.rs index 3c4cf32..b3f848a 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -4,6 +4,7 @@ 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; @@ -197,18 +198,17 @@ pub fn build_output_stream(cfg: &Stream) -> Box { } pub struct OutputStreamFormatted { - format: Box) -> Vec>, + format: Option, stream: Box, } pub fn build_formatted_output_stream(cfg: &Stream, stream: Box) -> Box { return Box::new(OutputStreamFormatted{ - format: Box::new(|v: Vec| reg.render("x", &json!()).unwrap()), + format: cfg.format.clone(), stream: stream, }); } -// TODO use https://crates.io/crates/handlebars instead impl OutputStream for OutputStreamFormatted { fn put(&mut self, v: Vec) { match self.format.as_ref() { @@ -218,6 +218,13 @@ impl OutputStream for OutputStreamFormatted { } } +fn sprintf(x: &String, v: Vec) -> Vec { + let mut reg = Handlebars::new(); + return reg.render_template(x, &json!({ + "VALUE": v.iter().collect::(), + })).unwrap().chars().collect(); +} + pub fn _build_output_stream(cfg: &Stream) -> Box { if cfg.engine.name.as_str() == "stdout" { return Box::new(OutputStreamStdout{}); @@ -256,18 +263,16 @@ mod test_output { engine.put("teehee".to_string().chars().collect()); } - /* #[test] fn test_output_stream_formatted() { assert_eq!( "hello world".to_string().chars().collect::>(), sprintf( - &String::from("hello %s"), + &String::from("hello {{ VALUE }}"), String::from("world").chars().collect(), ), ); } - */ } pub struct OutputStreamUDP {