Skip to content

Instantly share code, notes, and snippets.

@conor-f
Created June 19, 2022 13:15
Show Gist options
  • Save conor-f/c0a3178b33f74cc7725f821c261e0576 to your computer and use it in GitHub Desktop.
Save conor-f/c0a3178b33f74cc7725f821c261e0576 to your computer and use it in GitHub Desktop.
Wrapper around notify-send to display desktop notifications when long-running tasks complete.
#!/bin/bash
## tellme
# tellme displays a basic desktop notification using notify-send to be used when a long
# running task completes. It supports one optional argument, -l, which will show a "loud"
# notification. This depends on notify-send provided by libnotify.
# This script can be run to simply alert or to show the output of the commands:
# $ sleep 10 && tellme
# $ echo "I show on the notification" | tellme
notify () {
if [ -z "$STD_IN" ]; then
title=" "
message="Your command has finished!"
else
title="Your command has finished with output:";
message="$(echo -e \"$STD_IN\" | head -n 10)"
fi
# Loud will force you to click the notification away, and show bold text.
if [ "${is_loud}" ]; then
notify-send -t 99999999 "$title" "<span color='#DD2222' font='26px'><b>$message</b></span>";
else
notify-send -t 5000 "$title" "<span font='16px'>$message</span>";
fi
}
# Checks if stdin contains anything. This is to use tellme as ... && tellme or ... | tellme
if [ -t 0 ]; then
:
else
STD_IN="$(</dev/stdin)"
echo -e "$STD_IN"
fi
# Takes one optional command line param: -l for loud which makes the notification need to be clicked away
while getopts "l" o; do
case "${o}" in
l)
is_loud=1
;;
esac
done
shift $((OPTIND-1))
notify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment