nix-configs/programs/bar-ws-monitor/src/main.rs

54 lines
1.2 KiB
Rust
Raw Normal View History

use core::panic;
use serde::Serialize;
use swayipc::{Connection, Event, EventType, Fallible, Workspace, WorkspaceChange};
fn main() -> Fallible<()> {
let mut con = Connection::new()?;
let mut workspaces: Vec<WsData> = con
.get_workspaces()?
.into_iter()
.map(|ws| ws.into())
.collect();
println!("{}", serde_json::ser::to_string(&workspaces).unwrap());
for ev in con.subscribe([EventType::Workspace])? {
// the lazy/ugly solution!
// we create a new connection and request workspaces again and again and again
// TODO: properly handle events one by one
let mut con = Connection::new()?;
workspaces = con
.get_workspaces()?
.into_iter()
.map(|ws| ws.into())
.collect();
println!("{}", serde_json::ser::to_string(&workspaces).unwrap());
}
Ok(())
}
#[derive(Debug, Serialize)]
struct WsData {
name: String,
focused: bool,
urgent: bool,
}
impl From<Workspace> for WsData {
fn from(
Workspace {
name,
focused,
urgent,
..
}: Workspace,
) -> Self {
WsData {
name,
focused,
urgent,
}
}
}