Skip to content

Instantly share code, notes, and snippets.

@stelcodes
Last active September 29, 2022 05:09
Show Gist options
  • Save stelcodes/7d9136a5839b645b6cd5bc829a9fe541 to your computer and use it in GitHub Desktop.
Save stelcodes/7d9136a5839b645b6cd5bc829a9fe541 to your computer and use it in GitHub Desktop.
babashka pactl cycle default sink
#!/usr/bin/env bb
(ns cycle-sink
(:require [babashka.process :as p]
[clojure.pprint :as pp]
[clojure.string :as str]))
(defmacro debug [sym] `(do (println ~(keyword sym)) (pp/pprint ~sym) (println)))
(def sinks
(let [default-sink-name (->> (p/sh ["pactl" "info"])
:out
(re-find #"Default Sink: (\S+)")
second)]
(->> (p/sh ["pactl" "list" "short" "sinks"])
:out
str/split-lines
(map #(rest (re-find #"^([0-9]+)\t(\S+)" %)))
(map #(zipmap [:number :name] %))
(map #(assoc % :default (= (:name %) default-sink-name))))))
(debug sinks)
(assert (some :default sinks) "Default sink not found")
(def next-sink (->> sinks
(sort-by :number)
cycle
(reduce (fn [flag sink]
(cond flag (reduced sink)
(:default sink) :default-sink
:else nil))
nil)))
(debug next-sink)
(p/sh ["pactl" "set-default-sink" (:number next-sink)])
(println "Changed default sink to" (:name next-sink))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment