Last active
January 27, 2026 03:36
-
-
Save neeasade/26455b507027c5c05279f02c4260e91f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bb | |
| (ns example.socket | |
| (:require [clojure.java.io :as io]) | |
| (:import | |
| java.net.UnixDomainSocketAddress | |
| java.net.StandardProtocolFamily | |
| [java.nio.channels ServerSocketChannel SocketChannel] | |
| [java.nio ByteBuffer] | |
| [java.nio.channels SocketChannel] | |
| [java.net InetSocketAddress])) | |
| (def sockaddr (UnixDomainSocketAddress/of | |
| (-> (doto (io/file | |
| (System/getenv "NIRI_SOCKET") | |
| ;; "/tmp/sock" | |
| )) | |
| str))) | |
| (def ch (SocketChannel/open StandardProtocolFamily/UNIX)) | |
| (.connect ch sockaddr) | |
| (defn read-line-from-channel [^SocketChannel channel] | |
| (let [buffer (ByteBuffer/allocate 1) | |
| sb (StringBuilder.)] | |
| (loop [] | |
| (let [bytes-read (.read channel buffer)] | |
| (when (pos? bytes-read) | |
| (.flip buffer) | |
| (let [b (.get buffer)] | |
| (.clear buffer) | |
| (if (= b (byte \newline)) | |
| (str sb) | |
| (do | |
| (.append sb (char b)) | |
| (recur))))))))) | |
| (defn write-to-channel [^SocketChannel channel ^String message] | |
| (let [buffer (ByteBuffer/wrap (.getBytes message))] | |
| (.write channel buffer) | |
| (.clear buffer))) | |
| (defn do-command [cmd] | |
| ;; (write-to-channel ch "\"FocusedWindow\"\n") | |
| (write-to-channel ch cmd) | |
| (read-line-from-channel ch)) | |
| (do | |
| (do-command "{\"Action\":{\"SwitchPresetWindowWidth\":{\"id\":null}}}\n") | |
| (Thread/sleep 50) | |
| (do-command "{\"Action\":{\"CenterWindow\":{\"id\":null}}}\n")) | |
| ;; $ socat STDIO UNIX-LISTEN:temp.sock | |
| ;; # then, in a different terminal: | |
| ;; $ env NIRI_SOCKET=./temp.sock niri msg action focus-workspace 2 | |
| ;; # then, look in the socat terminal: | |
| ;; {"Action":{"FocusWorkspace":{"reference":{"Index":2}}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment