znotif: cleanup and fixes

This commit is contained in:
zawz 2020-10-23 10:01:33 +02:00
parent c28b95ffae
commit 7325ba40bb

View file

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/sh
_arg_t=2 _arg_t=2
@ -6,25 +6,20 @@ fname=$(basename "$0")
usage () { usage () {
echo "$fname [options] [command] echo "$fname [options] [command]
Sends notification when the given command finished executing Sends notification when the given command finished executing
Options:
-h Show this message then exit Options:
-t <sec> Time the notification stays. By default 2 -h Show this message then exit
-T <title> Notification title -t <sec> Time the notification stays. By default 2
-m <string> Displays this message when finished. Variable resolution on -T <title> Notification title
> Default message is '<command> finished' -m <string> Displays this message when finished. Variable resolution on
-y Display a yes/no prompt instead. Time doesn't apply" > Default message is '<command> finished'
-y Display a yes/no prompt instead. Time doesn't apply"
} }
warning () { error() {
if [ ! -n "$_opt_w" ] ; then printf "\033[1;31m%s\033[0m\n" "$1" >&2
printf "\033[0;33m$1\033[0m\n" >&2 return 1
fi
} }
error () {
printf "\033[1;31m$1\033[0m\n" >&2
}
_args=""
# $1 = message , $2 = title , $3 = time # $1 = message , $2 = title , $3 = time
notify () { notify () {
@ -34,7 +29,7 @@ notify () {
elif which notify-send >/dev/null elif which notify-send >/dev/null
then then
notify-send -t "$3" "$2" "$1" notify-send -t "$3" "$2" "$1"
else echo "No supported notification" >&2 && return 1 else error "No supported notification"
fi fi
} }
@ -45,26 +40,21 @@ yesno () {
elif which zenity elif which zenity
then then
zenity --question --text="$1" --title="$2" zenity --question --text="$1" --title="$2"
else "No supported prompt" else error "No supported prompt"
fi fi
} }
# read options # read options
while getopts ":hym:T:t:" opt; unset opt_y opt_w
while getopts ":hywm:T:t:" opt;
do do
case $opt in case $opt in
h) h) usage && exit 1 ;;
usage y) opt_y=y ;;
exit 1 m) message=$OPTARG ;;
;;
y) _opt_y=y ;;
m)
[ ! -n "$OPTARG" ] && { error "m needs an argument" ; exit 2; }
message=$OPTARG
_opt_m=y
;;
T) title=$OPTARG ;; T) title=$OPTARG ;;
t) _arg_t=$OPTARG ;; t) _arg_t=$OPTARG ;;
w) opt_w=y ;;
\?) echo "Uknown option: $OPTARG" && usage && exit 2 ;; \?) echo "Uknown option: $OPTARG" && usage && exit 2 ;;
esac esac
done done
@ -73,15 +63,15 @@ shift $((OPTIND-1))
if [ $# -gt 0 ] if [ $# -gt 0 ]
then then
[ ! -n "$_opt_m" ] && message="'$*' finished" [ -z "$opt_m" ] && message="'$*' finished"
[ -z "$title" ] && title=$* [ -z "$title" ] && title=$*
$@ "$@"
else else
[ ! -n "$_opt_m" ] && message="Ping" [ -z "$opt_m" ] && message="Ping"
[ -z "$title" ] && title="Ping" [ -z "$title" ] && title="Ping"
fi fi
if [ -n "$_opt_y" ] if [ -n "$opt_y" ]
then then
yesno "$message" "$title" yesno "$message" "$title"
else else