aboutsummaryrefslogtreecommitdiff
path: root/.local
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2021-03-04 19:55:23 +0100
committerPatrick Spek <p.spek@tyil.nl>2021-08-14 12:01:14 +0200
commit0d6f7705205343340d5020cf3c9c08f6720590ac (patch)
treec6879524da2433ff6b2cfeb95aac3b553ae96f50 /.local
parent704b5e0342dc6b0ab513d0feba126309a18fb682 (diff)
Add vol utility
Diffstat (limited to '.local')
-rwxr-xr-x.local/bin/vol109
1 files changed, 109 insertions, 0 deletions
diff --git a/.local/bin/vol b/.local/bin/vol
new file mode 100755
index 0000000..9df37b6
--- /dev/null
+++ b/.local/bin/vol
@@ -0,0 +1,109 @@
+#!/bin/sh
+
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
+# details.
+
+main()
+{
+ # Handle opts
+ while getopts ":h" opt
+ do
+ case "$opt" in
+ h) usage && exit 0 ;;
+ *)
+ printf "Invalid option passed: %s\n" "$OPTARG" >&2
+ ;;
+ esac
+ done
+
+ shift $(( OPTIND - 1 ))
+
+ [ $# -lt 1 ] && usage && exit 1
+
+ case "$1" in
+ inc)
+ amixer set Master ${2:-5}%+ > /dev/null
+ notify="Level increased to $(volume_level)%"
+ ;;
+ dec)
+ amixer set Master ${2:-5}%- > /dev/null
+ notify="Level decreased to $(volume_level)%"
+ ;;
+ set)
+ amixer set Master $2% > /dev/null
+ notify="Level set to $(volume_level)%"
+ ;;
+ on)
+ amixer set Master on > /dev/null
+ notify="Unmuted at $(volume_level)%"
+ ;;
+ off)
+ amixer set Master off > /dev/null
+ notify="Muted"
+ ;;
+ toggle)
+ case "$(volume_state)" in
+ on) exec $0 off ;;
+ off) exec $0 on ;;
+ esac
+ ;;
+ esac
+
+ notify-send -n 19269 -s -i "$(volume_icon)" "Volume" "$notify"
+}
+
+volume_level()
+{
+ amixer get Master | awk '/Left:/ { print $5 }' | head -n 1 | tr -d '[]%'
+}
+
+volume_state()
+{
+ amixer get Master | awk '/Left:/ { print $6 }' | head -n 1 | tr -d '[]'
+}
+
+volume_icon()
+{
+ [ "$(volume_state)" = "off" ] && printf "audio-volume-muted-panel" && return
+ [ $(volume_level) -lt 1 ] && printf "audio-volume-low-zero-panel" && return
+ [ $(volume_level) -lt 30 ] && printf "audio-volume-low-panel" && return
+ [ $(volume_level) -lt 70 ] && printf "audio-volume-medium-panel" && return
+
+ printf "audio-volume-high-panel"
+}
+
+usage()
+{
+ cat <<EOF
+Usage:
+ ${0##*/} -h
+ ${0##*/} inc [level]
+ ${0##*/} dec [level]
+ ${0##*/} set <level>
+ ${0##*/} on
+ ${0##*/} off
+ ${0##*/} toggle
+
+Alter the current volume level or state, and add in a notification.
+
+Subcommands:
+ inc Increase the volume by level, defaults to 5.
+ dec Decrease the volume by level, defaults to 5.
+ set Set the volume to level.
+ on Unmute.
+ off Mute.
+ toggle Toggle between muted and unmuted state.
+
+Options:
+ -h Show this help text and exit.
+EOF
+}
+
+main "$@"