random-scripts/select-sound-output.sh

29 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env zsh
local info="$(pactl list sinks)"
local default_name="$(pactl info | grep -Po '(?<=Default Sink: ).+')"
local ids=(${(@f)"$(echo "${info}" | grep -Po '(?<=Sink #).+')"})
local names=(${(@f)"$(echo "${info}" | grep -Po '(?<=Name: ).+')"})
local descs=(${(@f)"$(echo "${info}" | grep -Po '(?<=Description: ).+')"})
local default_id=""
local query_string=""
for ((i = 1; i <= "${#ids}"; ++i)); do
if [[ "${names[${i}]}" == "${default_name}" ]]; then
default_id="${ids[${i}]}"
fi
query_string+="${ids[${i}]}: ${descs[${i}]} (${names[${i}]})\n"
done
[[ -v WAYLAND_DISPLAY ]] \
&& prompt_cmd=("fuzzel" "-dp" "${default_id}> ") \
|| prompt_cmd=("dmenu" "-p" "${default_id}:")
local choice
choice="$(echo "${query_string%"\n"}" | ${prompt_cmd})"
if (( "${?}" != 0 )); then
exit
fi
local selected_id="$(echo "${choice}" | grep -Eo '^[0-9]+')"
pactl set-default-sink "${selected_id}"
local selected_name=$(echo "${choice}" \
| cut -c "$(("${#selected_id}" + 3))-" \
| grep -Po '^.+(?= \()')
notify-send 'Output Device' "${selected_name} (${selected_id})"