Able to output basic structure
This commit is contained in:
parent
03b06ce14f
commit
49c82ade0b
20
src/main.rs
20
src/main.rs
|
@ -1,3 +1,4 @@
|
||||||
|
pub mod output;
|
||||||
pub mod tile;
|
pub mod tile;
|
||||||
pub mod tiles;
|
pub mod tiles;
|
||||||
|
|
||||||
|
@ -11,7 +12,7 @@ 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>> {
|
||||||
// We can't do much until we have a D-Bus connection so just do it synchronously
|
// 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
|
// Now start listening on our D-Bus connection
|
||||||
tokio::spawn(async {
|
tokio::spawn(async {
|
||||||
|
@ -19,10 +20,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
panic!("Lost connection to D-Bus: {}", err);
|
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 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 {
|
for tile in &tiles {
|
||||||
tile.clone().spawn();
|
tile.clone().spawn();
|
||||||
|
@ -30,18 +31,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
|
||||||
let num_tiles = tiles.len();
|
let num_tiles = tiles.len();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let mut blocks = Vec::new();
|
output::launch(num_tiles, receiver).await.unwrap();
|
||||||
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 {}
|
||||||
|
|
23
src/output.rs
Normal file
23
src/output.rs
Normal 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?;
|
||||||
|
}
|
||||||
|
}
|
16
src/tile.rs
16
src/tile.rs
|
@ -32,23 +32,35 @@ impl Default for Markup {
|
||||||
#[derive(Clone, Serialize, Default, Debug)]
|
#[derive(Clone, Serialize, Default, Debug)]
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
pub full_text: Box<str>,
|
pub full_text: Box<str>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub short_text: Option<Box<str>>,
|
pub short_text: Option<Box<str>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub color: Option<Box<str>>,
|
pub color: Option<Box<str>>,
|
||||||
#[serde(rename = "background")]
|
#[serde(skip_serializing_if = "Option::is_none", rename = "background")]
|
||||||
pub background_color: Option<Box<str>>,
|
pub background_color: Option<Box<str>>,
|
||||||
#[serde(rename = "border")]
|
#[serde(skip_serializing_if = "Option::is_none", rename = "border")]
|
||||||
pub border_color: Option<Box<str>>,
|
pub border_color: Option<Box<str>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub border_top: Option<u32>,
|
pub border_top: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub border_right: Option<u32>,
|
pub border_right: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub border_bottom: Option<u32>,
|
pub border_bottom: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub border_left: Option<u32>,
|
pub border_left: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub min_width: Option<u32>,
|
pub min_width: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub align: Option<Alignment>,
|
pub align: Option<Alignment>,
|
||||||
pub name: Box<str>,
|
pub name: Box<str>,
|
||||||
pub instance: Box<str>,
|
pub instance: Box<str>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub urgent: Option<bool>,
|
pub urgent: Option<bool>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub separator: Option<bool>,
|
pub separator: Option<bool>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub separator_block_width: Option<u32>,
|
pub separator_block_width: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub markup: Option<Markup>,
|
pub markup: Option<Markup>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ impl Time {
|
||||||
async fn run(&self) {
|
async fn run(&self) {
|
||||||
let mut time = Local::now();
|
let mut time = Local::now();
|
||||||
loop {
|
loop {
|
||||||
self.send_time(time).await;
|
self.send_time(time).await.unwrap();
|
||||||
time = Local::now();
|
time = Local::now();
|
||||||
let millis_part = time.naive_local().timestamp_subsec_millis() as u64;
|
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
|
let delay_ms = 1000u64 - millis_part % 1000; // Don't crash if we hit a leap second
|
||||||
|
|
Loading…
Reference in a new issue