Skip to content

Instantly share code, notes, and snippets.

@bennyandresen
Created April 17, 2022 17:44
Show Gist options
  • Select an option

  • Save bennyandresen/500cd5c50d62eb23a25829d93e39d7ae to your computer and use it in GitHub Desktop.

Select an option

Save bennyandresen/500cd5c50d62eb23a25829d93e39d7ae to your computer and use it in GitHub Desktop.
(ns icu.lambda.bbterm.app
(:require
[clojure.string :as str]
[babashka.process :refer [process check]])
(:gen-class))
(defn current-activity-id []
(->
(process ["qdbus"
"org.kde.ActivityManager"
"/ActivityManager/Activities"
"org.kde.ActivityManager.Activities.CurrentActivity"]
{:out :string})
check
:out
str/trim))
(defn current-activity-name [id]
(->
(process ["qdbus"
"org.kde.ActivityManager"
"/ActivityManager/Activities"
"org.kde.ActivityManager.Activities.ActivityName"
id]
{:out :string})
check
:out
str/trim))
(defn kitty-tmux-group-name [group]
(let [cur-act (current-activity-name (current-activity-id))]
(str (if (not= cur-act "") (str "[" cur-act "] " group)
group))))
(defn kitty-tmux-session-name [group num]
(str group "-" num))
(defn net-desktop-names []
(let [out (-> (process ["xprop" "-notype" "-root" "_NET_DESKTOP_NAMES"]
{:out :string})
check
:out
str/trim
(str/replace "\"" ""))
names (-> out (str/split #" = ") last (str/split #", "))]
names))
(defn get-desktop []
(-> (process ["xdotool"
"get_desktop"]
{:out :string})
check
:out
str/trim
Integer/parseInt))
(defn desktop-name
([]
(desktop-name (get-desktop)))
([index]
(nth (net-desktop-names) index)))
(comment
(desktop-name)
)
(defn tmux-session-name [s]
(-> (str/split s #":")
first))
(defn tmux-group-name [s]
(-> (re-find #"\(group (.*?)\)" s)
last))
(defn tmux-attached? [s]
(str/includes? s "(attached)"))
(comment
(let [example-string "gamma-1: 1 windows (created Mon Dec 27 15:57:33 2021) (group (FOO) gamma) (attached)
"]
#_(tmux-session-name example-string)
(tmux-group-name example-string)
#_(tmux-attached? example-string))
)
(defn tmux-session-meta-data [s]
(let [session (tmux-session-name s)
group (tmux-group-name s)
attached? (if (tmux-attached? s) true false)]
{:session session, :group group, :attached? attached?}))
(comment
(let [example-string "(FOO) gamma-1: 1 windows (created Mon Dec 27 15:57:33 2021) (group gamma) (attached)
"]
(tmux-session-meta-data example-string))
)
(defn tmux-running? []
(let [proc (process ["tmux" "list-sessions"])]
(and true
(when (try (-> proc
check)
(catch Exception _
nil))
true))))
(comment
(tmux-running?)
)
(defn tmux-sessions []
(when (tmux-running?)
(let [out (-> (process ["tmux" "list-sessions"]
{:out :string})
check
:out
str/trim
str/split-lines)]
(map tmux-session-meta-data out))))
(comment
(tmux-sessions)
)
(defn tmux-session-exist? [name]
(let [sessions (tmux-sessions)]
(when sessions
(let [filtered (filter #(= name (:session %)) sessions)]
(if (> (count filtered) 0) filtered nil)))))
(comment
(tmux-session-exist? "gamma-2")
)
(defn tmux-session-attached? [name]
(let [sessions (tmux-sessions)]
(when sessions
(let [filtered (filter #(and (:attached? %)
(= name (:session %)))
sessions)]
(if (> (count filtered) 0) filtered nil)))))
(comment
(tmux-session-attached? "gamma-2")
)
(defn tmux-new-window! [session]
(-> (process ["tmux" "new-window" "-t" session]
{:out :string})
check
:out))
(comment
(tmux-new-window! "gamma-1")
)
(defn kitty-tmux-attach! [session]
(-> (process ["kitty" "tmux" "attach" "-t" session])
check
:out))
(comment
(kitty-tmux-attach! "gamma-1")
)
(defn kitty-tmux-new! [group session]
(-> (process ["kitty"
"tmux"
"new"
"-t"
group
"-s"
session])))
(defn notify [title text]
(-> (process ["notify-send"
"-a" title
"-t" "500"
text])))
(def max-terms 4)
(defn kitty-tmux-dwim [desktop]
(let [group (kitty-tmux-group-name desktop)]
(doseq [i (range 1 (inc max-terms))
:let [session-name (kitty-tmux-session-name group i)]]
(cond
(not (tmux-session-exist? session-name))
(do
(kitty-tmux-new! group session-name)
(when-not (= i 1)
(Thread/sleep 500)
(tmux-new-window! session-name))
(System/exit 0))
(and (tmux-session-exist? session-name)
(not (tmux-session-attached? session-name)))
(do
(kitty-tmux-attach! session-name)
(System/exit 0))
:else
(when (= i max-terms)
(notify "kitty/tmux" "Maximum number of terminals already open")
(System/exit 0))))))
(defn -main [& _args]
(kitty-tmux-dwim (desktop-name)))
#_(-main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment