Basic tile communication working

This commit is contained in:
Artemis Tosini 2020-05-30 19:45:43 +00:00
parent ed480f03d3
commit 03b06ce14f
Signed by: artemist
GPG key ID: EE5227935FE3FF18
5 changed files with 477 additions and 42 deletions

380
Cargo.lock generated
View file

@ -6,24 +6,154 @@ version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b585a98a234c46fc563103e9278c9391fde1f4e6850334da895d27edb9580f62" checksum = "b585a98a234c46fc563103e9278c9391fde1f4e6850334da895d27edb9580f62"
[[package]]
name = "async-std"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a45cee2749d880d7066e328a7e161c7470ced883b2fd000ca4643e9f1dd5083a"
dependencies = [
"async-task",
"crossbeam-utils",
"futures-channel",
"futures-core",
"futures-io",
"futures-timer",
"kv-log-macro",
"log",
"memchr",
"num_cpus",
"once_cell",
"pin-project-lite",
"pin-utils",
"slab",
"smol",
"wasm-bindgen-futures",
]
[[package]]
name = "async-task"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c17772156ef2829aadc587461c7753af20b7e8db1529bc66855add962a3b35d3"
[[package]]
name = "autocfg"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "bumpalo"
version = "3.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5356f1d23ee24a1f785a56d1d1a5f0fd5b0f6a0c0fb2412ce11da71649ab78f6"
[[package]] [[package]]
name = "bytes" name = "bytes"
version = "0.5.4" version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1" checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1"
[[package]]
name = "cc"
version = "1.0.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bbb73db36c1246e9034e307d0fba23f9a2e251faa47ade70c1bd252220c8311"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "0.1.10" version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "chrono"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2"
dependencies = [
"num-integer",
"num-traits",
"time",
]
[[package]]
name = "crossbeam"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69323bff1fb41c635347b8ead484a5ca6c3f11914d784170b158d8449ab07f8e"
dependencies = [
"cfg-if",
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-epoch",
"crossbeam-queue",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-channel"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cced8691919c02aac3cb0a1bc2e9b73d89e832bf9a06fc579d4e71b68a2da061"
dependencies = [
"crossbeam-utils",
"maybe-uninit",
]
[[package]]
name = "crossbeam-deque"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
"maybe-uninit",
]
[[package]]
name = "crossbeam-epoch"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"lazy_static",
"maybe-uninit",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-queue"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab6bffe714b6bb07e42f201352c34f51fefd355ace793f9e638ebd52d23f98d2"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
dependencies = [
"autocfg",
"cfg-if",
"lazy_static",
]
[[package]] [[package]]
name = "dbus" name = "dbus"
version = "0.8.3" version = "0.8.3"
@ -143,6 +273,16 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "futures-timer"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c"
dependencies = [
"gloo-timers",
"send_wrapper",
]
[[package]] [[package]]
name = "futures-util" name = "futures-util"
version = "0.3.5" version = "0.3.5"
@ -174,6 +314,19 @@ dependencies = [
"wasi", "wasi",
] ]
[[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]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.13" version = "0.1.13"
@ -198,6 +351,15 @@ version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e" checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
[[package]]
name = "js-sys"
version = "0.3.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce10c23ad2ea25ceca0093bd3192229da4c5b3c0f2de499c1ecac0d98d452177"
dependencies = [
"wasm-bindgen",
]
[[package]] [[package]]
name = "kernel32-sys" name = "kernel32-sys"
version = "0.2.2" version = "0.2.2"
@ -208,6 +370,15 @@ dependencies = [
"winapi-build", "winapi-build",
] ]
[[package]]
name = "kv-log-macro"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ff57d6d215f7ca7eb35a9a64d656ba4d9d2bef114d741dc08048e75e2f5d418"
dependencies = [
"log",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -238,12 +409,27 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "maybe-uninit"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.3.3" version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "memoffset"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4fc2c02a7e374099d4ee95a193111f72d2110197fe200272371758f6c3643d8"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "mio" name = "mio"
version = "0.6.22" version = "0.6.22"
@ -319,6 +505,38 @@ dependencies = [
"winapi 0.3.8", "winapi 0.3.8",
] ]
[[package]]
name = "nix"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363"
dependencies = [
"bitflags",
"cc",
"cfg-if",
"libc",
"void",
]
[[package]]
name = "num-integer"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "num_cpus" name = "num_cpus"
version = "1.13.0" version = "1.13.0"
@ -367,6 +585,18 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "piper"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b0deb65f46e873ba8aa7c6a8dbe3f23cb1bf59c339a81a1d56361dde4d66ac8"
dependencies = [
"crossbeam-utils",
"futures-io",
"futures-sink",
"futures-util",
]
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.17" version = "0.3.17"
@ -460,6 +690,8 @@ checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
name = "rustybar" name = "rustybar"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-std",
"chrono",
"dbus", "dbus",
"dbus-tokio", "dbus-tokio",
"serde", "serde",
@ -474,6 +706,24 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed3d612bc64430efeb3f7ee6ef26d590dce0c43249217bddc62112540c7941e1" checksum = "ed3d612bc64430efeb3f7ee6ef26d590dce0c43249217bddc62112540c7941e1"
[[package]]
name = "scoped-tls-hkt"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2e9d7eaddb227e8fbaaa71136ae0e1e913ca159b86c7da82f3e8f0044ad3a63"
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "send_wrapper"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f638d531eccd6e23b980caf34876660d38e265409d8e99b397ab71eb3612fad0"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.110" version = "1.0.110"
@ -521,6 +771,25 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
[[package]]
name = "smol"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "686c634ad1873fffef6aed20f180eede424fbf3bb31802394c90fd7335a661b7"
dependencies = [
"async-task",
"crossbeam",
"futures-io",
"futures-util",
"nix",
"once_cell",
"piper",
"scoped-tls-hkt",
"slab",
"socket2",
"wepoll-binding",
]
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.3.12" version = "0.3.12"
@ -544,6 +813,16 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "time"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
dependencies = [
"libc",
"winapi 0.3.8",
]
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "0.2.21" version = "0.2.21"
@ -594,12 +873,113 @@ dependencies = [
"rand", "rand",
] ]
[[package]]
name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.9.0+wasi-snapshot-preview1" version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasm-bindgen"
version = "0.2.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c2dc4aa152834bc334f506c1a06b866416a8b6697d5c9f75b9a689c8486def0"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ded84f06e0ed21499f6184df0e0cb3494727b0c5da89534e0fcc55c51d812101"
dependencies = [
"bumpalo",
"lazy_static",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64487204d863f109eb77e8462189d111f27cb5712cc9fdb3461297a76963a2f6"
dependencies = [
"cfg-if",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "838e423688dac18d73e31edce74ddfac468e37b1506ad163ffaf0a46f703ffe3"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3156052d8ec77142051a533cdd686cba889537b213f948cd1d20869926e68e92"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9ba19973a58daf4db6f352eda73dc0e289493cd29fb2632eb172085b6521acd"
[[package]]
name = "web-sys"
version = "0.3.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b72fe77fd39e4bd3eaa4412fd299a0be6b3dfe9d2597e2f1c20beb968f41d17"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "wepoll-binding"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "374fff4ff9701ff8b6ad0d14bacd3156c44063632d8c136186ff5967d48999a7"
dependencies = [
"bitflags",
"wepoll-sys",
]
[[package]]
name = "wepoll-sys"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9082a777aed991f6769e2b654aa0cb29f1c3d615daf009829b07b66c7aff6a24"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.2.8" version = "0.2.8"

View file

@ -6,11 +6,11 @@ edition = "2018"
[dependencies] [dependencies]
async-std = "1.6"
chrono = "0.4"
dbus = "0.8"
dbus-tokio = "0.5"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
uuid = { version = "0.8", features = [ "v4" ] }
tokio = { version = "0.2", features = ["full"] } tokio = { version = "0.2", features = ["full"] }
dbus-tokio = "0.5" uuid = { version = "0.8", features = [ "v4" ] }
dbus = "0.8"

View file

@ -6,6 +6,7 @@ use std::sync::Arc;
use tile::Tile; use tile::Tile;
use tokio; use tokio;
use tokio::sync::mpsc::channel; use tokio::sync::mpsc::channel;
use uuid::Uuid;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -18,13 +19,30 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
panic!("Lost connection to D-Bus: {}", err); panic!("Lost connection to D-Bus: {}", err);
}); });
let (sender, receiver) = channel(1024); let (mut sender, mut receiver) = channel(1024);
let tiles: Vec<Arc<dyn Tile>> = vec![Arc::new(tiles::Time::new(0, sender.clone()))]; let instance = Uuid::new_v4().to_string().into_boxed_str();
let tiles: Vec<Arc<dyn Tile>> = vec![Arc::new(tiles::Time::new(0, sender.clone(), instance))];
for tile in tiles { for tile in &tiles {
tile.spawn(); tile.clone().spawn();
} }
let num_tiles = tiles.len();
tokio::spawn(async move {
let mut blocks = Vec::new();
blocks.resize_with(num_tiles, Default::default);
loop {
let message = receiver.recv().await.unwrap();
if message.sender_id < num_tiles {
blocks[message.sender_id] = Some(message.block);
} else {
eprintln!("Invalid message with sender id {}", message.sender_id);
continue;
}
eprintln!("Current state: {:?}", blocks);
}
});
loop {} loop {}
} }

View file

@ -2,7 +2,7 @@ use serde::Serialize;
use std::sync::Arc; use std::sync::Arc;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
#[derive(Copy, Clone, Serialize)] #[derive(Copy, Clone, Debug, Serialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
pub enum Alignment { pub enum Alignment {
Left, Left,
@ -16,7 +16,7 @@ impl Default for Alignment {
} }
} }
#[derive(Copy, Clone, Serialize)] #[derive(Copy, Clone, Debug, Serialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
pub enum Markup { pub enum Markup {
None, None,
@ -29,33 +29,33 @@ impl Default for Markup {
} }
} }
#[derive(Clone, Serialize, Default)] #[derive(Clone, Serialize, Default, Debug)]
pub struct Block { pub struct Block {
full_text: Box<str>, pub full_text: Box<str>,
short_text: Option<Box<str>>, pub short_text: Option<Box<str>>,
color: Option<Box<str>>, pub color: Option<Box<str>>,
#[serde(rename = "background")] #[serde(rename = "background")]
background_color: Option<Box<str>>, pub background_color: Option<Box<str>>,
#[serde(rename = "border")] #[serde(rename = "border")]
border_color: Option<Box<str>>, pub border_color: Option<Box<str>>,
border_top: Option<u32>, pub border_top: Option<u32>,
border_right: Option<u32>, pub border_right: Option<u32>,
border_bottom: Option<u32>, pub border_bottom: Option<u32>,
border_left: Option<u32>, pub border_left: Option<u32>,
min_width: Option<u32>, pub min_width: Option<u32>,
align: Option<Alignment>, pub align: Option<Alignment>,
name: Box<str>, pub name: Box<str>,
instance: Box<str>, pub instance: Box<str>,
urgent: Option<bool>, pub urgent: Option<bool>,
separator: Option<bool>, pub separator: Option<bool>,
separator_block_width: Option<u32>, pub separator_block_width: Option<u32>,
markup: Option<Markup>, pub markup: Option<Markup>,
} }
#[derive(Clone)] #[derive(Clone, Debug)]
pub struct TileData { pub struct TileData {
sender_id: usize, pub sender_id: usize,
block: Block, pub block: Block,
} }
pub trait Tile: Send { pub trait Tile: Send {

View file

@ -1,34 +1,71 @@
use crate::tile::{Block, Tile, TileData}; use crate::tile::{Block, Tile, TileData};
use async_std::sync::RwLock;
use chrono::prelude::*;
use chrono::DateTime;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::mpsc::Sender; use std::time::Duration;
use tokio::sync::mpsc::{error::SendError, Sender};
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tokio::time; use tokio::time::delay_for;
use uuid::Uuid;
pub struct Time { pub struct Time {
sender_id: usize, sender_id: usize,
sender: Sender<TileData>, sender: RwLock<Sender<TileData>>,
instance: Box<str>, instance: Box<str>,
format: Box<str>,
short_format: Box<str>,
} }
impl Time { impl Time {
pub fn new(sender_id: usize, sender: Sender<TileData>) -> Time { pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Box<str>) -> Time {
let instance = Uuid::new_v4().to_string().into_boxed_str();
Time { Time {
sender_id, sender_id,
sender, sender: RwLock::new(sender),
instance, instance,
format: "%Y-%m-%d %H:%M:%S".into(),
short_format: "%H:%M:%S".into(),
} }
} }
async fn run(&self) {} async fn send(&self, data: TileData) -> Result<(), SendError<TileData>> {
let mut sender = self.sender.write().await;
sender.send(data).await
}
async fn send_time(&self, time: DateTime<Local>) -> Result<(), SendError<TileData>> {
let block = Block {
full_text: time.format(&self.format).to_string().into(),
short_text: Some(time.format(&self.short_format).to_string().into()),
instance: self.instance.clone(),
name: "time".into(),
..Default::default()
};
let data = TileData {
sender_id: self.sender_id,
block,
};
self.send(data).await
}
async fn run(&self) {
let mut time = Local::now();
loop {
self.send_time(time).await;
time = Local::now();
let millis_part = time.naive_local().timestamp_subsec_millis() as u64;
let delay_ms = 1000u64 - millis_part % 1000; // Don't crash if we hit a leap second
delay_for(Duration::from_millis(delay_ms)).await;
}
}
} }
impl Tile for Time { impl Tile for Time {
fn spawn(self: Arc<Self>) -> JoinHandle<()> { fn spawn(self: Arc<Self>) -> JoinHandle<()> {
tokio::spawn(async { tokio::spawn(async move {
let instance = self; let instance = self;
instance.run().await loop {
instance.run().await
}
}) })
} }
} }