From f16724e986b17dd739b086a917672bb36b1d9b16 Mon Sep 17 00:00:00 2001 From: Mustafa Salih <37256744+MustafaSalih1993@users.noreply.github.com> Date: Mon, 15 Feb 2021 04:31:29 +0300 Subject: [PATCH] Dev (#45) * added bitcoins prices * removed unwanted formating --- Cargo.lock | 187 ++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 12 +-- rsblocks.yml | 11 ++- src/config/bitcoins.rs | 21 +++++ src/config/mod.rs | 6 ++ src/run.rs | 24 ++++-- src/types.rs | 10 +++ src/utils/bitcoins.rs | 26 ++++++ src/utils/mod.rs | 1 + src/utils/netspeed.rs | 2 +- src/utils/pub_ip.rs | 2 +- 11 files changed, 282 insertions(+), 20 deletions(-) create mode 100644 src/config/bitcoins.rs create mode 100644 src/utils/bitcoins.rs diff --git a/Cargo.lock b/Cargo.lock index 131781b..31e665f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -89,6 +89,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + [[package]] name = "bitflags" version = "1.2.1" @@ -136,6 +142,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40e38929add23cdf8a366df9b0e088953150724bcbe5fc330b0d8eb3b328eec8" +[[package]] +name = "bumpalo" +version = "3.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "099e596ef14349721d9016f6b80dd3419ea1bf289ab9b44df8e4dfd3a005d5d9" + [[package]] name = "bytemuck" version = "1.5.0" @@ -270,6 +282,21 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "itoa" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" + +[[package]] +name = "js-sys" +version = "0.3.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cfb73131c35423a367daf8cbd24100af0d077668c8c2943f0e7dd775fef0f65" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -317,6 +344,14 @@ name = "minreq" version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85b112458b07e0864c7415847c423fe758c7189a28e72347e95d92c4bfd33f5d" +dependencies = [ + "lazy_static", + "rustls", + "serde", + "serde_json", + "webpki", + "webpki-roots", +] [[package]] name = "mpd" @@ -444,9 +479,24 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e1b7878800220a76a08f32c057829511440f65528b63b940f2f2bc145d7ac68" +[[package]] +name = "ring" +version = "0.16.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" +dependencies = [ + "cc", + "libc", + "once_cell", + "spin", + "untrusted", + "web-sys", + "winapi", +] + [[package]] name = "rsblocks" -version = "0.1.12" +version = "0.1.13" dependencies = [ "alsa", "breadx", @@ -466,6 +516,35 @@ version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" +[[package]] +name = "rustls" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "064fd21ff87c6e87ed4506e68beb42459caa4a0e2eb144932e6776768556980b" +dependencies = [ + "base64", + "log", + "ring", + "sct", + "webpki", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "sct" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3042af939fca8c3453b7af0f1c66e533a15a86169e39de2657310ade8f98d3c" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "serde" version = "1.0.123" @@ -486,6 +565,17 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea1c6153794552ea7cf7cf63b1231a25de00ec90db326ba6264440fa08e31486" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_yaml" version = "0.8.15" @@ -498,6 +588,12 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + [[package]] name = "syn" version = "1.0.60" @@ -541,6 +637,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + [[package]] name = "vec-arena" version = "1.0.0" @@ -565,6 +667,89 @@ version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" +[[package]] +name = "wasm-bindgen" +version = "0.2.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55c0f7123de74f0dab9b7d00fd614e7b19349cd1e2f5252bbe9b1754b59433be" +dependencies = [ + "cfg-if 1.0.0", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bc45447f0d4573f3d65720f636bbcc3dd6ce920ed704670118650bcd47764c7" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b8853882eef39593ad4174dd26fc9865a64e84026d223f63bb2c42affcbba2c" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4133b5e7f2a531fa413b3a1695e925038a05a71cf67e87dafa295cb645a01385" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd4945e4943ae02d15c13962b38a5b1e81eadd4b71214eee75af64a4d6a4fd64" + +[[package]] +name = "web-sys" +version = "0.3.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c40dc691fc48003eba817c38da7113c15698142da971298003cac3ef175680b3" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki" +version = "0.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e38c0608262c46d4a56202ebabdeb094cef7e560ca7a226c6bf055188aa4ea" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "webpki-roots" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91cd5736df7f12a964a5067a12c62fa38e1bd8080aff1f80bc29be7c80d19ab4" +dependencies = [ + "webpki", +] + [[package]] name = "wepoll-sys" version = "3.0.1" diff --git a/Cargo.toml b/Cargo.toml index e85723d..780e7cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsblocks" -version = "0.1.12" +version = "0.1.13" authors = ["mustafa salih "] edition = "2018" readme = "README.md" @@ -11,15 +11,15 @@ 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 [dependencies] -chrono = "0.4" -minreq = "2.2.1" alsa = "0.4.3" -nix = "0.19.1" -mpd = "0.0.12" +chrono = "0.4" dbus = "0.9.1" +lazy_static = "1.4.0" +minreq = {version = "2.2.1", features = ["https","json-using-serde"]} +mpd = "0.0.12" +nix = "0.19.1" serde = {version = "1.0.120", features = ["derive"]} serde_yaml = "0.8.15" -lazy_static = "1.4.0" [dependencies.breadx] diff --git a/rsblocks.yml b/rsblocks.yml index d0918cc..1fda65e 100644 --- a/rsblocks.yml +++ b/rsblocks.yml @@ -55,7 +55,7 @@ battery: cpu_temperature: delay: 120.0 - enabled: true + enabled: false icon: '' @@ -64,6 +64,13 @@ loadavg: delay: 2.0 enabled: false icon: '' + +# bitcoin price +bitcoins: + delay: 7200.0 # 2 hours + enabled: false + icon: '' + symbol: 'BTC-USD' mpd: @@ -77,7 +84,7 @@ mpd: volume: card: 'PULSE' delay: 0.18 - enabled: true + enabled: false icon: '' diff --git a/src/config/bitcoins.rs b/src/config/bitcoins.rs new file mode 100644 index 0000000..f9d5976 --- /dev/null +++ b/src/config/bitcoins.rs @@ -0,0 +1,21 @@ +use serde::{Deserialize, Serialize}; +use std::default::Default; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct BitCoins { + pub icon: String, + pub symbol: String, + pub enabled: bool, + pub delay: f64, +} + +impl Default for BitCoins { + fn default() -> Self { + BitCoins { + icon: String::from(""), + symbol: String::from("BTC-USD"), + enabled: false, + delay: 120.0, + } + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs index e55465f..1fa35ba 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,4 +1,5 @@ mod battery; +mod bitcoins; mod cputemp; mod disk; mod loadavg; @@ -14,6 +15,7 @@ mod weather; use self::mpd::Mpd; use battery::Battery; +use bitcoins::BitCoins; use cputemp::CpuTemp; use disk::Disk; use loadavg::LoadAvg; @@ -92,6 +94,9 @@ pub struct Config { #[serde(default)] pub pub_ip: PubIp, + + #[serde(default)] + pub bitcoins: BitCoins, } impl Default for Config { @@ -111,6 +116,7 @@ impl Default for Config { loadavg: Default::default(), netspeed: Default::default(), pub_ip: Default::default(), + bitcoins: Default::default(), } } } diff --git a/src/run.rs b/src/run.rs index 3f7405d..201c337 100644 --- a/src/run.rs +++ b/src/run.rs @@ -72,6 +72,11 @@ pub fn run(mut blocks: Blocks) { spawn_thread_loop(tx.clone(), uptime::get_uptime, CONFIG.uptime.delay); } + // BTC thread + if CONFIG.bitcoins.enabled { + spawn_thread_loop(tx.clone(), bitcoins::get_price, CONFIG.bitcoins.delay); + } + // net speed thread // get_netspeed will sleep inside the function if CONFIG.netspeed.enabled { @@ -91,7 +96,7 @@ pub fn run(mut blocks: Blocks) { { // NOTE: order matters to the final format - let mut bar: Vec = vec![String::from(""); 13]; + let mut bar: Vec = vec![String::from(""); 14]; //iterating the values recieved from the threads for data in rx { match data { @@ -100,14 +105,15 @@ pub fn run(mut blocks: Blocks) { ThreadsData::Sound(x) => bar[2] = x, ThreadsData::Weather(x) => bar[3] = x, ThreadsData::NetSpeed(x) => bar[4] = x, - ThreadsData::PubIp(x) => bar[5] = x, - ThreadsData::Disk(x) => bar[6] = x, - ThreadsData::Memory(x) => bar[7] = x, - ThreadsData::CpuTemp(x) => bar[8] = x, - ThreadsData::LoadAvg(x) => bar[9] = x, - ThreadsData::Battery(x) => bar[10] = x, - ThreadsData::Uptime(x) => bar[11] = x, - ThreadsData::Time(x) => bar[12] = x, + ThreadsData::BitCoins(x) => bar[5] = x, + ThreadsData::PubIp(x) => bar[6] = x, + ThreadsData::Disk(x) => bar[7] = x, + ThreadsData::Memory(x) => bar[8] = x, + ThreadsData::CpuTemp(x) => bar[9] = x, + ThreadsData::LoadAvg(x) => bar[10] = x, + ThreadsData::Battery(x) => bar[11] = x, + ThreadsData::Uptime(x) => bar[12] = x, + ThreadsData::Time(x) => bar[13] = x, } // match ends here diff --git a/src/types.rs b/src/types.rs index 5def739..a7fd975 100644 --- a/src/types.rs +++ b/src/types.rs @@ -15,6 +15,7 @@ pub enum ThreadsData { LoadAvg(String), NetSpeed(String), PubIp(String), + BitCoins(String), } #[derive(Clone)] @@ -32,6 +33,7 @@ pub struct Config { pub spotify: Spotify, pub loadavg: LoadAvg, pub pub_ip: PubIp, + pub bitcoins: BitCoins, } #[derive(Clone)] @@ -131,6 +133,14 @@ pub struct PubIp { pub delay: f64, } +#[derive(Clone)] +pub struct BitCoins { + pub icon: String, + pub symbol: String, + pub enabled: bool, + pub delay: f64, +} + pub struct Blocks { pub disp: Display, pub root: Window, diff --git a/src/utils/bitcoins.rs b/src/utils/bitcoins.rs new file mode 100644 index 0000000..465d907 --- /dev/null +++ b/src/utils/bitcoins.rs @@ -0,0 +1,26 @@ +use crate::config::CONFIG; +use crate::types::ThreadsData; +use serde::Deserialize; + +#[derive(Deserialize)] +struct Response { + price_24h: f64, +} + +pub fn get_price() -> ThreadsData { + let url = format!( + "https://api.blockchain.com/v3/exchange/tickers/{}", + CONFIG.bitcoins.symbol + ); + let _err = String::from("Error"); + let res = match minreq::get(url).send() { + Ok(resp) => match resp.json::() { + Ok(data) => data.price_24h.to_string(), + _ => _err, + }, + Err(_) => _err, + }; + + let data = format!(" {} {} {}", CONFIG.bitcoins.icon, res, CONFIG.seperator); + ThreadsData::BitCoins(data) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e70cf88..22b4bad 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,4 +1,5 @@ pub mod battery; +pub mod bitcoins; pub mod cpu; pub mod disk; pub mod load_average; diff --git a/src/utils/netspeed.rs b/src/utils/netspeed.rs index fe172a3..16caaa4 100644 --- a/src/utils/netspeed.rs +++ b/src/utils/netspeed.rs @@ -26,7 +26,7 @@ pub fn get_netspeed() -> ThreadsData { fn parse_speed_file(pth: &str) -> u64 { let base_path = format!("/sys/class/net/{}/statistics/", CONFIG.netspeed.interface); - let x: u64 = read_to_string(base_path.to_owned() + pth) + let x: u64 = read_to_string(base_path + pth) .unwrap() .trim() .parse::() diff --git a/src/utils/pub_ip.rs b/src/utils/pub_ip.rs index c0e6e4c..4f1335f 100644 --- a/src/utils/pub_ip.rs +++ b/src/utils/pub_ip.rs @@ -2,7 +2,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; pub fn get_pub_ip() -> ThreadsData { - let url = format!("http://api.ipify.org"); + let url = "http://api.ipify.org".to_string(); let _err = String::from("Error"); let res = match minreq::get(url).send() { Ok(resp) => match resp.as_str() {