random-scripts/sound-control.sh

98 lines
2.3 KiB
Bash
Raw Permalink Normal View History

2024-08-24 16:13:26 -07:00
#!/usr/bin/env zsh
setopt rcquotes
function print_help {
echo "usage: sound-control.sh [-n] [-h] <command>"
echo 'Options:'
echo ' -h: print this message, then exit'
echo ' -n: don''t notify the volume'
echo 'Commands:'
echo ' up: increase the volume (default: 5%)'
echo ' down: decrease the volume (default: 5%)'
echo ' set: set the volume'
echo ' mute: toggle the mute with no argument, or set it with one'
echo ' help: print this message, then exit'
echo ' notify: notify the volume, then exit (do nothing with -n)'
}
let notify=1
function notify_vol {
2024-08-24 16:49:55 -07:00
((${notify})) || return
2024-08-24 16:13:26 -07:00
let vol="$(pamixer --get-volume)"
local icon
if [[ "$(pamixer --get-mute)" == 'true' ]]; then
icon='󰸈'
elif (( ${vol} > 50 )); then
icon='󰕾'
elif (( ${vol} >= 0 )); then
icon='󰖀'
else
icon=''
fi
notify-send -h string:x-canonical-private-synchronous:sound-control-sh \
-t 4000 \
-u normal \
-a "Volume" \
-h int:value:"${vol}" \
2024-08-24 16:21:12 -07:00
"${icon} ${vol}%"
2024-08-24 16:13:26 -07:00
}
function check_number {
2024-08-24 16:21:12 -07:00
if ! [[ -z "${1}" ]] && ! [[ "${1}" =~ '^[0-9]+%?$' ]]; then
echo "error: not a valid value: \"${1}\""
2024-08-24 16:13:26 -07:00
exit 1
fi
}
if [[ "${1}" == '-n' ]]; then
shift 1
notify=0
fi
case "${1}" in
-h|help|'')
print_help
;;
up)
check_number "${2}"
pamixer -i "${${2:-5}%'%'}"
notify_vol
;;
down)
check_number "${2}"
pamixer -d "${${2:-5}%'%'}"
notify_vol
;;
set)
(( ${#} >= 2 )) || { echo 'error: no input value'; exit 1 }
check_number "${2}"
pamixer --set-volume "${2%'%'}"
notify_vol
;;
mute)
if (( ${#} >= 2 )); then
if [[ "${2:l}" =~ '^y(es?)?$' ]]; then
pamixer -m
elif [[ "${2:l}" =~ '^no?$' ]]; then
pamixer -u
else
echo "error: must be one of 'yes' or 'no': \"${2}\""
exit 1
fi
else
pamixer -t
fi
notify_vol
;;
notify)
notify_vol
;;
*)
echo "error: unknown command: \"${1}\""
print_help
exit 1
;;
esac