~tardypad/dotfiles

ref: 03a818bd23c8c8c19b661d564661f33ee1ce2547 dotfiles/files/scripts/shell/notify -rwxr-xr-x 1.7 KiB
03a818bd — Damien Tardy-Panis Less uses lesskey file directly in XDG config folder 2 months ago
                                                                                
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/sh

init_variables() {
  COMMAND=${0##*/}

  SUMMARY=
  BODY=
  CATEGORY=
  EXPIRE_TIME=0
  HINTS='{}'
  IDENTIFIER=

  # unused as parameters
  APP_NAME=
  REPLACE_ID=0
  ICON=
  ACTIONS='[]'
}

parse_command_line() {
  while getopts c:i:t: OPT; do
    case "${OPT}" in
      c) CATEGORY="${OPTARG}" ;;
      i) IDENTIFIER="${OPTARG}" ;;
      t) EXPIRE_TIME="${OPTARG}" ;;
      ?) exit_error ;;
    esac
  done

  shift $(( OPTIND - 1 ))

  SUMMARY="$1"
  BODY="$2"
}

validate_command_line() {
  if [ -z "${SUMMARY}" ]; then
    exit_error 'missing summary argument'
  fi

  if ! echo "${EXPIRE_TIME}" | grep -E -q '^[0-9]*$'; then
    exit_error 'expire time must be a number'
  fi
}

exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  exit 1
} >&2

send_notification() {
  if [ -n "${CATEGORY}" ]; then
    HINTS="{\"category\":<string \"${CATEGORY}\">}"
  fi

  if [ -n "${IDENTIFIER}" ]; then
    NOTIFICATION_FILE="${XDG_RUNTIME_DIR:-/tmp}/notification_${IDENTIFIER}"
    if [ -f "${NOTIFICATION_FILE}" ]; then
      REPLACE_ID=$( cat "${NOTIFICATION_FILE}" )
    fi
  fi

  NOTIFICATION_ID=$(
    gdbus call \
      --session \
      --dest org.freedesktop.Notifications \
      --object-path /org/freedesktop/Notifications \
      --method org.freedesktop.Notifications.Notify \
      "${APP_NAME}" \
      "${REPLACE_ID}" \
      "${ICON}" \
      "${SUMMARY}" \
      "${BODY}" \
      "${ACTIONS}" \
      "${HINTS}" \
      "int32 $(( EXPIRE_TIME * 1000 ))" \
    | sed 's/(uint32 \([0-9]*\),)/\1/'
  )

  if [ -n "${IDENTIFIER}" ]; then
    echo "${NOTIFICATION_ID}" > "${NOTIFICATION_FILE}"
  fi
}

init_variables
parse_command_line "$@"
validate_command_line

[ "$( presentation-mode status )" = 'disabled' ] || exit 0

send_notification