فهرست منبع

Add support for configuration file

Thomas Dy 9 سال پیش
والد
کامیت
4c05dda74d
5فایلهای تغییر یافته به همراه41 افزوده شده و 9 حذف شده
  1. 1 0
      Cargo.toml
  2. 15 0
      src/config.rs
  3. 13 6
      src/main.rs
  4. 10 2
      src/netspeed.rs
  5. 2 1
      src/sensors.rs

+ 1 - 0
Cargo.toml

@@ -5,3 +5,4 @@ authors = ["Thomas Dy <thatsmydoing@gmail.com>"]
 
 [dependencies]
 time = "0.1"
+toml = "0.1"

+ 15 - 0
src/config.rs

@@ -0,0 +1,15 @@
+extern crate toml;
+
+use std::fs::File;
+use std::io::prelude::*;
+
+pub type Config = toml::Value;
+
+pub fn load(path: &str) -> Config {
+    let mut text = String::new();
+    let mut f = File::open(path).unwrap();
+    f.read_to_string(&mut text).ok().expect("Failed to load config");
+    let value = toml::Parser::new(&text).parse();
+    let value = value.unwrap_or(toml::Table::new());
+    toml::Value::Table(value)
+}

+ 13 - 6
src/main.rs

@@ -1,18 +1,24 @@
 mod sensors;
 mod netspeed;
 mod comm;
+mod config;
 
 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;
 
 fn main() {
+    let cfg = config::load("./panel.toml");
+    let cfg = Arc::new(cfg);
+
     let (tx, rx) = mpsc::channel::<Message>();
-    make_thread(&tx, stdin);
-    make_thread(&tx, sensors::sensors);
-    make_thread(&tx, netspeed::netspeed);
+    make_thread(&tx, &cfg, stdin);
+    make_thread(&tx, &cfg, sensors::sensors);
+    make_thread(&tx, &cfg, netspeed::netspeed);
 
     let mut data = HashMap::new();
 
@@ -29,15 +35,16 @@ fn main() {
     }
 }
 
-fn make_thread(tx: &Channel, func: fn(&Channel) -> ()) {
+fn make_thread(tx: &Channel, cfg: &Arc<Config>, func: fn(&Channel, &Config) -> ()) {
     let thread_tx = tx.clone();
+    let thread_cfg = cfg.clone();
     thread::spawn(move || {
-        func(&thread_tx);
+        func(&thread_tx, &thread_cfg);
     });
 }
 
 
-fn stdin(tx: &Channel) {
+fn stdin(tx: &Channel, _cfg: &Config) {
     let mut line = String::new();
     loop {
         io::stdin().read_line(&mut line).ok().expect("Failed to read line");

+ 10 - 2
src/netspeed.rs

@@ -1,5 +1,6 @@
 use comm;
 use comm::Channel;
+use config::Config;
 use std::fs::File;
 use std::io::prelude::*;
 use std::io;
@@ -15,8 +16,8 @@ struct Stats {
     tx: u32
 }
 
-pub fn netspeed(tx: &Channel) {
-    let devices = [ "wlp2s0b1" ];
+pub fn netspeed(tx: &Channel, config: &Config) {
+    let devices = parse_config(config);
     let mut files : Vec<StatFiles> = devices.iter()
         .flat_map(|dev| open_stats(&dev).ok())
         .collect();
@@ -44,6 +45,13 @@ pub fn netspeed(tx: &Channel) {
     }
 }
 
+fn parse_config(cfg: &Config) -> Vec<&str> {
+    let val = cfg.lookup("netspeed.devices").unwrap();
+    let arr = val.as_slice().unwrap();
+    let arr: Vec<&str> = arr.iter().flat_map(|elem| elem.as_str()).collect();
+    arr
+}
+
 fn format_bytes(bytes: u32) -> String {
     let kib = bytes >> 10;
     if kib > 1024 {

+ 2 - 1
src/sensors.rs

@@ -2,11 +2,12 @@ extern crate time;
 
 use comm;
 use comm::Channel;
+use config::Config;
 use std::fs::File;
 use std::io::prelude::*;
 use std::thread;
 
-pub fn sensors(tx: &Channel) {
+pub fn sensors(tx: &Channel, _config: &Config) {
     loop {
         let right = vec![
             temp(),