sisyphus/scripts/notify.sh

71 lines
1.6 KiB
Bash
Raw Normal View History

2022-04-28 13:43:42 +02:00
#!/usr/bin/env bash
# Show system status in notification, or your own message
2022-06-05 20:20:05 +02:00
# Syntaxis: notify [-vb] [-t <timeout>] [<title> <message>]
2022-04-28 13:43:42 +02:00
2022-06-02 11:44:45 +02:00
# Requirements:
# - brightnessctl
# - pulsemixer
panic () {
>&2 echo "Syntaxis: notify [-vb] [<title> [<message>]]"
exit 1
}
2022-04-28 13:43:42 +02:00
# Get options
2022-06-05 20:20:05 +02:00
while getopts ":bvt:" options; do
2022-04-28 13:43:42 +02:00
case "${options}" in
b)
2022-06-02 11:44:45 +02:00
value=$( brightnessctl | grep -o "[0-9]*%" )
2022-04-28 13:43:42 +02:00
title="Brightness: ${value}"
2022-06-01 21:38:20 +02:00
timeout=2000
2022-06-03 11:20:17 +02:00
category='sysinfo'
2022-04-28 13:43:42 +02:00
;;
v)
# Get volume (don't use pamixer because that is way slower)
value=$(pactl get-sink-volume @DEFAULT_SINK@ | cut -d '/' -f2 | grep -o '[0-9]*%')
2022-06-02 11:44:45 +02:00
# If audio disabled
if [ ! "$( pactl get-sink-mute @DEFAULT_SINK@ )" -eq "Mute: yes" ] ; then
2022-06-02 11:44:45 +02:00
width=0
value="${value} (Disabled)"
fi
2022-04-28 13:43:42 +02:00
title="Volume: ${value:-'0%'}"
2022-06-01 21:38:20 +02:00
timeout=2000
2022-06-03 11:20:17 +02:00
category='sysinfo'
2022-04-28 13:43:42 +02:00
;;
2022-06-05 20:20:05 +02:00
t)
timeout="${OPTARG}"
;;
2022-04-28 13:43:42 +02:00
*)
2022-06-02 11:44:45 +02:00
panic
2022-04-28 13:43:42 +02:00
;;
esac
done
shift $((OPTIND - 1))
2022-06-01 21:38:20 +02:00
# Check arguments
2022-06-02 11:44:45 +02:00
if [ $# -gt 2 ] ; then
panic
elif [ $# -eq 2 ] ; then
2022-06-01 21:38:20 +02:00
title=$1
message=$2
2022-06-02 11:44:45 +02:00
elif [ $# -eq 1 ] ; then
2022-06-01 21:38:20 +02:00
title=$1
fi
# Calculate length of coloured bar.
2022-06-02 11:44:45 +02:00
if [[ "${value}" =~ ^[0-9]+%$ && "${width:--1}" -ne 0 ]] ; then
2022-06-01 21:38:20 +02:00
width=$(grep -o "[0-9]*" <<< "${value}")
fi
# Send message
notify-send "${title}" "${message}" \
-t "${timeout:=5000}" \
2022-06-03 11:20:17 +02:00
-c "${category:=''}" \
2022-06-01 21:38:20 +02:00
-h int:value:"${width:=0}" \
2022-06-07 02:06:46 +02:00
-h string:wired-tag:byMe \
2022-06-02 11:44:45 +02:00
-h string:x-canonical-private-synchronous:byMe # Replace if previous still exists
2022-04-28 13:43:42 +02:00