|  | @@ -1,6 +1,8 @@
 | 
	
		
			
				|  |  |  use config::Config;
 | 
	
		
			
				|  |  | -use std::io::Write;
 | 
	
		
			
				|  |  | -use std::process::{ChildStdin, Command, Stdio};
 | 
	
		
			
				|  |  | +use std::io::prelude::*;
 | 
	
		
			
				|  |  | +use std::io::BufReader;
 | 
	
		
			
				|  |  | +use std::process::{ChildStdin, ChildStdout, Command, Stdio};
 | 
	
		
			
				|  |  | +use std::thread;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  pub struct Bar {
 | 
	
		
			
				|  |  |      stdin: ChildStdin
 | 
	
	
		
			
				|  | @@ -14,6 +16,7 @@ impl Bar {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          let mut bar = Command::new("lemonbar");
 | 
	
		
			
				|  |  |          bar.stdin(Stdio::piped());
 | 
	
		
			
				|  |  | +        bar.stdout(Stdio::piped());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          if !top {
 | 
	
		
			
				|  |  |              bar.arg("-b");
 | 
	
	
		
			
				|  | @@ -28,9 +31,36 @@ impl Bar {
 | 
	
		
			
				|  |  |              .ok()
 | 
	
		
			
				|  |  |              .expect("Failed to start lemonbar");
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        let stdout = child.stdout.unwrap();
 | 
	
		
			
				|  |  | +        Bar::read_loop(stdout);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          Bar { stdin: child.stdin.unwrap() }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    fn read_loop(stdout: ChildStdout) {
 | 
	
		
			
				|  |  | +        thread::spawn(move || {
 | 
	
		
			
				|  |  | +            let mut s = String::new();
 | 
	
		
			
				|  |  | +            let mut reader = BufReader::new(stdout);
 | 
	
		
			
				|  |  | +            loop {
 | 
	
		
			
				|  |  | +                s.clear();
 | 
	
		
			
				|  |  | +                reader.read_line(&mut s).ok().expect("Failed to read from lemonbar");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                let mut chars = s.trim().chars();
 | 
	
		
			
				|  |  | +                let kind = chars.next().unwrap();
 | 
	
		
			
				|  |  | +                let name = chars.collect::<String>();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                if kind == 'w' {
 | 
	
		
			
				|  |  | +                    Command::new("bspc")
 | 
	
		
			
				|  |  | +                        .arg("desktop")
 | 
	
		
			
				|  |  | +                        .arg("-f")
 | 
	
		
			
				|  |  | +                        .arg(&name)
 | 
	
		
			
				|  |  | +                        .output()
 | 
	
		
			
				|  |  | +                        .ok();
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      pub fn send(&mut self, text: &str) {
 | 
	
		
			
				|  |  |          writeln!(&mut self.stdin, "{}", text).ok();
 | 
	
		
			
				|  |  |      }
 |