소스 검색

Implement external handler for title

stdin was refactored into this
Thomas Dy 9 년 전
부모
커밋
cbfff965a2
2개의 변경된 파일24개의 추가작업 그리고 10개의 파일을 삭제
  1. 20 0
      src/external.rs
  2. 4 10
      src/main.rs

+ 20 - 0
src/external.rs

@@ -0,0 +1,20 @@
+use comm;
+use comm::Channel;
+use config::Config;
+use std::io;
+
+pub fn external(tx: &Channel, _cfg: &Config) {
+    let mut line = String::new();
+    loop {
+        line.clear();
+        io::stdin().read_line(&mut line).ok().expect("Failed to read line");
+        let kind = line.remove(0);
+        let line = line.trim();
+
+        match kind {
+            'T' => comm::send(tx, "title", line),
+            _ => ()
+        }
+    }
+}
+

+ 4 - 10
src/main.rs

@@ -2,11 +2,11 @@ mod sensors;
 mod netspeed;
 mod comm;
 mod config;
+mod external;
 
 use comm::{Channel, Message};
 use config::Config;
 use std::collections::HashMap;
-use std::io;
 use std::sync::Arc;
 use std::sync::mpsc;
 use std::thread;
@@ -16,7 +16,7 @@ fn main() {
     let cfg = Arc::new(cfg);
 
     let (tx, rx) = mpsc::channel::<Message>();
-    make_thread(&tx, &cfg, stdin);
+    make_thread(&tx, &cfg, external::external);
     make_thread(&tx, &cfg, sensors::sensors);
     make_thread(&tx, &cfg, netspeed::netspeed);
 
@@ -28,7 +28,8 @@ fn main() {
         match msg {
             Message { kind, text } => data.insert(kind, text),
         };
-        println!("%{{r}}{} | {}",
+        println!("{}%{{r}}{} | {}",
+            data.get("title").unwrap_or(&empty),
             data.get("netspeed").unwrap_or(&empty),
             data.get("sensors").unwrap_or(&empty)
         );
@@ -44,10 +45,3 @@ fn make_thread(tx: &Channel, cfg: &Arc<Config>, func: fn(&Channel, &Config) -> (
 }
 
 
-fn stdin(tx: &Channel, _cfg: &Config) {
-    let mut line = String::new();
-    loop {
-        io::stdin().read_line(&mut line).ok().expect("Failed to read line");
-    }
-}
-