Merge pull request #12 from MustafaSalih1993/dev

rsblocks now is a multi threaded
This commit is contained in:
Mustafa Salih 2021-01-03 01:53:15 +03:00 committed by GitHub
commit 2de98ee8b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 190 additions and 75 deletions

View File

@ -1,12 +1,13 @@
[package] [package]
name = "rsblocks" name = "rsblocks"
version = "0.1.1" version = "0.1.2"
authors = ["mustafa salih <mustafasalih1991@gmail.com>"] authors = ["mustafa salih <mustafasalih1991@gmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"
license = "MIT" license = "MIT"
keywords = ["linux", "dwm","statusbar"]
repository = "https://github.com/MustafaSalih1993/rsblocks" repository = "https://github.com/MustafaSalih1993/rsblocks"
description = "This is a minimal status bar for dwm window manager for linux" description = "a multi threaded status bar for dwm window manager for linux"
# 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]

View File

@ -1,19 +1,21 @@
# rsblocks # rsblocks
A minimal fast dwm status bar written in **Rust** 🦀 A minimal multi threaded fast status bar for dwm window manager written in **Rust** 🦀
<p> <p>
<img align="center" src="./screenshots/1.png"/> <img align="center" src="./screenshots/1.png"/>
</p><br/> </p><br/>
## Features ## Features
* Multi Threads
* Time/Date * Time/Date
* Used Memory * Used Memory
* Used Disk space * Used Disk space
* Sound volume _reads from `amixer` for now_ * Sound volume _reads from `alsa-utils` for now_
* Easy to configure * Easy to configure with `rsblocks.yml` file
* Minimal
## Note
This tool is still in development stage. ## Notes
* This tool is still in development stage.
* currently supports only linux.
## Cargo Installation ## Cargo Installation
You can install the binary crate directly You can install the binary crate directly
@ -38,7 +40,9 @@ 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 ## Configuration
**rsblocks** will try to read the file `~/.config/rsblocks/rsblocks.yml`, if it does not exist, it will load the defaults. #### Notes:
* **rsblocks** will try to read the file `$HOME/.config/rsblocks/rsblocks.yml`, if it does not exist, it will load the defaults.
* **rsblocks** will read the configuration file **only** on startup, which means you have to kill it and start it again if you updated your `rsblocks.yml` file.
create the directory create the directory
```sh ```sh

View File

@ -1,22 +1,28 @@
# This is the full configuration template available for rsblocks tool # This is the full configuration template available for rsblocks tool
# the names are clearly defines itself. # the names are clearly defines itself.
# NOTE: the (delay) is **seconds** with the float point to update the item in the bar.
general: general:
seperator: '┃' seperator: '┃'
time: time:
icon: '' icon: ''
format: '%d %b, %I:%M:%S %p' format: '%d %b, %I:%M:%S %p'
delay: 1.0
memory: memory:
enable: true enable: true
icon: '▦' icon: '▦'
delay: 2.0
disk: disk:
enable: true enable: true
icon: '' icon: ''
delay: 120.0
# reads from amixer # reads from alsa-utils
volume: volume:
enable: false enable: false
icon: '' icon: ''
delay: 0.18

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 6.0 KiB

View File

@ -4,10 +4,20 @@ use std::fs::File;
use std::io::Error; use std::io::Error;
use std::io::Read; use std::io::Read;
use std::process::Command; use std::process::Command;
use std::sync::mpsc;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use yaml_rust::{yaml, YamlLoader}; use yaml_rust::{yaml, YamlLoader};
#[derive(Debug)]
pub enum ThreadsData {
Sound(String),
Disk(String),
Memory(String),
Time(String),
}
#[derive(Clone)]
pub struct Config { pub struct Config {
pub seperator: String, pub seperator: String,
pub time: Time, pub time: Time,
@ -16,45 +26,78 @@ pub struct Config {
pub volume: Volume, pub volume: Volume,
} }
#[derive(Clone)]
pub struct Time { pub struct Time {
pub format: String, pub format: String,
pub icon: String, pub icon: String,
pub delay: f64,
} }
#[derive(Clone)]
pub struct Memory { pub struct Memory {
pub icon: String, pub icon: String,
pub enabled: bool, pub enabled: bool,
pub delay: f64,
} }
#[derive(Clone)]
pub struct Disk { pub struct Disk {
pub icon: String, pub icon: String,
pub enabled: bool, pub enabled: bool,
pub delay: f64,
} }
#[derive(Clone)]
pub struct Volume { pub struct Volume {
pub icon: String, pub icon: String,
pub enabled: bool, pub enabled: bool,
pub delay: f64,
} }
pub fn load_config() -> Result<Config, Error> { pub fn load_config() -> Result<Config, Error> {
let yml_source = env::var("HOME").unwrap() + "/.config/rsblocks/rsblocks.yml"; let yml_source = env::var("HOME").unwrap() + "/.config/rsblocks/rsblocks.yml";
let mut data = String::new(); let mut data = String::new();
let mut file = match File::open(yml_source) { let mut file = match File::open(&yml_source) {
Ok(file) => file, Ok(file) => file,
Err(_) => { Err(_) => {
println!("~/.config/rsblocks/rsblocks.yml file not found, loading defaults!"); eprintln!("{} file not found, loading defaults!", &yml_source);
return Ok(load_defaults()); return Ok(load_defaults());
} }
}; };
file.read_to_string(&mut data)?; file.read_to_string(&mut data)?;
let yml_content = &YamlLoader::load_from_str(&data).unwrap()[0]; let yml_content = &YamlLoader::load_from_str(&data).unwrap()[0];
let config = gen_default_config(yml_content); let config = parse_config(yml_content);
Ok(config) Ok(config)
} }
fn gen_default_config(doc: &yaml::Yaml) -> Config { fn load_defaults() -> Config {
// setting icons Config {
seperator: String::from("|"),
time: Time {
format: String::from("%T"),
icon: String::from(""),
delay: 1.0,
},
memory: Memory {
icon: String::from(""),
enabled: true,
delay: 2.0,
},
disk: Disk {
icon: String::from(""),
enabled: false,
delay: 60.0,
},
volume: Volume {
icon: String::from(""),
enabled: false,
delay: 0.17,
},
}
}
fn parse_config(doc: &yaml::Yaml) -> Config {
// parsing icons and set default if not exist in the config file
let seperator = get_or_set_string(doc, "general", "seperator", "|"); let seperator = get_or_set_string(doc, "general", "seperator", "|");
let time_icon = get_or_set_string(doc, "time", "icon", ""); let time_icon = get_or_set_string(doc, "time", "icon", "");
let time_format = get_or_set_string(doc, "time", "format", "%T"); let time_format = get_or_set_string(doc, "time", "format", "%T");
@ -62,32 +105,52 @@ fn gen_default_config(doc: &yaml::Yaml) -> Config {
let disk_icon = get_or_set_string(doc, "disk", "icon", ""); let disk_icon = get_or_set_string(doc, "disk", "icon", "");
let volume_icon = get_or_set_string(doc, "volume", "icon", ""); let volume_icon = get_or_set_string(doc, "volume", "icon", "");
// everything false by default // parsing enabled state (everything false by default)
let disk_enabled = get_or_set_bool(doc, "disk", "enable"); let disk_enabled = get_or_set_bool(doc, "disk", "enable");
let memory_enabled = get_or_set_bool(doc, "memory", "enable"); let memory_enabled = get_or_set_bool(doc, "memory", "enable");
let volume_enabled = get_or_set_bool(doc, "volume", "enable"); let volume_enabled = get_or_set_bool(doc, "volume", "enable");
// parsing update_delay state (should be all seconds in f64 type)
let time_delay = get_or_set_f32(doc, "time", "delay", 1.0);
let disk_delay = get_or_set_f32(doc, "disk", "delay", 60.0);
let memory_delay = get_or_set_f32(doc, "memory", "delay", 2.0);
let volume_delay = get_or_set_f32(doc, "volume", "delay", 0.17);
Config { Config {
seperator, seperator,
time: Time { time: Time {
format: time_format, format: time_format,
icon: time_icon, icon: time_icon,
delay: time_delay,
}, },
memory: Memory { memory: Memory {
icon: mem_icon, icon: mem_icon,
enabled: memory_enabled, enabled: memory_enabled,
delay: memory_delay,
}, },
disk: Disk { disk: Disk {
icon: disk_icon, icon: disk_icon,
enabled: disk_enabled, enabled: disk_enabled,
delay: disk_delay,
}, },
volume: Volume { volume: Volume {
icon: volume_icon, icon: volume_icon,
enabled: volume_enabled, enabled: volume_enabled,
delay: volume_delay,
}, },
} }
} }
// getting a f32 value from rsblocks.yml file or set default (last argument)
fn get_or_set_f32(doc: &yaml::Yaml, parent: &str, child: &str, default: f64) -> f64 {
let val: f64 = if doc[parent][child].is_badvalue() {
default
} else {
doc[parent][child].as_f64().unwrap()
};
val
}
// getting a boolean value from rsblocks.yml file or set it false if it does not exist // getting a boolean value from rsblocks.yml file or set it false if it does not exist
fn get_or_set_bool(doc: &yaml::Yaml, parent: &str, child: &str) -> bool { fn get_or_set_bool(doc: &yaml::Yaml, parent: &str, child: &str) -> bool {
let val: bool; let val: bool;
@ -99,7 +162,7 @@ fn get_or_set_bool(doc: &yaml::Yaml, parent: &str, child: &str) -> bool {
val val
} }
// getting a String value from the rsblocks.yml file or set the default in the last parameter // getting a String value from the rsblocks.yml file or set the default(last argument)
fn get_or_set_string(doc: &yaml::Yaml, parent: &str, child: &str, default_val: &str) -> String { fn get_or_set_string(doc: &yaml::Yaml, parent: &str, child: &str, default_val: &str) -> String {
let val: String; let val: String;
if doc[parent][child].is_badvalue() { if doc[parent][child].is_badvalue() {
@ -111,68 +174,109 @@ fn get_or_set_string(doc: &yaml::Yaml, parent: &str, child: &str, default_val: &
val val
} }
fn load_defaults() -> Config { /*######################## LOADING CONFIGS ENDS HERE ############################
Config {
seperator: String::from("|"), */
time: Time {
format: String::from("%T"),
icon: String::from(""),
},
memory: Memory {
icon: String::from(""),
enabled: false,
},
disk: Disk {
icon: String::from(""),
enabled: false,
},
volume: Volume {
icon: String::from(""),
enabled: false,
},
}
}
/* LOADING CONFIGS ENDS HERE */
/* Running the program: /* Running the program:
TODO: this is sucks, repeated code in threads below, fix me you fucking asshole
*/
TODO: this is sucks, i want to update each one in a diffrent delay, maybe i'll try to use threads
*/
pub fn run(config: Config) { pub fn run(config: Config) {
loop { let (tx, rx) = mpsc::channel();
let mut bar = String::from("");
// the order of the IF's below matters to the final format // volume thread
if config.volume.enabled {
let volume_tx = tx.clone();
let configcp = config.clone();
let mut vol_data = ThreadsData::Sound(get_volume(&configcp));
thread::spawn(move || loop {
let _ = volume_tx.send(vol_data);
vol_data = ThreadsData::Sound(get_volume(&configcp));
thread::sleep(Duration::from_secs_f64(configcp.volume.delay))
});
}
if config.volume.enabled { // Disk thread
// volume return String if config.disk.enabled {
bar.push_str(&get_sound(&config)); let disk_tx = tx.clone();
let configcp = config.clone();
let mut disk_data = ThreadsData::Disk(get_disk(&configcp));
thread::spawn(move || loop {
disk_tx.send(disk_data).unwrap();
disk_data = ThreadsData::Disk(get_disk(&configcp));
thread::sleep(Duration::from_secs_f64(configcp.disk.delay))
});
}
// Memory thread
if config.memory.enabled {
let memory_tx = tx.clone();
let configcp = config.clone();
let memory_data = get_memory(&configcp).unwrap();
let mut memory_data = ThreadsData::Memory(memory_data);
thread::spawn(move || loop {
memory_tx.send(memory_data).unwrap();
memory_data = ThreadsData::Memory(get_memory(&configcp).unwrap());
thread::sleep(Duration::from_secs_f64(configcp.memory.delay))
});
}
// Time thread
{
let time_tx = tx;
let configcp = config.clone();
let mut time_data = ThreadsData::Time(get_time(&configcp));
thread::spawn(move || loop {
time_tx.send(time_data).unwrap();
time_data = ThreadsData::Time(get_time(&configcp));
thread::sleep(Duration::from_secs_f64(configcp.time.delay))
});
}
//Main
{
// NOTE: order matters to the final format
let mut bar: Vec<String> = vec!["".to_string(); 4];
//iterating the values recieved from the threads
for data in rx {
match data {
ThreadsData::Sound(x) => bar[0] = x,
ThreadsData::Disk(x) => bar[1] = x,
ThreadsData::Memory(x) => bar[2] = x,
ThreadsData::Time(x) => bar[3] = x,
}
// match ends here
update(&bar);
} }
if config.disk.enabled {
// disk_free return String
bar.push_str(&disk_free(&config));
}
if config.memory.enabled {
// mem return Result
bar.push_str(&mem(&config).unwrap());
}
bar.push_str(&fmt_date(&config));
Command::new("xsetroot")
.arg("-name")
.arg(bar)
.output()
.unwrap();
thread::sleep(Duration::from_millis(75));
} }
} }
// format bar date/time pub fn update(bar: &Vec<String>) {
pub fn fmt_date(config: &Config) -> String { // TODO: FIX ME, this solution sucks
let mut x = String::new();
for i in bar.iter() {
x.push_str(i.as_str());
}
Command::new("xsetroot")
.arg("-name")
.arg(x)
.output()
.unwrap();
}
/*############################ RUNNING THE PROGRAM ENDS HERE ###########################################*/
/*
############################# HELPER FUNCTIONS BELOW ###################################
*/
pub fn get_time(config: &Config) -> String {
let now = Local::now(); let now = Local::now();
format!( format!(
@ -183,7 +287,7 @@ pub fn fmt_date(config: &Config) -> String {
) )
} }
pub fn disk_free(config: &Config) -> String { pub fn get_disk(config: &Config) -> String {
let cmd = Command::new("sh") let cmd = Command::new("sh")
.arg("-c") .arg("-c")
.args(&["df -h"]) .args(&["df -h"])
@ -205,9 +309,9 @@ pub fn disk_free(config: &Config) -> String {
} }
// TODO: what a horrible solution to get the sound, i dont like it // TODO: what a horrible solution to get the sound, i dont like it
// find another way you dumb fuck // find another way
pub fn get_sound(config: &Config) -> String { pub fn get_volume(config: &Config) -> String {
let cmd_content = Command::new("amixer") let cmd_content = Command::new("amixer")
.args(&["-D", "pulse", "get", "Master"]) .args(&["-D", "pulse", "get", "Master"])
.output() .output()
@ -227,7 +331,7 @@ pub fn get_sound(config: &Config) -> String {
format!(" {} {} {}", config.volume.icon, vol, config.seperator) format!(" {} {} {}", config.volume.icon, vol, config.seperator)
} }
pub fn mem(config: &Config) -> Result<String, std::io::Error> { pub fn get_memory(config: &Config) -> 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)?;
@ -288,7 +392,7 @@ pub fn mem(config: &Config) -> Result<String, std::io::Error> {
} }
Ok(result) Ok(result)
} }
// helper function for the get_memory function
fn assign_val(line: &str, assignable: &mut u32) { fn assign_val(line: &str, assignable: &mut u32) {
let parsed: u32 = line.split(':').collect::<Vec<&str>>()[1] let parsed: u32 = line.split(':').collect::<Vec<&str>>()[1]
.trim() .trim()