~jplatte/ledybar-mod-i3-workspaces

320d53285df91680b8ab953e4ec653136c2fca13 — Jonas Platte 9 months ago
Initial commit
4 files changed, 128 insertions(+), 0 deletions(-)

A .gitignore
A Cargo.lock
A Cargo.toml
A src/main.rs
A  => .gitignore +1 -0
@@ 1,1 @@
/target

A  => Cargo.lock +77 -0
@@ 1,77 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "anyhow"
version = "1.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9a60d744a80c30fcb657dfe2c1b22bcb3e814c1a1e3674f32bf5820b570fbff"

[[package]]
name = "byteorder"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"

[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"

[[package]]
name = "i3ipc"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63f3dac00c473fae88cb3114f35312204469a32ffb20874264a5214d6c8c927e"
dependencies = [
 "byteorder",
 "log",
 "serde",
 "serde_json",
]

[[package]]
name = "itoa"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"

[[package]]
name = "ledybar-mod-i3-workspaces"
version = "0.1.0"
dependencies = [
 "anyhow",
 "i3ipc",
]

[[package]]
name = "log"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
dependencies = [
 "cfg-if",
]

[[package]]
name = "ryu"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "535622e6be132bccd223f4bb2b8ac8d53cda3c7a6394944d3b2b33fb974f9d76"

[[package]]
name = "serde"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399"

[[package]]
name = "serde_json"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da07b57ee2623368351e9a0488bb0b261322a15a6e0ae53e243cbdc0f4208da9"
dependencies = [
 "itoa",
 "ryu",
 "serde",
]

A  => Cargo.toml +9 -0
@@ 1,9 @@
[package]
name = "ledybar-mod-i3-workspaces"
version = "0.1.0"
authors = ["Jonas Platte <jplatte+git@posteo.de>"]
edition = "2018"

[dependencies]
anyhow = "1.0.28"
i3ipc = "0.10.1"

A  => src/main.rs +41 -0
@@ 1,41 @@
use std::io::{self, StdoutLock, Write};

use i3ipc::{reply::Workspace, I3Connection, I3EventListener, Subscription};

fn main() -> anyhow::Result<()> {
    let mut i3conn = I3Connection::connect()?;
    let mut event_listener = I3EventListener::connect()?;
    let initial_workspaces = i3conn.get_workspaces()?.workspaces;

    let _stdout = io::stdout();
    let mut stdout = _stdout.lock();
    print_workspaces(&initial_workspaces, &mut stdout);

    let res = event_listener.subscribe(&[Subscription::Workspace])?;
    assert!(res.success);

    for _ in event_listener.listen() {
        match i3conn.get_workspaces() {
            Ok(reply) => print_workspaces(&reply.workspaces, &mut stdout),
            Err(err) => eprintln!("error in receiving event messages: {}", err),
        }
    }

    Ok(())
}

fn print_workspaces(workspaces: &[Workspace], stdout: &mut StdoutLock) {
    for ws in workspaces {
        let prefix = ws.num.to_string() + ":";
        let stripped_name = if ws.name.starts_with(&prefix) {
            &ws.name[prefix.len()..]
        } else {
            &ws.name[..]
        };

        let classes = if ws.focused { "ws focused" } else { "ws" };
        let _ = write!(stdout, "<div class='{}'>{}</div>", classes, stripped_name);
    }

    let _ = writeln!(stdout);
}