Consolidation and delegation of local vars

This commit is contained in:
Xavier Moffett 2025-03-22 19:27:37 -04:00
parent 7b5466dd9e
commit a97434bfcf
Signed by: Sapphirus
GPG key ID: A6C061B2CEA1A7AC
2 changed files with 23 additions and 25 deletions

View file

@ -50,4 +50,4 @@ bindel = SHIFT,XF86AudioLowerVolume, exec, volbash down --bypass
## License
[MIT license](./LICENSE)
[MIT License](./LICENSE)

46
volbash
View file

@ -25,12 +25,15 @@
set -eEo pipefail
SOURCE="@DEFAULT_AUDIO_SINK@"
VERSION="1.0"
OUTPUT="@DEFAULT_AUDIO_SINK@"
SOURCE="@DEFAULT_AUDIO_SOURCE@"
main() {
local action bypass
local sink="$OUTPUT"
local delta=5
local action=
local bypass=
while (($#)); do
case $1 in
@ -38,7 +41,7 @@ main() {
bypass=1
;;
--source | -s)
SOURCE="@DEFAULT_AUDIO_SOURCE@"
sink="$SOURCE"
;;
--delta | -d)
shift
@ -62,55 +65,50 @@ main() {
case $action in
up | down)
adjust "$action" "$delta" "$bypass"
adjust "$delta"
;;
mute | unmute)
toggle "$action"
;;
toggle)
mute | unmute | toggle)
toggle
;;
*)
invalid_option "$action"
invalid_option
;;
esac
}
toggle() {
if [[ -z $1 ]]; then
wpctl set-mute "$SOURCE" toggle
if [[ -z $action ]]; then
wpctl set-mute "$sink" toggle
else
wpctl set-mute "$SOURCE" "$([[ "$1" == "mute" ]] && echo 1 || echo 0)"
wpctl set-mute "$sink" "$([[ "$action" == "mute" ]] && echo 1 || echo 0)"
fi
}
adjust() {
local vol=
local op=
local delta=
local delta vol op
# Check for valid input value
case $2 in '' | *[!0-9]*)
case $1 in '' | *[!0-9]*)
echo "error: Delta value must be a positive integer."
exit 2
;;
esac
# Ascertain the volume level by parsing wpctl's output
vol=$(v="$(wpctl get-volume "$SOURCE")"; echo "${v#* }")
vol=$(v="$(wpctl get-volume "$sink")"; echo "${v#* }")
vol=$([[ ${vol%.*} -gt 0 ]] && echo "${vol%.*}" || echo)${vol#*.}
# Designate an arithmetic operator for the operation
op=$([[ "$1" == "up" ]] && echo "+" || echo "-")
delta="${2}%$op"
op=$([[ "$action" == "up" ]] && echo "+" || echo "-")
delta="${1}%$op"
# We check if the volume increment exceeds 100 and override
# Check if the volume increment exceeds 100 and override
# the delta if the bypass option has not been specified.
if [[ -z $3 ]] && [[ $(("$vol" "$op" "$2")) -gt 100 ]]; then
if [[ -z $bypass ]] && [[ $(("$vol" "$op" "$1")) -gt 100 ]]; then
delta="100%"
fi
wpctl set-volume "$SOURCE" "$delta"
wpctl set-volume "$sink" "$delta"
}
invalid_option() {
@ -125,7 +123,7 @@ invalid_option() {
}
version() {
echo -e "volbash 1.0 - simple wpctl wrapper
echo -e "volbash $VERSION - simple wpctl wrapper
Copyright (C) 2025 Xavier Moffett\n
Licensed under the MIT License\n"
exit