aboutsummaryrefslogtreecommitdiff
path: root/i3/blocks/notifier
blob: 35c94f1362bcf2f02e6d28148bb355d013dc7f01 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/bash
# IMPORTANT: 
# To make this block work, you have to edit your block-command.
# It should point to this file. For best performance, only use it on blocks
# you want to monitor.
# 
# Example:
# command=~/.i3-blocks/blocks/notifier $BLOCK_NAME

BLOCK_NAME="${1}"

if [[ "${BLOCK_NAME}" == "" ]] || [[ "${BLOCK_NAME}" == "notifier" ]]; then
  exit 1
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BLOCK_PATH="${SCRIPT_DIR}/${BLOCK_NAME}"

if [[ ! -f "${BLOCK_PATH}" ]]; then
  exit 1
fi

BLOCK_OUT=$(bash "${BLOCK_PATH}")
BLOCK_URGENT=$?

# Nothing to display.
if [[ "${BLOCK_OUT}" == "" ]]; then
  exit "${BLOCK_URGENT}"
fi

NOTIFY=0

NOTIFY_CHANGES="weather display"
NOTIFY_ALWAYS=""
NOTIFY_NEVER="launcher playerctl scroll"

if [[ "${NOTIFY_NEVER}" != *${BLOCK_NAME}* ]]; then
  BLOCK_UCFIRST=$(echo "${BLOCK_NAME}" | head -c 1 | tr [a-z] [A-Z]; echo "${BLOCK_NAME}" | tail -c +2)

  CHECK_SUM=$(echo "${BLOCK_NAME}-${BLOCK_INSTANCE}" | md5sum | awk '{print $1}')
  CACHE_FILE="/tmp/.notify-cache-${CHECK_SUM}"

  BLOCK_DESC=$(echo "${BLOCK_OUT}" | head -n 1)
  BLOCK_VALUE=$(echo "${BLOCK_OUT}" | tail -n 1)
  BLOCK_ICON=""

  if [[ "${NOTIFY_ALWAYS}" == *${BLOCK_NAME}* ]]; then
    NOTIFY=1
  elif [[ "${BLOCK_URGENT}" -gt 0 ]]; then
    NOTIFY=1
  fi

  VALUE_CHANGED=0
  HAS_CACHEFILE=0
  if [[ -f "${CACHE_FILE}" ]]; then
    HAS_CACHEFILE=1
    CACHED_VALUE=$(cat "${CACHE_FILE}")
    #MODIFIED_TIME=$(stat -c '%Y' "${CACHE_FILE}")
    #CURRENT_TIME=$(date +%s)
    #ELAPSED_TIME=$((MODIFIED_TIME - CURRENT_TIME))
    if [[ "${BLOCK_VALUE}" != "${CACHED_VALUE}" ]]; then
      VALUE_CHANGED=1
      if [[ "${NOTIFY_CHANGES}" == *${BLOCK_NAME}* ]]; then
        NOTIFY=1
      fi
    fi
  elif [[ "${NOTIFY_CHANGES}" == *${BLOCK_NAME}* ]]; then
    NOTIFY=1
  fi

  if [[ "${NOTIFY}" -eq 1 ]]; then
    if [[ "${BLOCK_NAME}" == "audio" ]]; then
      BLOCK_MESSAGE="Audio level changed to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "bandwidth" ]]; then
      BLOCK_MESSAGE="Traffic went to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "battery" ]]; then
      BLOCK_MESSAGE="Energy changed to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "countdown" ]]; then
      BLOCK_MESSAGE="Remaining time is now ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "cpu" ]]; then
      BLOCK_MESSAGE="CPU utilization is now ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "feed" ]]; then
      BLOCK_MESSAGE="Newest article is ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "mail" ]]; then
      BLOCK_MESSAGE="Unread emails went to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "memory" ]]; then
      BLOCK_MESSAGE="Memory utilization is now ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "packages" ]]; then
      BLOCK_MESSAGE="${BLOCK_VALUE} packages are now ready to update."
    elif [[ "${BLOCK_NAME}" == "space" ]]; then
      BLOCK_MESSAGE="Space utilization is now ${BLOCK_INSTANCE}."
    elif [[ "${BLOCK_NAME}" == "ssid" ]]; then
      BLOCK_MESSAGE="SSID changed to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "temperature" ]]; then
      BLOCK_MESSAGE="Sensor temperature changed to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "trash" ]]; then
      BLOCK_MESSAGE="${BLOCK_VALUE} items are now in the trash."
    else
      BLOCK_MESSAGE="${BLOCK_UCFIRST} is now ${BLOCK_VALUE}."
    fi

    if [[ "${BLOCK_NAME}" == "audio" ]]; then
      BLOCK_ICON="audio-volume-high-symbolic"
    elif [[ "${BLOCK_NAME}" == "bandwidth" ]]; then
      BLOCK_ICON="network-wireless-acquiring-symbolic"
    elif [[ "${BLOCK_NAME}" == "battery" ]]; then
      BLOCK_ICON="battery-symbolic"
    elif [[ "${BLOCK_NAME}" == "bluetooth" ]]; then
      BLOCK_ICON="bluetooth-active-symbolic"
    elif [[ "${BLOCK_NAME}" == "brightness" ]]; then
      BLOCK_ICON="display-brightness-symbolic"
    elif [[ "${BLOCK_NAME}" == "cmus" ]]; then
      BLOCK_ICON="emblem-music-symbolic"
    elif [[ "${BLOCK_NAME}" == "countdown" ]]; then
      BLOCK_ICON="document-open-recent-symbolic"
    elif [[ "${BLOCK_NAME}" == "cpu" ]]; then
      BLOCK_ICON="indicator-cpufreq"
    elif [[ "${BLOCK_NAME}" == "datetime" ]]; then
      BLOCK_ICON="system-run-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "device" ]]; then
      BLOCK_ICON="system-run-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "display" ]]; then
      BLOCK_ICON="video-display-symbolic"
    elif [[ "${BLOCK_NAME}" == "feed" ]]; then
      BLOCK_ICON="folder-documents-symbolic"
    elif [[ "${BLOCK_NAME}" == "firewall" ]]; then
      BLOCK_ICON="changes-prevent-symbolic.svg" # TODO
    elif [[ "${BLOCK_NAME}" == "ip-address" ]]; then
      BLOCK_ICON="avatar-default-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "keystate" ]]; then
      BLOCK_ICON="format-text-underline-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "launcher" ]]; then
      BLOCK_ICON="system-run-symbolic"
    elif [[ "${BLOCK_NAME}" == "load" ]]; then
      BLOCK_ICON="indicator-cpufreq" # TODO
    elif [[ "${BLOCK_NAME}" == "locale" ]]; then
      BLOCK_ICON="format-text-larger-symbolic"
    elif [[ "${BLOCK_NAME}" == "mail" ]]; then
      BLOCK_ICON="mail-send-symbolic"
    elif [[ "${BLOCK_NAME}" == "memory" ]]; then
      BLOCK_ICON="drive-harddisk-solidstate-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "microphone" ]]; then
      BLOCK_ICON="audio-input-microphone-symbolic"
    elif [[ "${BLOCK_NAME}" == "network" ]]; then
      BLOCK_ICON="network-wireless-connected-symbolic"
    elif [[ "${BLOCK_NAME}" == "packages" ]]; then
      BLOCK_ICON="view-refresh-symbolic"
    elif [[ "${BLOCK_NAME}" == "playerctl" ]]; then
      BLOCK_ICON="emblem-music-symbolic"
    elif [[ "${BLOCK_NAME}" == "process" ]]; then
      BLOCK_ICON="process-stop-symbolic"
    elif [[ "${BLOCK_NAME}" == "space" ]]; then
      BLOCK_ICON="drive-harddisk-solidstate-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "ssid" ]]; then
      BLOCK_ICON="system-users-symbolic"
    elif [[ "${BLOCK_NAME}" == "temperature" ]]; then
      BLOCK_ICON="object-inverse" # TODO
    elif [[ "${BLOCK_NAME}" == "trash" ]]; then
      BLOCK_ICON="user-trash-symbolic"
    elif [[ "${BLOCK_NAME}" == "usb" ]]; then
      BLOCK_ICON="drive-removable-media-usb-symbolic-1"
    elif [[ "${BLOCK_NAME}" == "vpn" ]]; then
      BLOCK_ICON="dialog-password-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "weather" ]]; then
      BLOCK_ICON="weather-few-clouds-symbolic"
    elif [[ "${BLOCK_NAME}" == "webcam" ]]; then
      BLOCK_ICON="camera-web-symbolic"
    elif [[ "${BLOCK_NAME}" == "window" ]]; then
      BLOCK_ICON="view-dual-symbolic" # TODO
    fi
    
    if [[ "${BLOCK_URGENT}" -gt 0 ]]; then
      notify-send -u critical -t 5000 -i "${BLOCK_ICON}" -a "i3block-${BLOCK_NAME}" "${BLOCK_UCFIRST}" "${BLOCK_MESSAGE}"
    else
      notify-send -u normal -t 3500 -i "${BLOCK_ICON}" -a "i3block-${BLOCK_NAME}" "${BLOCK_UCFIRST}" "${BLOCK_MESSAGE}"
    fi
  fi

  if [[ "${VALUE_CHANGED}" -eq 1 ]] || [[ "${HAS_CACHEFILE}" -eq 0 ]]; then
    echo "${BLOCK_VALUE}" > "${CACHE_FILE}"
  fi
fi

echo "${BLOCK_OUT}"
exit "${BLOCK_URGENT}"