From b47d0f918460692a99bcb25951871be29075feef Mon Sep 17 00:00:00 2001 From: Valerie Date: Tue, 20 Jun 2023 13:02:12 -0400 Subject: [PATCH] cleaned up code, updated version and README --- Cargo.toml | 2 +- README.md | 3 +++ src/main.rs | 35 +++++++++++++++++++++-------------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9d2f9e4..253a1cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "i3-sec" -version = "0.0.1" +version = "0.0.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 4921cef..dd409c7 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,9 @@ The workspace numbers emitted will have the `ws-btn` class, `focused` and `urgent` classes where appropriate, and an `onclick` handler that switches to the workspace it represents. +The program will also issue commands to EWW to update the `WM_MODE` variable with +the current i3 mode. + ## Libraries - [i3ipc](https://crates.io/crates/i3ipc) — handles IPC to i3 diff --git a/src/main.rs b/src/main.rs index 2d8771d..cddd084 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,36 +10,40 @@ use i3ipc::{ event::{Event, ModeEventInfo} }; - +/// builds the i3 workspace widget and prints it pub fn build_widget(connection: &mut I3Connection) { + // open base element let base = "(box :class \"workspace\" :orientation \"h\" :spacing 2 :space-evenly false "; let mut output = base.to_string(); + + // get workspaces from IPC let reply = connection.get_workspaces(); if reply.is_err() { exit(1); } let workspaces = reply.ok().unwrap(); + + // loop to build elements for workspaces for ws in workspaces.workspaces { - let mut element = String::from("(button :vexpand true :class \""); - + // build classes let mut classes = String::from("ws-btn "); - if ws.focused { - classes += "focused "; - } - if ws.urgent { - classes += "urgent "; - } - element += &(classes + "\" :onclick \"i3-msg workspace "); - - element += &(ws.num.to_string() + "\" \""); - element += &(ws.num.to_string() + "\")"); + if ws.focused { classes += "focused "; } + if ws.urgent { classes += "urgent "; } + // build workspace number + let ws_num = ws.num.to_string(); + // build element yuck + let element = format!("(button :vexpand true :class \"{classes}\" :onclick \"i3-msg workspace {ws_num}\" \"{ws_num}\")"); + // ... and add to output output += &element; } - println!("{})", output); + // ... and emit! + println!("{output})"); } +/// issues a command for eww to update the WM_MODE variable on +/// i3 mode change. pub fn set_mode(e: ModeEventInfo) { let mut cmd = Command::new("eww"); let mode_str = String::from("WM_MODE=") + &e.change; @@ -48,9 +52,12 @@ pub fn set_mode(e: ModeEventInfo) { } fn main() { + // open IPC let mut connection = I3Connection::connect().unwrap(); + // build initial widget build_widget(&mut connection); + // and await workspace and mode events effectively forever let mut listener = I3EventListener::connect().unwrap(); let subs = [Subscription::Workspace, Subscription::Mode]; listener.subscribe(&subs).unwrap();