commit 5a4a2b7f0999acad18e874e7363b6a62ebbd3f9f Author: Valerie Date: Tue May 2 17:36:42 2023 -0400 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..07a1e39 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +Cargo.lock + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c42460c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "i3-sec" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +i3ipc = "0.10.1" + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..2d8771d --- /dev/null +++ b/src/main.rs @@ -0,0 +1,64 @@ +use std::process::{ + Command, + exit +}; + +use i3ipc::{ + I3Connection, + I3EventListener, + Subscription, + event::{Event, ModeEventInfo} +}; + + +pub fn build_widget(connection: &mut I3Connection) { + let base = "(box :class \"workspace\" :orientation \"h\" :spacing 2 :space-evenly false "; + let mut output = base.to_string(); + let reply = connection.get_workspaces(); + if reply.is_err() { + exit(1); + } + let workspaces = reply.ok().unwrap(); + for ws in workspaces.workspaces { + let mut element = String::from("(button :vexpand true :class \""); + + 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() + "\")"); + + output += &element; + } + + println!("{})", output); +} + +pub fn set_mode(e: ModeEventInfo) { + let mut cmd = Command::new("eww"); + let mode_str = String::from("WM_MODE=") + &e.change; + cmd.args(["update", &mode_str]); + cmd.output().ok(); +} + +fn main() { + let mut connection = I3Connection::connect().unwrap(); + build_widget(&mut connection); + + let mut listener = I3EventListener::connect().unwrap(); + let subs = [Subscription::Workspace, Subscription::Mode]; + listener.subscribe(&subs).unwrap(); + for event in listener.listen() { + match event.unwrap() { + Event::WorkspaceEvent(_) => build_widget(&mut connection), + Event::ModeEvent(e) => set_mode(e), + _ => unreachable!() + } + } +}