From 49c82ade0b22a769637177bff2f1166d175c0827 Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Sat, 30 May 2020 21:37:17 +0000 Subject: [PATCH] Able to output basic structure --- src/main.rs | 20 +++++--------------- src/output.rs | 23 +++++++++++++++++++++++ src/tile.rs | 16 ++++++++++++++-- src/tiles/time.rs | 2 +- 4 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 src/output.rs diff --git a/src/main.rs b/src/main.rs index 2fde63b..8a817d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +pub mod output; pub mod tile; pub mod tiles; @@ -11,7 +12,7 @@ use uuid::Uuid; #[tokio::main] async fn main() -> Result<(), Box> { // We can't do much until we have a D-Bus connection so just do it synchronously - let (resource, conn) = new_session_sync()?; + let (resource, _conn) = new_session_sync()?; // Now start listening on our D-Bus connection tokio::spawn(async { @@ -19,10 +20,10 @@ async fn main() -> Result<(), Box> { panic!("Lost connection to D-Bus: {}", err); }); - let (mut sender, mut receiver) = channel(1024); + let (sender, receiver) = channel(1024); let instance = Uuid::new_v4().to_string().into_boxed_str(); - let tiles: Vec> = vec![Arc::new(tiles::Time::new(0, sender.clone(), instance))]; + let tiles: Vec> = vec![Arc::new(tiles::Time::new(0, sender, instance))]; for tile in &tiles { tile.clone().spawn(); @@ -30,18 +31,7 @@ async fn main() -> Result<(), Box> { 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); - } + output::launch(num_tiles, receiver).await.unwrap(); }); loop {} diff --git a/src/output.rs b/src/output.rs new file mode 100644 index 0000000..35bb1bb --- /dev/null +++ b/src/output.rs @@ -0,0 +1,23 @@ +use crate::tile::TileData; +use tokio::io::{self, AsyncWriteExt}; +use tokio::sync::mpsc::Receiver; + +pub async fn launch(num_tiles: usize, mut receiver: Receiver) -> io::Result<()> { + let mut stdout = io::stdout(); + stdout.write_all(b"{ \"version\": 1 }\n[").await?; + + 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; + } + let serialized = serde_json::to_vec(&blocks).unwrap(); + stdout.write_all(&serialized).await?; + stdout.write_all(b",\n").await?; + } +} diff --git a/src/tile.rs b/src/tile.rs index ad63592..18d2ad0 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -32,23 +32,35 @@ impl Default for Markup { #[derive(Clone, Serialize, Default, Debug)] pub struct Block { pub full_text: Box, + #[serde(skip_serializing_if = "Option::is_none")] pub short_text: Option>, + #[serde(skip_serializing_if = "Option::is_none")] pub color: Option>, - #[serde(rename = "background")] + #[serde(skip_serializing_if = "Option::is_none", rename = "background")] pub background_color: Option>, - #[serde(rename = "border")] + #[serde(skip_serializing_if = "Option::is_none", rename = "border")] pub border_color: Option>, + #[serde(skip_serializing_if = "Option::is_none")] pub border_top: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub border_right: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub border_bottom: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub border_left: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub min_width: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub align: Option, pub name: Box, pub instance: Box, + #[serde(skip_serializing_if = "Option::is_none")] pub urgent: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub separator: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub separator_block_width: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub markup: Option, } diff --git a/src/tiles/time.rs b/src/tiles/time.rs index 601ec83..53f9a4b 100644 --- a/src/tiles/time.rs +++ b/src/tiles/time.rs @@ -50,7 +50,7 @@ impl Time { async fn run(&self) { let mut time = Local::now(); loop { - self.send_time(time).await; + self.send_time(time).await.unwrap(); 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