summaryrefslogtreecommitdiff
path: root/alert
blob: d25b383c6f5e97c83782ef540c0f141e1c37b420 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env bash
## Taken from https://github.com/arl/scripts/blob/main/alert
## alert sends desktop notification
##
## Usage: alert MSG [--] [CMD]
##   MSG is the notification text.
##   CMD is the optional command to run before showing the notification,
##    in which case alert shows an error notification if CMD failed.
##
## Options:
##   -h, --help    Display this message.

usage() {
  [ "$*" ] && echo "$0: $*"
  sed -n '/^##/,/^$/s/^## \{0,1\}//p' "$0"
  exit 2
} 2>/dev/null

main() {
  if [ $# -lt 1 ]; then
    usage
  fi
  MSG="${1}"
  shift

  while [ $# -gt 0 ]; do
    case $1 in
    -h | --help) usage 2>&1 ;;
    --)
      shift
      "$@"
      RC=$?
      break
      ;;
    -*) usage "$1: unknown option" ;;
    *) break ;;
    esac
  done

  # List of freedesktop stock icons at:
  # https://specifications.freedesktop.org/icon-naming-spec/latest
  if [ -z "$RC" ]; then
    ICON=dialog-information
  elif [[ $RC -eq 0 ]]; then
    MSG=$(echo -e "$MSG\n\n\"$*\"\n\nsuccess!")
    ICON=face-cool
  else
    MSG=$(echo -e "$MSG\n\n\"$*\"\n\nfailure!")
    ICON=face-angry
  fi

  notify-send --urgency=critical --icon $ICON "${MSG}"
}

main "$@"