diff --git a/Cargo.lock b/Cargo.lock index c1c1c87..59ec1e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,107 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[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" +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", +] diff --git a/Cargo.toml b/Cargo.toml index 2e7e252..191edfe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "rs_blocks" +name = "rsblocks" version = "0.1.0" authors = ["mustafa salih "] edition = "2018" @@ -7,3 +7,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chrono = "0.4" +yaml-rust = "0.4" \ No newline at end of file diff --git a/README.md b/README.md index d8a6a43..9bdc3c7 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ A minimal fast dwm status bar written in **Rust** 🦀 * Time/Date * Used Memory * Used Disk space -* Minimal, Fast +* Easy to configure +* Minimal ## Note 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` +## 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 All Contributions are welcome. diff --git a/rsblocks.yml b/rsblocks.yml new file mode 100644 index 0000000..86ea00e --- /dev/null +++ b/rsblocks.yml @@ -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: '' 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 { + 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 { +pub fn mem(setting: &Settings) -> Result { let mut buf = String::new(); File::open("/proc/meminfo")?.read_to_string(&mut buf)?; @@ -48,9 +51,17 @@ pub fn mem() -> Result { 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) }