diff options
| -rw-r--r-- | Cargo.lock | 9 | ||||
| -rw-r--r-- | Cargo.toml | 5 | ||||
| -rw-r--r-- | README.md | 16 | ||||
| -rw-r--r-- | rsblocks.yml | 27 | ||||
| -rw-r--r-- | screenshots/1.png | bin | 6093 -> 6878 bytes | |||
| -rw-r--r-- | src/lib.rs | 119 | 
6 files changed, 156 insertions, 20 deletions
@@ -32,6 +32,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"  [[package]] +name = "minreq" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2466d0a7e6bfcd54f69e4a17d4a4318985aaaf7fe3df4cd3b6f11ff551129ca3" + +[[package]]  name = "num-integer"  version = "0.1.44"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -52,9 +58,10 @@ dependencies = [  [[package]]  name = "rsblocks" -version = "0.1.1" +version = "0.1.3"  dependencies = [   "chrono", + "minreq",   "yaml-rust",  ] @@ -1,6 +1,6 @@  [package]  name = "rsblocks" -version = "0.1.2" +version = "0.1.3"  authors = ["mustafa salih <mustafasalih1991@gmail.com>"]  edition = "2018"  readme = "README.md" @@ -12,4 +12,5 @@ description = "a multi threaded status bar for dwm window manager for linux"  [dependencies]  chrono = "0.4" -yaml-rust = "0.4"
\ No newline at end of file +yaml-rust = "0.4" +minreq = "2.2.1"
\ No newline at end of file @@ -1,5 +1,9 @@  # rsblocks -A minimal multi threaded fast status bar for dwm window manager written in **Rust** 🦀 +[<img alt="github" src="https://img.shields.io/static/v1?label=github&message=rsblocks&color=acb0d0&logo=Github&style=flat-square&logoColor=a9b1d6" height="20">](https://github.com/MustafaSalih1993/rsblocks) +[<img alt="crates" src="https://img.shields.io/crates/v/rsblocks?logo=rust&logoColor=a9b1d6&style=flat-square&color=fc8d62" height="20">](https://crates.io/crates/rsblocks) +[<img alt="build" src="https://img.shields.io/github/workflow/status/mustafasalih1993/rsblocks/Rust?color=b9f27c&logoColor=a9b1d6&style=flat-square" height="20">](https://github.com/MustafaSalih1993/rsblocks/actions?query=workflow%3ARust) + +A multi threaded fast status bar for dwm window manager written in **Rust** 🦀  <p>  <img align="center" src="./screenshots/1.png"/>  </p><br/> @@ -7,9 +11,10 @@ A minimal multi threaded fast status bar for dwm window manager written in **Rus  ## Features  * Multi Threads  * Time/Date -* Used Memory -* Used Disk space -* Sound volume _reads from `alsa-utils` for now_ +* Memory Usage +* Disk Usage +* Weather Temperature +* Sound volume _reads from `alsa-utils`_  * Easy to configure with `rsblocks.yml` file @@ -58,5 +63,8 @@ cp ./rsblocks.yml ~/.config/rsblocks/rsblocks.yml  ## Contributions  All Contributions are welcome. +## Credits +* [wttr.in](https://github.com/chubin/wttr.in) for using their weather API +  ## License  [MIT](./LICENSE) diff --git a/rsblocks.yml b/rsblocks.yml index be6939f..76e83f6 100644 --- a/rsblocks.yml +++ b/rsblocks.yml @@ -1,28 +1,47 @@ -# This is the full configuration template available for rsblocks tool -# the names are clearly defines itself. +# This is the full configuration template available for rsblocks. -# NOTE: the (delay) is **seconds** with the float point to update the item in the bar. +# NOTE: the (delay) is in **SECONDS** and the float point "." is required.  general:    seperator: '┃' +    time:    icon: ''    format: '%d %b, %I:%M:%S %p'    delay: 1.0 +    memory:    enable: true    icon: '▦'    delay: 2.0 +    disk:    enable: true    icon: ''    delay: 120.0 -# reads from alsa-utils +   +# reads from alsa, alsa-utils package should +# be installed on the system to make it work.  volume:    enable: false    icon: ''    delay: 0.18 + + +# weather format options is available on +# https://github.com/chubin/wttr.in +# NOTES: +#       - if the city field set to empty, it will try to read location automatically. +#       - if you have multiple formats and want to insert space between them use '+' instead. +#       - giving (city) a value is recommended. +weather: +  enable: true +  icon: '' +  city: ''         +  format: '%l:+%t' +  delay: 7200.0   # 7200 seconds = 2 hours + diff --git a/screenshots/1.png b/screenshots/1.png Binary files differindex 67bff90..495c216 100644 --- a/screenshots/1.png +++ b/screenshots/1.png @@ -1,4 +1,5 @@  use chrono::prelude::*; +use minreq;  use std::env;  use std::fs::File;  use std::io::Error; @@ -15,6 +16,7 @@ pub enum ThreadsData {      Disk(String),      Memory(String),      Time(String), +    Weather(String),  }  #[derive(Clone)] @@ -24,6 +26,7 @@ pub struct Config {      pub memory: Memory,      pub disk: Disk,      pub volume: Volume, +    pub weather: Weather,  }  #[derive(Clone)] @@ -53,6 +56,24 @@ pub struct Volume {      pub delay: f64,  } +#[derive(Clone)] +pub struct Weather { +    pub city: String, +    pub format: String, +    pub icon: String, +    pub enabled: bool, +    pub delay: f64, +} + +/* +// TODO: error handling required if rsblocks.yml file is empty + +this function is responsible to check if the rsblocks.yml file +exists to call parse_config to read it OTHERWISE +it will call load_defaults to load a hardcoded default configuration + +it will always return a valid configuration inside a Result. +*/  pub fn load_config() -> Result<Config, Error> {      let yml_source = env::var("HOME").unwrap() + "/.config/rsblocks/rsblocks.yml";      let mut data = String::new(); @@ -64,12 +85,14 @@ pub fn load_config() -> Result<Config, Error> {          }      };      file.read_to_string(&mut data)?; -      let yml_content = &YamlLoader::load_from_str(&data).unwrap()[0];      let config = parse_config(yml_content);      Ok(config)  } +/* +this is simply returns a hardcoded configuration as default +*/  fn load_defaults() -> Config {      Config {          seperator: String::from("|"), @@ -93,28 +116,47 @@ fn load_defaults() -> Config {              enabled: false,              delay: 0.17,          }, +        weather: Weather { +            city: String::from(""), +            format: String::from("+%t"), +            icon: String::from(""), +            enabled: false, +            delay: 7200.0, //7200 seconds = 2 hours +        },      }  } +/* +it will read and parse the rsblocks.yml file content and return a valid configuration +IF some content is missing in the rsblocks.yml file, it will set +a default values to that +*/  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 time_icon = get_or_set_string(doc, "time", "icon", ""); -    let time_format = get_or_set_string(doc, "time", "format", "%T");      let mem_icon = get_or_set_string(doc, "memory", "icon", "");      let disk_icon = get_or_set_string(doc, "disk", "icon", "");      let volume_icon = get_or_set_string(doc, "volume", "icon", ""); +    let weather_icon = get_or_set_string(doc, "weather", "icon", ""); + +    //parsing formats and city weather +    let time_format = get_or_set_string(doc, "time", "format", "%T"); +    let weather_format = get_or_set_string(doc, "weather", "format", "%l:+%t"); +    let weather_city = get_or_set_string(doc, "weather", "city", "");      // parsing enabled state (everything false by default)      let disk_enabled = get_or_set_bool(doc, "disk", "enable");      let memory_enabled = get_or_set_bool(doc, "memory", "enable");      let volume_enabled = get_or_set_bool(doc, "volume", "enable"); +    let weather_enabled = get_or_set_bool(doc, "weather", "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 disk_delay = get_or_set_f32(doc, "disk", "delay", 120.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); +    let weather_delay = get_or_set_f32(doc, "weather", "delay", 7200.0);      Config {          seperator, @@ -138,6 +180,13 @@ fn parse_config(doc: &yaml::Yaml) -> Config {              enabled: volume_enabled,              delay: volume_delay,          }, +        weather: Weather { +            city: weather_city, +            format: weather_format, +            icon: weather_icon, +            enabled: weather_enabled, +            delay: weather_delay, +        },      }  } @@ -222,6 +271,19 @@ pub fn run(config: Config) {          });      } +    // Weather thread +    if config.weather.enabled { +        let weather_tx = tx.clone(); +        let configcp = config.clone(); +        let weather_data = get_weather(&configcp); +        let mut weather_data = ThreadsData::Weather(weather_data); +        thread::spawn(move || loop { +            weather_tx.send(weather_data).unwrap(); +            weather_data = ThreadsData::Weather(get_weather(&configcp)); +            thread::sleep(Duration::from_secs_f64(configcp.weather.delay)) +        }); +    } +      // Time thread      {          let time_tx = tx; @@ -238,14 +300,15 @@ pub fn run(config: Config) {      {          // NOTE: order matters to the final format -        let mut bar: Vec<String> = vec!["".to_string(); 4]; +        let mut bar: Vec<String> = vec!["".to_string(); 5];          //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, +                ThreadsData::Weather(x) => bar[1] = x, +                ThreadsData::Disk(x) => bar[2] = x, +                ThreadsData::Memory(x) => bar[3] = x, +                ThreadsData::Time(x) => bar[4] = x,              }              // match ends here @@ -254,6 +317,9 @@ pub fn run(config: Config) {      }  } +/* +this will call the command 'xsetroot -name <the arg>' +*/  pub fn update(bar: &Vec<String>) {      // TODO: FIX ME, this solution sucks      let mut x = String::new(); @@ -276,6 +342,7 @@ pub fn update(bar: &Vec<String>) {  ############################# HELPER FUNCTIONS BELOW ###################################  */ +  pub fn get_time(config: &Config) -> String {      let now = Local::now(); @@ -287,6 +354,30 @@ pub fn get_time(config: &Config) -> String {      )  } +/* +CREDIT: thanks for wttr.in to use their API +will make a GET request from wttr.in +*/ +fn get_weather(config: &Config) -> String { +    let format = if config.weather.format.is_empty() { +        String::from("%l:+%t") +    } else { +        config.weather.format.clone() +    }; + +    let url = format!("http://wttr.in/{}?format=\"{}", config.weather.city, format); +    let err_string = String::from("Error"); +    let res = match minreq::get(url).send() { +        Ok(resp) => match resp.as_str() { +            Ok(res_str) => res_str.trim_matches('"').to_string(), +            Err(_) => err_string, +        }, +        Err(_) => err_string, +    }; + +    format!("  {}  {}  {}", config.weather.icon, res, config.seperator) +} +  pub fn get_disk(config: &Config) -> String {      let cmd = Command::new("sh")          .arg("-c") @@ -308,7 +399,7 @@ pub fn get_disk(config: &Config) -> String {      )  } -// TODO: what a horrible solution to get the sound, i dont like it +// FIX ME: what a horrible solution to get the sound, i dont like it  //       find another way  pub fn get_volume(config: &Config) -> String { @@ -331,6 +422,11 @@ pub fn get_volume(config: &Config) -> String {      format!("  {}  {}  {}", config.volume.icon, vol, config.seperator)  } +/* +mem_used = (mem_total + shmem - mem_free - mem_buffers - mem_cached - mem_srecl +thanks for htop's developer on stackoverflow for providing this algorithm to +calculate used memory. +*/  pub fn get_memory(config: &Config) -> Result<String, std::io::Error> {      let mut buf = String::new(); @@ -392,7 +488,12 @@ pub fn get_memory(config: &Config) -> Result<String, std::io::Error> {      }      Ok(result)  } -// helper function for the get_memory function + +/* +this helper function will split the line(first argument) by the character(:) +and then parse the right splited item as u32 +then assign that to the "assignable"(2nd argument). +*/  fn assign_val(line: &str, assignable: &mut u32) {      let parsed: u32 = line.split(':').collect::<Vec<&str>>()[1]          .trim()  | 
