From 2a23bd20440b0d1a359c6bb4385b6771f2657a3b Mon Sep 17 00:00:00 2001 From: Shoelace Date: Wed, 19 May 2021 21:44:58 +0200 Subject: [PATCH] Threaded to async (#58) * Add async deps * Rename blocks to 'BlockManager' * Refactor "Blocks" to own module * Make all util fn async * Remove stray println --- Cargo.lock | 486 +++++++++++++++++++++++++++++++++++--- Cargo.toml | 28 ++- src/blockmanager.rs | 54 +++++ src/main.rs | 12 +- src/run.rs | 158 ++++++------- src/types.rs | 19 -- src/utils/battery.rs | 2 +- src/utils/bitcoins.rs | 2 +- src/utils/cpu.rs | 2 +- src/utils/disk.rs | 2 +- src/utils/load_average.rs | 2 +- src/utils/memory.rs | 2 +- src/utils/mod.rs | 1 + src/utils/mpd.rs | 2 +- src/utils/netspeed.rs | 6 +- src/utils/pub_ip.rs | 2 +- src/utils/spotify.rs | 2 +- src/utils/time.rs | 3 +- src/utils/uptime.rs | 2 +- src/utils/volume.rs | 2 +- src/utils/weather.rs | 2 +- 21 files changed, 631 insertions(+), 160 deletions(-) create mode 100644 src/blockmanager.rs diff --git a/Cargo.lock b/Cargo.lock index 8f5f388..ba9b8f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "ahash" version = "0.4.7" @@ -28,6 +30,16 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "async-attributes" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "async-channel" version = "1.6.1" @@ -39,6 +51,36 @@ dependencies = [ "futures-core", ] +[[package]] +name = "async-executor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "871f9bb5e0a22eeb7e8cf16641feb87c9dc67032ccf8ff49e772eb9941d3a965" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "once_cell", + "slab", +] + +[[package]] +name = "async-global-executor" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9586ec52317f36de58453159d48351bc244bc24ced3effc1fce22f3d48664af6" +dependencies = [ + "async-channel", + "async-executor", + "async-io", + "async-mutex", + "blocking", + "futures-lite", + "num_cpus", + "once_cell", +] + [[package]] name = "async-io" version = "1.4.1" @@ -59,6 +101,24 @@ dependencies = [ "winapi", ] +[[package]] +name = "async-lock" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6a8ea61bf9947a1007c5cada31e647dbc77b103c679858150003ba697ea798b" +dependencies = [ + "event-listener", +] + +[[package]] +name = "async-mutex" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e" +dependencies = [ + "event-listener", +] + [[package]] name = "async-net" version = "1.6.0" @@ -71,12 +131,51 @@ dependencies = [ "futures-lite", ] +[[package]] +name = "async-std" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9f06685bad74e0570f5213741bea82158279a4103d988e57bfada11ad230341" +dependencies = [ + "async-attributes", + "async-channel", + "async-global-executor", + "async-io", + "async-lock", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite", + "gloo-timers", + "kv-log-macro", + "log", + "memchr", + "num_cpus", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + [[package]] name = "async-task" version = "4.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0" +[[package]] +name = "async-trait" +version = "0.1.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b98e84bbb4cbcdd97da190ba0c58a1bb0de2c1fdf67d159e192ed766aeca722" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "atomic-waker" version = "1.0.0" @@ -171,6 +270,12 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bed57e2090563b83ba8f83366628ce535a7584c9afa4c9fc0612a03925c6df58" +[[package]] +name = "bytes" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" + [[package]] name = "cache-padded" version = "1.1.1" @@ -227,6 +332,27 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac" +[[package]] +name = "crossbeam-utils" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4feb231f0d4d6af81aed15928e58ecf5816aa62a2393e2c82f46973e92a9a278" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + +[[package]] +name = "ctor" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e98e2ad1a782e33928b96fc3948e7c355e5af34ba4de7670fe8bac2a3b2006d" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "cty" version = "0.2.1" @@ -265,16 +391,52 @@ dependencies = [ ] [[package]] -name = "futures-core" -version = "0.3.14" +name = "futures" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "098cd1c6dda6ca01650f1a37a794245eb73181d0d4d4e955e2f3c37db7af1815" +checksum = "0e7e43a803dae2fa37c1f6a8fe121e1f7bf9548b4dfc0522a42f34145dadfc27" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e682a68b29a882df0545c143dc3646daefe80ba479bcdede94d5a703de2871e2" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0402f765d8a89a26043b889b26ce3c4679d268fa6bb22cd7c6aad98340e179d1" + +[[package]] +name = "futures-executor" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "badaa6a909fac9e7236d0620a2f57f7664640c56575b71a7552fbd68deafab79" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] [[package]] name = "futures-io" -version = "0.3.14" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "365a1a1fb30ea1c03a830fdb2158f5236833ac81fa0ad12fe35b29cddc35cb04" +checksum = "acc499defb3b348f8d8f3f66415835a9131856ff7714bf10dadfc4ec4bdb29a1" [[package]] name = "futures-lite" @@ -291,6 +453,65 @@ dependencies = [ "waker-fn", ] +[[package]] +name = "futures-macro" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c40298486cdf52cc00cd6d6987892ba502c7656a16a4192a9992b1ccedd121" +dependencies = [ + "autocfg", + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a57bead0ceff0d6dde8f465ecd96c9338121bb7717d3e7b108059531870c4282" + +[[package]] +name = "futures-task" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a16bef9fc1a4dddb5bee51c989e3fbba26569cbb0e31f5b303c184e3dd33dae" + +[[package]] +name = "futures-util" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "feb5c238d27e2bf94ffdfd27b2c29e3df4a68c4193bb6427384259e2bf191967" +dependencies = [ + "autocfg", + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "proc-macro-hack", + "proc-macro-nested", + "slab", +] + +[[package]] +name = "gloo-timers" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "hashbrown" version = "0.9.1" @@ -300,6 +521,15 @@ dependencies = [ "ahash", ] +[[package]] +name = "hermit-abi" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +dependencies = [ + "libc", +] + [[package]] name = "instant" version = "0.1.9" @@ -317,13 +547,22 @@ checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" [[package]] name = "js-sys" -version = "0.3.50" +version = "0.3.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d99f9e3e84b8f67f846ef5b4cbbc3b1c29f6c759fcbce6f01aa0e73d932a24c" +checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062" dependencies = [ "wasm-bindgen", ] +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -357,6 +596,15 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" +[[package]] +name = "lock_api" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" +dependencies = [ + "scopeguard", +] + [[package]] name = "log" version = "0.4.14" @@ -364,6 +612,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ "cfg-if", + "value-bag", ] [[package]] @@ -377,9 +626,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.3.4" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" +checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" [[package]] name = "minreq" @@ -395,6 +644,28 @@ dependencies = [ "webpki-roots", ] +[[package]] +name = "mio" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf80d3e903b34e0bd7282b218398aec54e082c840d9baf8339e0080a0c542956" +dependencies = [ + "libc", + "log", + "miow", + "ntapi", + "winapi", +] + +[[package]] +name = "miow" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" +dependencies = [ + "winapi", +] + [[package]] name = "mpd" version = "0.0.12" @@ -430,6 +701,15 @@ dependencies = [ "libc", ] +[[package]] +name = "ntapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" +dependencies = [ + "winapi", +] + [[package]] name = "num-integer" version = "0.1.44" @@ -449,6 +729,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "once_cell" version = "1.7.2" @@ -461,12 +751,43 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" +[[package]] +name = "parking_lot" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall", + "smallvec", + "winapi", +] + [[package]] name = "pin-project-lite" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "pkg-config" version = "0.3.19" @@ -486,6 +807,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + +[[package]] +name = "proc-macro-nested" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" + [[package]] name = "proc-macro2" version = "1.0.26" @@ -504,6 +837,15 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "742739e41cd49414de871ea5e549afb7e2a3ac77b589bcbebe8c82fab37147fc" +dependencies = [ + "bitflags", +] + [[package]] name = "ref_slice" version = "1.2.1" @@ -530,16 +872,20 @@ name = "rsblocks" version = "0.1.17" dependencies = [ "alsa", + "async-std", + "async-trait", "battery", "breadx", "chrono", "dbus", + "futures", "lazy_static", "minreq", "mpd", "nix 0.20.0", "serde", "serde_yaml", + "tokio", ] [[package]] @@ -567,6 +913,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + [[package]] name = "sct" version = "0.6.1" @@ -579,18 +931,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.125" +version = "1.0.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" +checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.125" +version = "1.0.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d" +checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43" dependencies = [ "proc-macro2", "quote", @@ -620,12 +972,27 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "signal-hook-registry" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16f1d0fef1604ba8f7a073c7e701f213e056707210e9020af4528e0101ce11a6" +dependencies = [ + "libc", +] + [[package]] name = "slab" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f173ac3d1a7e3b28003f40de0b5ce7fe2710f9b9dc3fc38664cebee46b3b6527" +[[package]] +name = "smallvec" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" + [[package]] name = "socket2" version = "0.4.0" @@ -644,9 +1011,9 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] name = "syn" -version = "1.0.71" +version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad184cc9470f9117b2ac6817bfe297307418819ba40552f9b3846f05c33d5373" +checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" dependencies = [ "proc-macro2", "quote", @@ -679,6 +1046,37 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" +[[package]] +name = "tokio" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd3076b5c8cc18138b8f8814895c11eb4de37114a5d127bafdc5e55798ceef37" +dependencies = [ + "autocfg", + "bytes", + "libc", + "memchr", + "mio", + "num_cpus", + "once_cell", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "tokio-macros", + "winapi", +] + +[[package]] +name = "tokio-macros" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c49e3df43841dafb86046472506755d8501c5615673955f6aa17181125d13c37" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "typenum" version = "1.13.0" @@ -687,9 +1085,9 @@ checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" [[package]] name = "unicode-xid" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" [[package]] name = "untrusted" @@ -707,6 +1105,22 @@ dependencies = [ "typenum", ] +[[package]] +name = "value-bag" +version = "1.0.0-alpha.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd320e1520f94261153e96f7534476ad869c14022aee1e59af7c778075d840ae" +dependencies = [ + "ctor", + "version_check", +] + +[[package]] +name = "version_check" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" + [[package]] name = "waker-fn" version = "1.1.0" @@ -721,9 +1135,9 @@ checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" [[package]] name = "wasm-bindgen" -version = "0.2.73" +version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83240549659d187488f91f33c0f8547cbfef0b2088bc470c116d1d260ef623d9" +checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -731,9 +1145,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.73" +version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae70622411ca953215ca6d06d3ebeb1e915f0f6613e3b495122878d7ebec7dae" +checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900" dependencies = [ "bumpalo", "lazy_static", @@ -745,10 +1159,22 @@ dependencies = [ ] [[package]] -name = "wasm-bindgen-macro" -version = "0.2.73" +name = "wasm-bindgen-futures" +version = "0.4.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e734d91443f177bfdb41969de821e15c516931c3c3db3d318fa1b68975d0f6f" +checksum = "5fba7978c679d53ce2d0ac80c8c175840feb849a161664365d1287b41f2e67f1" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -756,9 +1182,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.73" +version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d53739ff08c8a68b0fdbcd54c372b8ab800b1449ab3c9d706503bc7dd1621b2c" +checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" dependencies = [ "proc-macro2", "quote", @@ -769,15 +1195,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.73" +version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9a543ae66aa233d14bb765ed9af4a33e81b8b58d1584cf1b47ff8cd0b9e4489" +checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f" [[package]] name = "web-sys" -version = "0.3.50" +version = "0.3.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a905d57e488fec8861446d3393670fb50d27a262344013181c2cdf9fff5481be" +checksum = "e828417b379f3df7111d3a2a9e5753706cae29c41f7c4029ee9fd77f3e09e582" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index cbc2a95..fc6dcc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,15 +13,33 @@ description = "a multi threaded status bar for dwm window manager for linux" [dependencies] alsa = "0.5" chrono = "0.4" -dbus = "0.9" -lazy_static = "1.4" -minreq = {version = "2.2", features = ["https","json-using-serde"]} mpd = "0.0.12" nix = "0.20" -serde = {version = "1.0", features = ["derive"]} -serde_yaml = "0.8" battery = "0.7" +dbus = "0.9.1" +serde_yaml = "0.8.15" +lazy_static = "1.4.0" + +# Async deps +futures = "0.3.15" +async-trait = "0.1.50" + +[dependencies.async-std] +version = "1.9.0" +features = ["attributes"] + +[dependencies.tokio] +version = "1.6.0" +features = ["full"] + +[dependencies.serde] +version = "1.0.120" +features = ["derive"] [dependencies.breadx] version = "0.1.11" features = ["async"] + +[dependencies.minreq] +version = "2.2" +features = ["https", "json-using-serde"] diff --git a/src/blockmanager.rs b/src/blockmanager.rs new file mode 100644 index 0000000..b0b3b5e --- /dev/null +++ b/src/blockmanager.rs @@ -0,0 +1,54 @@ +use breadx::{display::*, window::Window}; +use crate::types::ThreadsData; + +pub struct BlockManager { + pub disp: Display, + blocks: Vec, + pub root: Window, +} + +impl BlockManager { + pub fn new() -> Self { + let disp = Display::create(None, None).expect("Failed to create x11 connection"); + let root = disp.default_screen().root; + Self { + disp, + blocks: vec![String::from(""); 14], + root, + } + } + + pub fn update(&mut self, data: ThreadsData) { + match data { + ThreadsData::Spotify(x) => self.blocks[0] = x, + ThreadsData::Mpd(x) => self.blocks[1] = x, + ThreadsData::Sound(x) => self.blocks[2] = x, + ThreadsData::Weather(x) => self.blocks[3] = x, + ThreadsData::NetSpeed(x) => self.blocks[4] = x, + ThreadsData::BitCoins(x) => self.blocks[5] = x, + ThreadsData::PubIp(x) => self.blocks[6] = x, + ThreadsData::Disk(x) => self.blocks[7] = x, + ThreadsData::Memory(x) => self.blocks[8] = x, + ThreadsData::CpuTemp(x) => self.blocks[9] = x, + ThreadsData::LoadAvg(x) => self.blocks[10] = x, + ThreadsData::Battery(x) => self.blocks[11] = x, + ThreadsData::Uptime(x) => self.blocks[12] = x, + ThreadsData::Time(x) => self.blocks[13] = x, + } + let mut x = String::new(); + for i in self.blocks.iter() { + x.push_str(i.as_str()); + } + + self.root + .set_title(&mut self.disp, &x) + .expect("Failed to set title"); + + } +} + +impl Default for BlockManager { + fn default() -> Self { + Self::new() + } +} diff --git a/src/main.rs b/src/main.rs index a2ed5d0..3cfd2e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,17 @@ mod config; mod run; mod types; mod utils; +mod blockmanager; + use std::env; use std::process; +use blockmanager::BlockManager; + use lazy_static::initialize; -fn main() { +#[async_std::main] +async fn main() -> Result<(), Box> { initialize(&config::CONFIG); @@ -17,6 +22,7 @@ fn main() { process::exit(1); }; - let blocks = types::Blocks::new(); - run::run(blocks); + let blocks = BlockManager::new(); + run::run(blocks).await; + Ok(()) } diff --git a/src/run.rs b/src/run.rs index 201c337..3a5186b 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,135 +1,121 @@ +use crate::blockmanager::*; use crate::config::CONFIG; use crate::types::*; use crate::utils::*; -use std::sync::mpsc; -use std::thread; +use async_std::channel::{unbounded, Sender}; +use async_std::task; +use async_std::task::sleep; +use futures::future; +use futures::stream::StreamExt; use std::time::Duration; +use std::future::Future; -fn spawn_thread_loop(tx: std::sync::mpsc::Sender, data: F, delay: f64) + + +async fn init_block(tx: Sender, block: F, delay: f64) where - F: Fn() -> ThreadsData + Send + 'static, + F: Fn() -> Fut, + Fut: Future { - thread::spawn(move || loop { - tx.send(data()).unwrap(); - thread::sleep(Duration::from_secs_f64(delay)); - }); + loop { + let _ = tx.send(block().await).await; + sleep(Duration::from_secs_f64(delay)).await; + } } -pub fn run(mut blocks: Blocks) { - let (tx, rx) = mpsc::channel(); +pub async fn run(mut blocks: BlockManager) { + let (tx, rx) = unbounded(); - // loadavrage thread + // loadavrage task if CONFIG.loadavg.enabled { - spawn_thread_loop(tx.clone(), load_average::get_load_avg, CONFIG.loadavg.delay); + let b = init_block(tx.clone(), load_average::get_load_avg, CONFIG.loadavg.delay); + task::spawn(b); } - // public ip thread + // public ip task if CONFIG.pub_ip.enabled { - spawn_thread_loop(tx.clone(), pub_ip::get_pub_ip, CONFIG.pub_ip.delay); + let b = init_block(tx.clone(), pub_ip::get_pub_ip, CONFIG.pub_ip.delay); + task::spawn(b); } - // spotify thread + // spotify task if CONFIG.spotify.enabled { - spawn_thread_loop(tx.clone(), spotify::get_spotify, CONFIG.spotify.delay); + let b = init_block(tx.clone(), spotify::get_spotify, CONFIG.spotify.delay); + task::spawn(b); } - // mpd thread + // mpd task if CONFIG.mpd.enabled { - spawn_thread_loop(tx.clone(), mpd::get_mpd_current, CONFIG.mpd.delay); + let b = init_block(tx.clone(), mpd::get_mpd_current, CONFIG.mpd.delay); + task::spawn(b); } - // volume thread + // volume task if CONFIG.volume.enabled { - spawn_thread_loop(tx.clone(), volume::get_volume, CONFIG.volume.delay); + let b = init_block(tx.clone(), volume::get_volume, CONFIG.volume.delay); + task::spawn(b); } - // Disk thread - if CONFIG.disk.enabled { - spawn_thread_loop(tx.clone(), disk::get_disk, CONFIG.disk.delay); + // Disk task + if + /*CONFIG.disk.enabled*/ + false { + let b = init_block(tx.clone(), disk::get_disk, CONFIG.disk.delay); + task::spawn(b); } - // Memory thread - if CONFIG.memory.enabled { - spawn_thread_loop(tx.clone(), memory::get_memory, CONFIG.memory.delay); + // Memory task + if + /*CONFIG.memory.enabled*/ + false { + let b = init_block(tx.clone(), memory::get_memory, CONFIG.memory.delay); + task::spawn(b); } - // Weather thread + // Weather task if CONFIG.weather.enabled { - spawn_thread_loop(tx.clone(), weather::get_weather, CONFIG.weather.delay); + let b = init_block(tx.clone(), weather::get_weather, CONFIG.weather.delay); + task::spawn(b); } - // Battery thread + // Battery task if CONFIG.battery.enabled { - spawn_thread_loop(tx.clone(), battery::get_battery, CONFIG.battery.delay); + let b = init_block(tx.clone(), battery::get_battery, CONFIG.battery.delay); + task::spawn(b); } - // Cpu temperature thread + // Cpu temperature task if CONFIG.cpu_temperature.enabled { - spawn_thread_loop(tx.clone(), cpu::get_cpu_temp, CONFIG.cpu_temperature.delay); + let b = init_block(tx.clone(), cpu::get_cpu_temp, CONFIG.cpu_temperature.delay); + task::spawn(b); } - // Uptime thread + // Uptime task if CONFIG.uptime.enabled { - spawn_thread_loop(tx.clone(), uptime::get_uptime, CONFIG.uptime.delay); + let b = init_block(tx.clone(), uptime::get_uptime, CONFIG.uptime.delay); + task::spawn(b); } - // BTC thread + // BTC task if CONFIG.bitcoins.enabled { - spawn_thread_loop(tx.clone(), bitcoins::get_price, CONFIG.bitcoins.delay); + let b = init_block(tx.clone(), bitcoins::get_price, CONFIG.bitcoins.delay); + task::spawn(b); } - // net speed thread - // get_netspeed will sleep inside the function + // net speed task if CONFIG.netspeed.enabled { - let net_tx = tx.clone(); - thread::spawn(move || loop { - let net_data = netspeed::get_netspeed(); - net_tx.send(net_data).unwrap(); - }); + let b = init_block(tx.clone(), netspeed::get_netspeed, 0.); + task::spawn(b); } - // Time thread - { - spawn_thread_loop(tx, time::get_time, CONFIG.time.delay); - } + // Time task + let b = init_block(tx, time::get_time, CONFIG.time.delay); + task::spawn(b); - //Main - { - // NOTE: order matters to the final format + // NOTE: order matters to the final format - let mut bar: Vec = vec![String::from(""); 14]; - //iterating the values recieved from the threads - for data in rx { - match data { - ThreadsData::Spotify(x) => bar[0] = x, - ThreadsData::Mpd(x) => bar[1] = x, - ThreadsData::Sound(x) => bar[2] = x, - ThreadsData::Weather(x) => bar[3] = x, - ThreadsData::NetSpeed(x) => bar[4] = 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 - update(&bar, &mut blocks); - } - } -} - -fn update(bar: &[String], blocks: &mut Blocks) { - let mut x = String::new(); - for i in bar.iter() { - x.push_str(i.as_str()); - } - - blocks - .root - .set_title(&mut blocks.disp, &x) - .expect("Failed to set title of root"); + rx.for_each(|data| { + blocks.update(data); + future::ready(()) + }) + .await; } diff --git a/src/types.rs b/src/types.rs index 9b31946..9fd834e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,3 @@ -use breadx::{display::*, window::Window}; #[derive(Debug, Clone)] pub enum ThreadsData { @@ -140,21 +139,3 @@ pub struct BitCoins { pub delay: f64, } -pub struct Blocks { - pub disp: Display, - pub root: Window, -} - -impl Blocks { - pub fn new() -> Self { - let disp = Display::create(None, None).expect("Failed to create x11 connection"); - let root = disp.default_screen().root; - Self { disp, root } - } -} - -impl Default for Blocks { - fn default() -> Self { - Self::new() - } -} diff --git a/src/utils/battery.rs b/src/utils/battery.rs index d3688e7..432e22a 100644 --- a/src/utils/battery.rs +++ b/src/utils/battery.rs @@ -5,7 +5,7 @@ use battery::Manager; // TODO: better error handeling // getting battery percentage -pub fn get_battery() -> ThreadsData { +pub async fn get_battery() -> ThreadsData { let battery_manager = if let Ok(manager) = Manager::new() { manager } else { diff --git a/src/utils/bitcoins.rs b/src/utils/bitcoins.rs index 465d907..94a0a3b 100644 --- a/src/utils/bitcoins.rs +++ b/src/utils/bitcoins.rs @@ -7,7 +7,7 @@ struct Response { price_24h: f64, } -pub fn get_price() -> ThreadsData { +pub async fn get_price() -> ThreadsData { let url = format!( "https://api.blockchain.com/v3/exchange/tickers/{}", CONFIG.bitcoins.symbol diff --git a/src/utils/cpu.rs b/src/utils/cpu.rs index 2047a07..f49fda0 100644 --- a/src/utils/cpu.rs +++ b/src/utils/cpu.rs @@ -3,7 +3,7 @@ use crate::types::ThreadsData; use std::fs::read_to_string; // getting cpu temperature -pub fn get_cpu_temp() -> ThreadsData { +pub async fn get_cpu_temp() -> ThreadsData { let buf = match read_to_string("/sys/class/thermal/thermal_zone0/temp") { Ok(data) => data, _ => return ThreadsData::CpuTemp(String::from("Error reading temp")), diff --git a/src/utils/disk.rs b/src/utils/disk.rs index 2250911..33b6449 100644 --- a/src/utils/disk.rs +++ b/src/utils/disk.rs @@ -1,7 +1,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; -pub fn get_disk() -> ThreadsData { +pub async fn get_disk() -> ThreadsData { const GB: u64 = (1024 * 1024) * 1024; let statvfs = nix::sys::statvfs::statvfs("/").unwrap(); let mut disk_used = String::new(); diff --git a/src/utils/load_average.rs b/src/utils/load_average.rs index 43dc88b..337875c 100644 --- a/src/utils/load_average.rs +++ b/src/utils/load_average.rs @@ -3,7 +3,7 @@ use crate::types::ThreadsData; use nix::libc::{c_double, c_int, getloadavg}; -pub fn get_load_avg() -> ThreadsData { +pub async fn get_load_avg() -> ThreadsData { let mut data: [c_double; 3] = [0f64; 3]; unsafe { getloadavg(data.as_mut_ptr(), data.len() as c_int) }; let [load, _, _] = data; diff --git a/src/utils/memory.rs b/src/utils/memory.rs index 3d51fd5..46b571b 100644 --- a/src/utils/memory.rs +++ b/src/utils/memory.rs @@ -7,7 +7,7 @@ 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() -> ThreadsData { +pub async fn get_memory() -> ThreadsData { let buf = match read_to_string("/proc/meminfo") { Ok(data) => data, _ => return ThreadsData::Memory(String::from("Error Reading memory!")), diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 22b4bad..6e0d944 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -12,3 +12,4 @@ pub mod time; pub mod uptime; pub mod volume; pub mod weather; + diff --git a/src/utils/mpd.rs b/src/utils/mpd.rs index 678b59d..95af768 100644 --- a/src/utils/mpd.rs +++ b/src/utils/mpd.rs @@ -3,7 +3,7 @@ use crate::types::ThreadsData; use mpd::{Client, Song}; // getting mpd song file -pub fn get_mpd_current() -> ThreadsData { +pub async fn get_mpd_current() -> ThreadsData { let stream_path = format!("{}:{}", CONFIG.mpd.host, CONFIG.mpd.port); let empty_data = ThreadsData::Mpd(String::from("")); let mut conn = match Client::connect(&stream_path) { diff --git a/src/utils/netspeed.rs b/src/utils/netspeed.rs index 16caaa4..fae36b5 100644 --- a/src/utils/netspeed.rs +++ b/src/utils/netspeed.rs @@ -1,13 +1,13 @@ use crate::config::CONFIG; use crate::types::ThreadsData; use std::fs::read_to_string; -use std::thread; +use async_std::task::sleep; use std::time::Duration; -pub fn get_netspeed() -> ThreadsData { +pub async fn get_netspeed() -> ThreadsData { let tx1: u64 = parse_speed_file("tx_bytes"); let rx1: u64 = parse_speed_file("rx_bytes"); - thread::sleep(Duration::from_secs(1)); + sleep(Duration::from_secs(1)).await; let tx2: u64 = parse_speed_file("tx_bytes"); let rx2: u64 = parse_speed_file("rx_bytes"); diff --git a/src/utils/pub_ip.rs b/src/utils/pub_ip.rs index 4f1335f..dde354a 100644 --- a/src/utils/pub_ip.rs +++ b/src/utils/pub_ip.rs @@ -1,7 +1,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; -pub fn get_pub_ip() -> ThreadsData { +pub async fn get_pub_ip() -> ThreadsData { let url = "http://api.ipify.org".to_string(); let _err = String::from("Error"); let res = match minreq::get(url).send() { diff --git a/src/utils/spotify.rs b/src/utils/spotify.rs index c48471d..5da9fea 100644 --- a/src/utils/spotify.rs +++ b/src/utils/spotify.rs @@ -5,7 +5,7 @@ use dbus::{arg, blocking::Connection}; use std::time::Duration; // getting spotify current artist and title. -pub fn get_spotify() -> ThreadsData { +pub async fn get_spotify() -> ThreadsData { let empty_data = ThreadsData::Spotify(String::from("")); let conn = match Connection::new_session() { Ok(conn) => conn, diff --git a/src/utils/time.rs b/src/utils/time.rs index 191d91b..903f301 100644 --- a/src/utils/time.rs +++ b/src/utils/time.rs @@ -2,7 +2,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; use chrono::prelude::*; -pub fn get_time() -> ThreadsData { +pub async fn get_time() -> ThreadsData { let now = Local::now(); let data = format!( @@ -11,6 +11,5 @@ pub fn get_time() -> ThreadsData { now.format(&CONFIG.time.format), CONFIG.seperator ); - ThreadsData::Time(data) } diff --git a/src/utils/uptime.rs b/src/utils/uptime.rs index f635b6b..e575e78 100644 --- a/src/utils/uptime.rs +++ b/src/utils/uptime.rs @@ -2,7 +2,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; use nix::time::clock_gettime; -pub fn get_uptime() -> ThreadsData { +pub async fn get_uptime() -> ThreadsData { let (_, hour, minutes, seconds) = get_uptime_data(); let uptime = if hour > 0 { format!("{}:{}:{}", hour, minutes, seconds) diff --git a/src/utils/volume.rs b/src/utils/volume.rs index 05c1dbe..47b4ed3 100644 --- a/src/utils/volume.rs +++ b/src/utils/volume.rs @@ -3,7 +3,7 @@ use crate::types::ThreadsData; use alsa::mixer::{Mixer, SelemChannelId, SelemId}; // getting volume percentage -pub fn get_volume() -> ThreadsData { +pub async fn get_volume() -> ThreadsData { let card = if CONFIG.volume.card == "PULSE" { "pulse" } else { diff --git a/src/utils/weather.rs b/src/utils/weather.rs index 11e446f..10c887c 100644 --- a/src/utils/weather.rs +++ b/src/utils/weather.rs @@ -2,7 +2,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; // will make a GET request from wttr.in -pub fn get_weather() -> ThreadsData { +pub async fn get_weather() -> ThreadsData { let format = if CONFIG.weather.format.is_empty() { String::from("%l:+%t") } else {