(import json)
(defn item-with-id [id items]
(find (fn [item] (= (item "id") id)) items))
(defn contains? [id node]
(if (= node :null)
false
(or (= (node "id") id)
(contains? id (node "firstChild"))
(contains? id (node "secondChild")))))
(defn not-focused [focused-id node-a node-b]
(if (contains? focused-id node-a) node-b node-a))
(defn fully-unhidden
"Gets the first subnode with all children unhidden"
[node]
(match node
:null nil
{"firstChild" {"hidden" false} "secondChild" {"hidden" false}} node
{"firstChild" {"hidden" true} "secondChild" second} (fully-unhidden second)
{"firstChild" first "secondChild" {"hidden" true}} (fully-unhidden first)))
(defn toggle-hidden [node]
(os/execute ["bspc" "node" (node "id") "--flag" "hidden"]))
(def data
(with [reader (file/open "state.json")] (file/read reader :all)))
(def state (json/decode data))
(def monitor
(item-with-id (state "focusedMonitorId") (state "monitors")))
(def desktop
(item-with-id (monitor "focusedDesktopId") (monitor "desktops")))
(def {"firstChild" first "secondChild" second} (desktop "root"))