From 2f36e073a36da27c9703762c5127219d363ae171 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Wed, 6 Apr 2022 20:37:12 +0200 Subject: [PATCH] Finally, we can read click events --- TODO | 3 +++ src/bar.rs | 43 ++++++++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/TODO b/TODO index e69de29..77f9708 100644 --- a/TODO +++ b/TODO @@ -0,0 +1,3 @@ +- Maybe add a launcher bar module +- Investigate cargo workspaces so that swayr and swaybar can be separate crates + sharing a common lib. diff --git a/src/bar.rs b/src/bar.rs index 1723ead..a4f5fef 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -18,7 +18,7 @@ use crate::bar::module::BarModuleFn; use env_logger::Env; use serde_json; -use serde_json::Deserializer; +use std::io; use std::{sync::Arc, thread}; use swaybar_types as sbt; @@ -51,17 +51,38 @@ pub fn start() { } pub fn handle_input(mods: Arc>>) { - // TODO: Read stdin and react to click events. - // let stream = - // Deserializer::from_reader(std::io::stdin()).into_iter::(); - // for click in stream { - // log::debug!("Click received: {:?}", click); - // } + let mut sb = String::new(); + io::stdin() + .read_line(&mut sb) + .expect("Could not read from stdin"); - // let lines = std::io::stdin().lock(); - // for l in lines { - // log::debug!("{}", l); - // } + if "[\n" != sb { + log::error!("Expected [\\n but got {}", sb); + log::error!("Sorry, input events won't work is this session."); + return; + } + + loop { + let mut buf = String::new(); + if let Err(err) = io::stdin().read_line(&mut buf) { + log::error!("Error while reading from stdin: {}", err); + log::error!("Skipping this input line..."); + continue; + } + + let click = match serde_json::from_str::( + buf.strip_prefix(",").unwrap_or(&buf), + ) { + Ok(click) => click, + Err(err) => { + log::error!("Error while parsing str to Click: {}", err); + log::error!("The string was {}", buf); + log::error!("Skipping this input line..."); + continue; + } + }; + log::debug!("Received click: {:?}", click); + } } pub fn generate_status(mods: &[Box], refresh_interval: u64) {