From 28fcdddcca7cdb762e4efea8907189e986cec6f8 Mon Sep 17 00:00:00 2001 From: akulij Date: Mon, 9 Sep 2024 17:41:54 +0300 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 16 +++++++++++++ Cargo.toml | 7 ++++++ run.sh | 8 +++++++ src/main.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100755 run.sh create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..541e493 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "libc" +version = "0.2.157" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "374af5f94e54fa97cf75e945cce8a6b201e88a1a07e688b47dfd2a59c66dbd86" + +[[package]] +name = "mybar" +version = "0.1.0" +dependencies = [ + "libc", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..467cd77 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "mybar" +version = "0.1.0" +edition = "2021" + +[dependencies] +libc = "*" diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..2d66fa6 --- /dev/null +++ b/run.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +scriptDir=$(dirname -- "$(readlink -f -- "$BASH_SOURCE")") + +hyprctl activeworkspace -j | jq .id # set initial value +# ./target/debug/mybar workspace\> | awk -F '>>' '{print $2}' +cd "$scriptDir" +./target/debug/mybar workspace\> split diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..2ca62a6 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,65 @@ +use std::fmt::format; +use std::os::fd::AsFd; +use std::{default, env, mem}; +use std::os::unix::net::UnixStream; +use std::io::{prelude::*, stdout, BufReader}; + +use libc::{signal, SIGPIPE, SIG_DFL, SIG_IGN}; + +fn main() { + let args : Vec = env::args().collect(); + unsafe {signal(SIGPIPE, SIG_IGN);} + + let mut filter_mode = false; + let mut split_mode = false; + if args.len() > 1 { + filter_mode = true; + } + for arg in &args[1..] { + if arg == "split" { + split_mode = true; + } + } + + let mut stream = UnixStream::connect(get_hypr_socket_path()).unwrap(); + + //let mut buf = [0u8; 1024]; + let reader = BufReader::new(stream); + if !filter_mode { + for line in reader.lines() { + write!(stdout(), "{}\n", line.unwrap()).unwrap(); + stdout().flush().unwrap(); + } + } else { + for line in reader.lines() { + let mut line = line.unwrap(); + let contains: bool = { + let mut contains = false; + for filter in &args[1..] { + // if line.contains(filter) { + if line.starts_with(filter) { + contains = true; + break; + } + }; + contains + }; + if contains { + //write!(stdout(), "{}\n", line).unwrap(); + //stdout().write(line.as_bytes()).unwrap(); + //stdout().write_fmt(format_args!("{}\n", line)).unwrap(); + //stdout().flush().unwrap(); + let mut idx = 0; + if split_mode { + idx = line.find(">>").unwrap(); + } + println!("{}", line.split_off(idx+2)); + } + } + } +} + +fn get_hypr_socket_path() -> String { + let instance = env::var("HYPRLAND_INSTANCE_SIGNATURE").unwrap(); + format!("/tmp/hypr/{instance}/.socket2.sock") +}