Added rsblocks.yml file and yaml parser to customize

This commit is contained in:
mustafa salih 2020-12-22 07:30:27 +03:00
parent 7075f7cf28
commit d34ef0dd03
9 changed files with 274 additions and 20 deletions

104
Cargo.lock generated
View File

@ -1,5 +1,107 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]] [[package]]
name = "rs_blocks" name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "chrono"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"libc",
"num-integer",
"num-traits",
"time",
"winapi",
]
[[package]]
name = "libc"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
[[package]]
name = "linked-hash-map"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
[[package]]
name = "num-integer"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
dependencies = [
"autocfg",
]
[[package]]
name = "rsblocks"
version = "0.1.0" version = "0.1.0"
dependencies = [
"chrono",
"yaml-rust",
]
[[package]]
name = "time"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [
"libc",
"wasi",
"winapi",
]
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "yaml-rust"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39f0c922f1a334134dc2f7a8b67dc5d25f0735263feec974345ff706bcf20b0d"
dependencies = [
"linked-hash-map",
]

View File

@ -1,5 +1,5 @@
[package] [package]
name = "rs_blocks" name = "rsblocks"
version = "0.1.0" version = "0.1.0"
authors = ["mustafa salih <mustafasalih1991@gmail.com>"] authors = ["mustafa salih <mustafasalih1991@gmail.com>"]
edition = "2018" edition = "2018"
@ -7,3 +7,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
chrono = "0.4"
yaml-rust = "0.4"

View File

@ -8,7 +8,8 @@ A minimal fast dwm status bar written in **Rust** 🦀
* Time/Date * Time/Date
* Used Memory * Used Memory
* Used Disk space * Used Disk space
* Minimal, Fast * Easy to configure
* Minimal
## Note ## Note
This tool is still in development stage. This tool is still in development stage.
@ -29,6 +30,20 @@ mv ./target/release/rsblocks /usr/local/bin
you good to go now and can run `rsblocks` from your terminal or put that in your `.xinitrc` you good to go now and can run `rsblocks` from your terminal or put that in your `.xinitrc`
## Configuration
**rsblocks** will try to read the file `~/.config/rsblocks/rsblocks.yml`, if it does not exist, it will load the defaults.
create the directory
```sh
mkdir ~/.config/rsblocks
```
copy the [template](./rsblocks.yml) to the new config directory (assuming you are in the root dir of the repo)
```sh
cp ./rsblocks.yml ~/.config/rsblocks/rsblocks.yml
```
## Contributions ## Contributions
All Contributions are welcome. All Contributions are welcome.

15
rsblocks.yml Normal file
View File

@ -0,0 +1,15 @@
# This is the full configuration template available for rsblocks tool
# the names are clearly defines itself.
general:
seperator: '┃'
time:
icon: ''
format: '%d %b, %I:%M:%S %p'
memory:
icon: '▦'
disk:
icon: ''

View File

@ -1,11 +1,13 @@
use std::process::Command; use crate::load_config::Settings;
use chrono::prelude::*;
pub fn fmt_date(ft: &str) -> String { pub fn fmt_date(setting: &Settings) -> String {
let cmd = format!("date +\"{}\"", ft); let now = Local::now();
let cmd = Command::new("sh").arg("-c").args(&[cmd]).output().unwrap();
let result = String::from_utf8_lossy(&cmd.stdout) format!(
.to_string() " {} {} {}",
.trim() setting.time.icon,
.to_string(); now.format(&setting.time.format),
format!("{}", result) setting.seperator
)
} }

View File

@ -1,6 +1,7 @@
use crate::load_config::Settings;
use std::process::Command; use std::process::Command;
pub fn disk_free() -> String { pub fn disk_free(setting: &Settings) -> String {
let cmd = Command::new("sh") let cmd = Command::new("sh")
.arg("-c") .arg("-c")
.args(&["df -h"]) .args(&["df -h"])
@ -15,5 +16,8 @@ pub fn disk_free() -> String {
break; break;
} }
} }
format!("{}", disk_used) format!(
" {} {} {}",
setting.disk.icon, disk_used, setting.seperator
)
} }

100
src/load_config.rs Normal file
View File

@ -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(""),
},
}
}

View File

@ -4,15 +4,18 @@ use std::time::Duration;
mod date; mod date;
mod disk; mod disk;
mod load_config;
mod mem; mod mem;
fn main() { fn main() {
let settings = load_config::load().unwrap();
loop { loop {
let args = format!( let args = format!(
"{}{}{}", "{}{}{}",
disk::disk_free(), disk::disk_free(&settings),
mem::mem().unwrap(), mem::mem(&settings).unwrap(),
date::fmt_date("%d %b, %I:%M:%S %p") date::fmt_date(&settings)
); );
Command::new("xsetroot") Command::new("xsetroot")

View File

@ -1,7 +1,10 @@
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use crate::load_config::Settings;
//MemUsed = Memtotal + Shmem - MemFree - Buffers - Cached - SReclaimable //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(); let mut buf = String::new();
File::open("/proc/meminfo")?.read_to_string(&mut buf)?; 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 mem_used = (mem_total + shmem - mem_free - mem_buffers - mem_cached - mem_srecl) / 1024;
let result: String; let result: String;
if mem_used > 1000 { 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 { } else {
result = format!("{}M │", mem_used); result = format!(
" {} {}M {}",
setting.memory.icon, mem_used, setting.seperator
);
} }
Ok(result) Ok(result)
} }