cleaned up code, updated version and README

This commit is contained in:
Valerie Wolfe 2023-06-20 13:02:12 -04:00
parent f8ff9425a2
commit b47d0f9184
3 changed files with 25 additions and 15 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "i3-sec" name = "i3-sec"
version = "0.0.1" version = "0.0.2"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -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 `urgent` classes where appropriate, and an `onclick` handler that switches to the
workspace it represents. workspace it represents.
The program will also issue commands to EWW to update the `WM_MODE` variable with
the current i3 mode.
## Libraries ## Libraries
- [i3ipc](https://crates.io/crates/i3ipc) — handles IPC to i3 - [i3ipc](https://crates.io/crates/i3ipc) — handles IPC to i3

View file

@ -10,36 +10,40 @@ use i3ipc::{
event::{Event, ModeEventInfo} event::{Event, ModeEventInfo}
}; };
/// builds the i3 workspace widget and prints it
pub fn build_widget(connection: &mut I3Connection) { pub fn build_widget(connection: &mut I3Connection) {
// open base element
let base = "(box :class \"workspace\" :orientation \"h\" :spacing 2 :space-evenly false "; let base = "(box :class \"workspace\" :orientation \"h\" :spacing 2 :space-evenly false ";
let mut output = base.to_string(); let mut output = base.to_string();
// get workspaces from IPC
let reply = connection.get_workspaces(); let reply = connection.get_workspaces();
if reply.is_err() { if reply.is_err() {
exit(1); exit(1);
} }
let workspaces = reply.ok().unwrap(); let workspaces = reply.ok().unwrap();
// loop to build elements for workspaces
for ws in workspaces.workspaces { for ws in workspaces.workspaces {
let mut element = String::from("(button :vexpand true :class \""); // build classes
let mut classes = String::from("ws-btn "); let mut classes = String::from("ws-btn ");
if ws.focused { if ws.focused { classes += "focused "; }
classes += "focused "; if ws.urgent { classes += "urgent "; }
} // build workspace number
if ws.urgent { let ws_num = ws.num.to_string();
classes += "urgent ";
}
element += &(classes + "\" :onclick \"i3-msg workspace ");
element += &(ws.num.to_string() + "\" \"");
element += &(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; 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) { pub fn set_mode(e: ModeEventInfo) {
let mut cmd = Command::new("eww"); let mut cmd = Command::new("eww");
let mode_str = String::from("WM_MODE=") + &e.change; let mode_str = String::from("WM_MODE=") + &e.change;
@ -48,9 +52,12 @@ pub fn set_mode(e: ModeEventInfo) {
} }
fn main() { fn main() {
// open IPC
let mut connection = I3Connection::connect().unwrap(); let mut connection = I3Connection::connect().unwrap();
// build initial widget
build_widget(&mut connection); build_widget(&mut connection);
// and await workspace and mode events effectively forever
let mut listener = I3EventListener::connect().unwrap(); let mut listener = I3EventListener::connect().unwrap();
let subs = [Subscription::Workspace, Subscription::Mode]; let subs = [Subscription::Workspace, Subscription::Mode];
listener.subscribe(&subs).unwrap(); listener.subscribe(&subs).unwrap();