Able to output basic structure

This commit is contained in:
Artemis Tosini 2020-05-30 21:37:17 +00:00
parent 03b06ce14f
commit 49c82ade0b
Signed by: artemist
GPG key ID: EE5227935FE3FF18
4 changed files with 43 additions and 18 deletions

View file

@ -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<dyn std::error::Error>> {
// 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<dyn std::error::Error>> {
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<Arc<dyn Tile>> = vec![Arc::new(tiles::Time::new(0, sender.clone(), instance))];
let tiles: Vec<Arc<dyn Tile>> = 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<dyn std::error::Error>> {
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 {}

23
src/output.rs Normal file
View file

@ -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<TileData>) -> 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?;
}
}

View file

@ -32,23 +32,35 @@ impl Default for Markup {
#[derive(Clone, Serialize, Default, Debug)]
pub struct Block {
pub full_text: Box<str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub short_text: Option<Box<str>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub color: Option<Box<str>>,
#[serde(rename = "background")]
#[serde(skip_serializing_if = "Option::is_none", rename = "background")]
pub background_color: Option<Box<str>>,
#[serde(rename = "border")]
#[serde(skip_serializing_if = "Option::is_none", rename = "border")]
pub border_color: Option<Box<str>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub border_top: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub border_right: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub border_bottom: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub border_left: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub min_width: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub align: Option<Alignment>,
pub name: Box<str>,
pub instance: Box<str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub urgent: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub separator: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub separator_block_width: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub markup: Option<Markup>,
}

View file

@ -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