Use futures_async_stream crate for writing streams

This commit is contained in:
Skye Jensen 2020-08-07 21:13:02 -04:00
parent c47d061fe6
commit 49d7f5e19b
9 changed files with 160 additions and 184 deletions

208
Cargo.lock generated
View file

@ -6,20 +6,20 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi 0.3.8",
"winapi 0.3.9",
]
[[package]]
name = "arc-swap"
version = "0.4.6"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b585a98a234c46fc563103e9278c9391fde1f4e6850334da895d27edb9580f62"
checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034"
[[package]]
name = "async-trait"
version = "0.1.32"
version = "0.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eb7f9ad01405feb3c1dac82463038945cf88eea4569acaf3ad662233496dd96"
checksum = "a265e3abeffdce30b2e26b7a11b222fe37c6067404001b434101457d0385eb92"
dependencies = [
"proc-macro2",
"quote",
@ -34,7 +34,7 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi 0.3.8",
"winapi 0.3.9",
]
[[package]]
@ -51,9 +51,9 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "bytes"
version = "0.5.4"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1"
checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"
[[package]]
name = "cfg-if"
@ -63,9 +63,9 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "chrono"
version = "0.4.11"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2"
checksum = "c74d84029116787153e02106bf53e66828452a4b325cc8652b788b5967c0a0b6"
dependencies = [
"num-integer",
"num-traits",
@ -74,9 +74,9 @@ dependencies = [
[[package]]
name = "clap"
version = "2.33.1"
version = "2.33.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129"
checksum = "10040cdf04294b565d9e0319955430099ec3813a64c952b86a41200ad714ae48"
dependencies = [
"ansi_term",
"atty",
@ -89,9 +89,9 @@ dependencies = [
[[package]]
name = "dbus"
version = "0.8.3"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b17a12ffaff26515889b006fc029493a3e340366a137c13cec2cdd545ea3b8"
checksum = "5cd9e78c210146a1860f897db03412fd5091fd73100778e43ee255cca252cf32"
dependencies = [
"futures",
"libc",
@ -100,11 +100,12 @@ dependencies = [
[[package]]
name = "dbus-tokio"
version = "0.5.1"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "641f98f32585b8a8cc5d88c6c55a8f6cdf60740baab17c57d59b9b662c73f522"
checksum = "baaf99ea9b8296ba0db293cad28c51d788e690c47ebe9974b864354b258a636a"
dependencies = [
"dbus",
"libc",
"mio",
"tokio",
]
@ -146,6 +147,28 @@ dependencies = [
"futures-util",
]
[[package]]
name = "futures-async-stream"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5000008eed6f9c83b0c7d512cd3061175b438d927977642488aa4c52a0204be9"
dependencies = [
"futures-async-stream-macro",
"futures-core",
"pin-project",
]
[[package]]
name = "futures-async-stream-macro"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b25dc863339037ae268f1b44f556c8afe49c4ee9ef5abc9140817224539fb2b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-channel"
version = "0.3.5"
@ -248,9 +271,9 @@ dependencies = [
[[package]]
name = "hermit-abi"
version = "0.1.13"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71"
checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9"
dependencies = [
"libc",
]
@ -266,9 +289,9 @@ dependencies = [
[[package]]
name = "itoa"
version = "0.4.5"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]]
name = "kernel32-sys"
@ -288,9 +311,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.71"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
checksum = "a2f02823cf78b754822df5f7f268fb59822e7296276d3e069d8e8cb26a14bd10"
[[package]]
name = "libdbus-sys"
@ -303,9 +326,9 @@ dependencies = [
[[package]]
name = "log"
version = "0.4.8"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
dependencies = [
"cfg-if",
]
@ -337,14 +360,14 @@ dependencies = [
[[package]]
name = "mio-named-pipes"
version = "0.1.6"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3"
checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656"
dependencies = [
"log",
"mio",
"miow 0.3.4",
"winapi 0.3.8",
"miow 0.3.5",
"winapi 0.3.9",
]
[[package]]
@ -372,12 +395,12 @@ dependencies = [
[[package]]
name = "miow"
version = "0.3.4"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22dfdd1d51b2639a5abd17ed07005c3af05fb7a2a3b1a1d0d7af1000a520c1c7"
checksum = "07b88fb9795d4d36d62a012dfbf49a8f5cf12751f36d31a9dbe66d528e58979e"
dependencies = [
"socket2",
"winapi 0.3.8",
"winapi 0.3.9",
]
[[package]]
@ -388,14 +411,14 @@ checksum = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7"
dependencies = [
"cfg-if",
"libc",
"winapi 0.3.8",
"winapi 0.3.9",
]
[[package]]
name = "num-integer"
version = "0.1.42"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba"
checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"
dependencies = [
"autocfg",
"num-traits",
@ -403,9 +426,9 @@ dependencies = [
[[package]]
name = "num-traits"
version = "0.2.11"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"
dependencies = [
"autocfg",
]
@ -428,18 +451,18 @@ checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d"
[[package]]
name = "pin-project"
version = "0.4.17"
version = "0.4.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edc93aeee735e60ecb40cf740eb319ff23eab1c5748abfdb5c180e4ce49f7791"
checksum = "ca4433fff2ae79342e497d9f8ee990d174071408f28f726d6d83af93e58e48aa"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "0.4.17"
version = "0.4.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e58db2081ba5b4c93bd6be09c40fd36cb9193a8336c384f3b40012e531aa7e40"
checksum = "2c0e815c3ee9a031fdf5af21c10aa17c573c9c6a566328d99e3936c34e36461f"
dependencies = [
"proc-macro2",
"quote",
@ -448,9 +471,9 @@ dependencies = [
[[package]]
name = "pin-project-lite"
version = "0.1.5"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7505eeebd78492e0f6108f7171c4948dbb120ee8119d9d77d0afa5469bef67f"
checksum = "282adbf10f2698a7a77f8e983a74b2d18176c19a7fd32a45446139ae7b02b715"
[[package]]
name = "pin-utils"
@ -460,9 +483,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.17"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33"
[[package]]
name = "ppv-lite86"
@ -472,9 +495,9 @@ checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
[[package]]
name = "proc-macro-error"
version = "1.0.2"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98e9e4b82e0ef281812565ea4751049f1bdcdfccda7d3f459f2e138a40c08678"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
@ -485,43 +508,41 @@ dependencies = [
[[package]]
name = "proc-macro-error-attr"
version = "1.0.2"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f5444ead4e9935abd7f27dc51f7e852a0569ac888096d5ec2499470794e2e53"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn-mid",
"version_check",
]
[[package]]
name = "proc-macro-hack"
version = "0.5.16"
version = "0.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e0456befd48169b9f13ef0f0ad46d492cf9d2dbb918bcf38e01eed4ce3ec5e4"
checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598"
[[package]]
name = "proc-macro-nested"
version = "0.1.4"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e946095f9d3ed29ec38de908c22f95d9ac008e424c7bcae54c75a79c527c694"
checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a"
[[package]]
name = "proc-macro2"
version = "1.0.17"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1502d12e458c49a4c9cbff560d0fe0060c252bc29799ed94ca2ed4bb665a0101"
checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.6"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54a21852a652ad6f610c9510194f398ff6f8692e334fd1145fed931f7fbe44ea"
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
dependencies = [
"proc-macro2",
]
@ -569,9 +590,9 @@ dependencies = [
[[package]]
name = "redox_syscall"
version = "0.1.56"
version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "rustybar"
@ -582,8 +603,8 @@ dependencies = [
"dbus",
"dbus-tokio",
"futures",
"futures-async-stream",
"futures-util",
"pin-project",
"serde",
"serde_json",
"smart-default",
@ -595,24 +616,24 @@ dependencies = [
[[package]]
name = "ryu"
version = "1.0.4"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed3d612bc64430efeb3f7ee6ef26d590dce0c43249217bddc62112540c7941e1"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "serde"
version = "1.0.110"
version = "1.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99e7b308464d16b56eba9964e4972a3eee817760ab60d88c3f86e1fecb08204c"
checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.110"
version = "1.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "818fbf6bfa9a42d3bfcaca148547aa00c7b915bec71d1757aa2d44ca68771984"
checksum = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e"
dependencies = [
"proc-macro2",
"quote",
@ -621,9 +642,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.53"
version = "1.0.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "993948e75b189211a9b31a7528f950c6adc21f9720b6438ff80a7fa2f864cea2"
checksum = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c"
dependencies = [
"itoa",
"ryu",
@ -632,9 +653,9 @@ dependencies = [
[[package]]
name = "signal-hook-registry"
version = "1.2.0"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94f478ede9f64724c5d173d7bb56099ec3e2d9fc2774aac65d34b8b890405f41"
checksum = "a3e12110bc539e657a646068aaf5eb5b63af9d0c1f7b29c97113fad80e15f035"
dependencies = [
"arc-swap",
"libc",
@ -666,7 +687,7 @@ dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"winapi 0.3.8",
"winapi 0.3.9",
]
[[package]]
@ -677,9 +698,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.14"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "863246aaf5ddd0d6928dfeb1a9ca65f505599e4e1b399935ef7e75107516b4ef"
checksum = "de5472fb24d7e80ae84a7801b7978f95a19ec32cb1876faea59ab711eb901976"
dependencies = [
"clap",
"lazy_static",
@ -688,9 +709,9 @@ dependencies = [
[[package]]
name = "structopt-derive"
version = "0.4.7"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d239ca4b13aee7a2142e6795cbd69e457665ff8037aed33b3effdc430d2f927a"
checksum = "1e0eb37335aeeebe51be42e2dc07f031163fbabfa6ac67d7ea68b5c2f68d5f99"
dependencies = [
"heck",
"proc-macro-error",
@ -701,26 +722,15 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.28"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7e33a62f20d3dc02a1bc9c1d385f92b459bbf35e4dc325eed20c53db5b90c03"
checksum = "e69abc24912995b3038597a7a593be5053eb0fb44f3cc5beec0deb421790c1f4"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "syn-mid"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "textwrap"
version = "0.11.0"
@ -737,14 +747,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
dependencies = [
"libc",
"winapi 0.3.8",
"winapi 0.3.9",
]
[[package]]
name = "tokio"
version = "0.2.21"
version = "0.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d099fa27b9702bed751524694adbe393e18b36b204da91eb1cbbbbb4a5ee2d58"
checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd"
dependencies = [
"bytes",
"fnv",
@ -761,7 +771,7 @@ dependencies = [
"signal-hook-registry",
"slab",
"tokio-macros",
"winapi 0.3.8",
"winapi 0.3.9",
]
[[package]]
@ -792,15 +802,15 @@ checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"
[[package]]
name = "unicode-width"
version = "0.1.7"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.0"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "uuid"
@ -837,9 +847,9 @@ checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
[[package]]
name = "winapi"
version = "0.3.8"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",

View file

@ -13,8 +13,8 @@ chrono = "0.4"
dbus = "0.8"
dbus-tokio = "0.5"
futures = "0.3"
futures-async-stream = "0.2"
futures-util = "0.3"
pin-project = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
smart-default = "0.6"

View file

@ -112,9 +112,9 @@ pub async fn read_config() -> Result<Config, Box<dyn std::error::Error>> {
Ok(toml::from_slice(&config_contents)?)
}
pub fn process_tile(
pub fn process_tile<'a>(
tile: &TileConfig,
connection: &Arc<SyncConnection>,
connection: &'a Arc<SyncConnection>,
) -> BoxStream<'static, TileResult> {
let five_secs = Duration::from_secs(5);
match &tile.config_type {

View file

@ -1,3 +1,7 @@
#![feature(generators)]
#![feature(stmt_expr_attributes)]
#![feature(proc_macro_hygiene)]
mod async_iter;
mod config;
mod output;

View file

@ -1,13 +1,13 @@
use super::TileResult;
use crate::tile::Block;
use futures::{future::try_join3, stream, Stream};
use futures_util::StreamExt;
use futures::future::try_join3;
use futures_async_stream::try_stream;
use std::error::Error;
use tokio::fs::File;
use tokio::prelude::*;
pub fn battery_stream() -> impl Stream<Item = TileResult> {
stream::repeat(()).then(|()| async {
#[try_stream(ok = Block, error = Box<dyn Error + Send + Sync>)]
pub async fn battery_stream() {
loop {
let charge_now = async {
let mut raw = String::new();
File::open("/sys/class/power_supply/BAT0/charge_now")
@ -38,11 +38,11 @@ pub fn battery_stream() -> impl Stream<Item = TileResult> {
let (charge_now, charge_total, status) =
try_join3(charge_now, charge_total, status).await?;
let percentage = charge_now * 100 / charge_total;
Ok(Block {
yield Block {
full_text: format!("{}% {}", percentage, status).into(),
short_text: format!("{}%", percentage).into_boxed_str().into(),
name: "battery".into(),
..Default::default()
})
})
};
}
}

View file

@ -2,7 +2,8 @@ use crate::tile::Block;
use crate::tiles::TileResult;
use dbus::nonblock::stdintf::org_freedesktop_dbus::Properties;
use dbus::nonblock::{Proxy, SyncConnection};
use futures::{FutureExt, Stream};
use futures::stream::Stream;
use futures_async_stream::try_stream;
use std::time::Duration;
pub fn hostname_stream(connection: &SyncConnection) -> impl Stream<Item = TileResult> {
@ -13,14 +14,13 @@ pub fn hostname_stream(connection: &SyncConnection) -> impl Stream<Item = TileRe
connection,
);
let reply = proxy.get("org.freedesktop.hostname1", "Hostname");
async {
#[try_stream]
async move {
let hostname: String = reply.await?;
let block = Block {
yield Block {
full_text: hostname.into(),
name: "hostname".into(),
..Default::default()
};
Ok(block)
}
.into_stream()
}

View file

@ -1,22 +1,20 @@
use super::TileResult;
use crate::tile::Block;
use futures::stream::StreamExt;
use futures_async_stream::try_stream;
use std::error::Error;
use tokio::fs::File;
use tokio::prelude::*;
use tokio::stream::Stream;
pub fn load_stream() -> impl Stream<Item = TileResult> {
futures::stream::repeat(()).then(|()| async {
#[try_stream(ok = Block, error = Box<dyn Error + Send + Sync>)]
pub async fn load_stream() {
loop {
let mut raw = String::new();
File::open("/proc/loadavg")
.await?
.read_to_string(&mut raw)
.await?;
let mut file = File::open("/proc/loadavg").await?;
file.read_to_string(&mut raw).await?;
let (load, _rest) = raw.split_at(raw.find(' ').unwrap_or(0));
Ok(Block {
yield Block {
full_text: load.into(),
name: "load".into(),
..Default::default()
})
})
};
}
}

View file

@ -1,6 +1,6 @@
use super::TileResult;
use crate::tile::Block;
use futures::{stream, Stream, StreamExt};
use futures_async_stream::try_stream;
use std::error::Error;
use std::{io, str, u64};
use tokio::fs::File;
use tokio::prelude::*;
@ -41,13 +41,12 @@ fn extract_value(line: &str) -> Result<u64, Box<dyn std::error::Error + Send + S
.parse()?)
}
pub fn memory_stream() -> impl Stream<Item = TileResult> {
stream::repeat(()).then(|_| async {
#[try_stream(ok = Block, error = Box<dyn Error + Send + Sync>)]
pub async fn memory_stream() {
loop {
let mut raw = [0u8; 256];
File::open("/proc/meminfo")
.await?
.read_exact(&mut raw)
.await?;
let mut file = File::open("/proc/meminfo").await?;
file.read_exact(&mut raw).await?;
let string_data = str::from_utf8(&raw)?;
let mut lines = string_data.split('\n');
let mem_total = prettify_kib(extract_value(
@ -65,11 +64,11 @@ pub fn memory_stream() -> impl Stream<Item = TileResult> {
let full_text = format!("{} avail / {}", mem_avail, mem_total).into_boxed_str();
let short_text = format!("{} / {}", mem_avail, mem_total).into_boxed_str();
Ok(Block {
yield Block {
full_text,
short_text: Some(short_text),
name: "memory".into(),
..Default::default()
})
})
};
}
}

View file

@ -2,58 +2,23 @@ use super::TileResult;
use crate::config::TimeConfig;
use crate::tile::Block;
use chrono::prelude::*;
use chrono::DateTime;
use futures::future::Future;
use futures::stream::Stream;
use futures_util::ready;
use pin_project::pin_project;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::time::{delay_for, delay_until, Delay, Instant};
use futures_async_stream::stream;
use tokio::time::delay_for;
pub fn time_stream(config: TimeConfig) -> impl Stream<Item = TileResult> {
TimeStream {
config,
delay: delay_until(Instant::now()),
}
}
#[pin_project]
struct TimeStream {
config: TimeConfig,
#[pin]
delay: Delay,
}
impl TimeStream {
fn send_time(&self, time: DateTime<Local>) -> Block {
Block {
full_text: time.format(&self.config.format).to_string().into(),
short_text: Some(time.format(&self.config.short_format).to_string().into()),
#[stream(item = TileResult)]
pub async fn time_stream(config: TimeConfig) {
loop {
let now = Local::now();
yield Ok(Block {
full_text: now.format(&config.format).to_string().into(),
short_text: Some(now.format(&config.short_format).to_string().into()),
name: "time".into(),
..Default::default()
}
}
});
fn wait_for_next_second(now: DateTime<Local>) -> Delay {
let next = now.trunc_subsecs(0) + chrono::Duration::seconds(1);
let difference = next - now;
delay_for(difference.to_std().unwrap())
}
}
impl Stream for TimeStream {
type Item = TileResult;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let project = Pin::as_mut(&mut self).project();
ready!(Future::poll(project.delay, cx));
let now = Local::now();
Pin::as_mut(&mut self)
.project()
.delay
.set(TimeStream::wait_for_next_second(now));
Poll::Ready(Some(Ok(self.send_time(now))))
delay_for(difference.to_std().unwrap()).await;
}
}