#!/usr/bin/env zsh setopt rcquotes function print_help { echo "usage: sound-control.sh [-n] [-h] " 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 { if ! ((${notify})); then return fi 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}" \ "${icon}" } function check_number { if ! [[ -z "${1}" ]] && ! [[ "${1}" =~ '^-?[0-9]+%?$' ]]; then echo "error: not a valid number: \"${1}\"" 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