aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMustafa Salih <37256744+MustafaSalih1993@users.noreply.github.com>2020-12-22 07:40:05 +0300
committerGitHub <noreply@github.com>2020-12-22 07:40:05 +0300
commitf6affec55f1c20366e11accf76b99caf4528753f (patch)
treec55292749bfefcc6ac8643e2933949bf72973f63 /src
parent7075f7cf2889076df5f932b17489eee01c6bcccd (diff)
parentd34ef0dd0392674d1a7a8f098b9c57945e96683a (diff)
Merge pull request #3 from MustafaSalih1993/dev
Added rsblocks.yml file and yaml parser to customize
Diffstat (limited to 'src')
-rw-r--r--src/date.rs20
-rw-r--r--src/disk.rs8
-rw-r--r--src/load_config.rs100
-rw-r--r--src/main.rs9
-rw-r--r--src/mem.rs17
5 files changed, 137 insertions, 17 deletions
diff --git a/src/date.rs b/src/date.rs
index e5d4ebd..3254c11 100644
--- a/src/date.rs
+++ b/src/date.rs
@@ -1,11 +1,13 @@
-use std::process::Command;
+use crate::load_config::Settings;
+use chrono::prelude::*;
-pub fn fmt_date(ft: &str) -> String {
- let cmd = format!("date +\"{}\"", ft);
- let cmd = Command::new("sh").arg("-c").args(&[cmd]).output().unwrap();
- let result = String::from_utf8_lossy(&cmd.stdout)
- .to_string()
- .trim()
- .to_string();
- format!("  {} │", result)
+pub fn fmt_date(setting: &Settings) -> String {
+ let now = Local::now();
+
+ format!(
+ " {} {} {}",
+ setting.time.icon,
+ now.format(&setting.time.format),
+ setting.seperator
+ )
}
diff --git a/src/disk.rs b/src/disk.rs
index 72dd5bf..3c5db62 100644
--- a/src/disk.rs
+++ b/src/disk.rs
@@ -1,6 +1,7 @@
+use crate::load_config::Settings;
use std::process::Command;
-pub fn disk_free() -> String {
+pub fn disk_free(setting: &Settings) -> String {
let cmd = Command::new("sh")
.arg("-c")
.args(&["df -h"])
@@ -15,5 +16,8 @@ pub fn disk_free() -> String {
break;
}
}
- format!("  {} │", disk_used)
+ format!(
+ " {} {} {}",
+ setting.disk.icon, disk_used, setting.seperator
+ )
}
diff --git a/src/load_config.rs b/src/load_config.rs
new file mode 100644
index 0000000..46a630e
--- /dev/null
+++ b/src/load_config.rs
@@ -0,0 +1,100 @@
+use std::env;
+use std::{fs::File, io::Error, io::Read};
+use yaml_rust::{yaml, YamlLoader};
+
+pub struct Time {
+ pub format: String,
+ pub icon: String,
+}
+
+pub struct Memory {
+ pub icon: String,
+}
+
+pub struct Disk {
+ pub icon: String,
+}
+
+pub struct Settings {
+ pub seperator: String,
+ pub time: Time,
+ pub memory: Memory,
+ pub disk: Disk,
+}
+
+pub fn load() -> Result<Settings, Error> {
+ let yml_source = env::var("HOME").unwrap() + "/.config/rsblocks/rsblocks.yml";
+ let mut data = String::new();
+ let mut file = match File::open(yml_source) {
+ Ok(file) => file,
+ Err(_) => {
+ println!("~/.config/rsblocks/rsblocks.yml file not found, loading defaults!");
+ return Ok(load_defaults());
+ }
+ };
+ file.read_to_string(&mut data)?;
+
+ let yml_doc = &YamlLoader::load_from_str(&data).unwrap()[0];
+ let settings = gen_settings(yml_doc);
+ Ok(settings)
+}
+
+fn gen_settings(doc: &yaml::Yaml) -> Settings {
+ let seperator: String;
+ let time_format: String;
+ let time_icon: String;
+ let mem_icon: String;
+ let disk_icon: String;
+
+ if doc["general"]["seperator"].is_badvalue() {
+ seperator = String::from("|");
+ } else {
+ seperator = String::from(doc["general"]["seperator"].as_str().unwrap());
+ }
+ if doc["time"]["icon"].is_badvalue() {
+ time_icon = String::from("")
+ } else {
+ time_icon = String::from(doc["time"]["icon"].as_str().unwrap());
+ }
+ if doc["time"]["format"].is_badvalue() {
+ time_format = String::from("%T")
+ } else {
+ time_format = String::from(doc["time"]["format"].as_str().unwrap())
+ }
+ if doc["memory"]["icon"].is_badvalue() {
+ mem_icon = String::from("")
+ } else {
+ mem_icon = String::from(doc["memory"]["icon"].as_str().unwrap());
+ }
+ if doc["disk"]["icon"].is_badvalue() {
+ disk_icon = String::from("")
+ } else {
+ disk_icon = String::from(doc["disk"]["icon"].as_str().unwrap());
+ }
+
+ Settings {
+ seperator,
+ time: Time {
+ format: time_format,
+ icon: time_icon,
+ },
+ memory: Memory { icon: mem_icon },
+ disk: Disk { icon: disk_icon },
+ }
+}
+
+fn load_defaults() -> Settings {
+ Settings {
+ seperator: String::from("|"),
+ time: Time {
+ format: String::from("%T"),
+ icon: String::from(""),
+ },
+ memory: Memory {
+ icon: String::from(""),
+ },
+ disk: Disk {
+ icon: String::from(""),
+ },
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index e69b5af..23960af 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,15 +4,18 @@ use std::time::Duration;
mod date;
mod disk;
+mod load_config;
mod mem;
fn main() {
+ let settings = load_config::load().unwrap();
+
loop {
let args = format!(
"{}{}{}",
- disk::disk_free(),
- mem::mem().unwrap(),
- date::fmt_date("%d %b, %I:%M:%S %p")
+ disk::disk_free(&settings),
+ mem::mem(&settings).unwrap(),
+ date::fmt_date(&settings)
);
Command::new("xsetroot")
diff --git a/src/mem.rs b/src/mem.rs
index ac0f0ae..8245c29 100644
--- a/src/mem.rs
+++ b/src/mem.rs
@@ -1,7 +1,10 @@
use std::fs::File;
use std::io::Read;
+
+use crate::load_config::Settings;
+
//MemUsed = Memtotal + Shmem - MemFree - Buffers - Cached - SReclaimable
-pub fn mem() -> Result<String, std::io::Error> {
+pub fn mem(setting: &Settings) -> Result<String, std::io::Error> {
let mut buf = String::new();
File::open("/proc/meminfo")?.read_to_string(&mut buf)?;
@@ -48,9 +51,17 @@ pub fn mem() -> Result<String, std::io::Error> {
let mem_used = (mem_total + shmem - mem_free - mem_buffers - mem_cached - mem_srecl) / 1024;
let result: String;
if mem_used > 1000 {
- result = format!(" ▦ {:.1}G │", mem_used as f32 / 1000.0);
+ result = format!(
+ " {} {:.1}G {}",
+ setting.memory.icon,
+ mem_used as f32 / 1000.0,
+ setting.seperator
+ );
} else {
- result = format!(" ▦ {}M │", mem_used);
+ result = format!(
+ " {} {}M {}",
+ setting.memory.icon, mem_used, setting.seperator
+ );
}
Ok(result)
}