nixos/waybar_maildir.sh

56 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# waybar-maildir.sh - count messages in a maildr (or other) directory
#
# USAGE: waybar-maildir.sh DIRECTORY ACCOUNT_NAME [nonotify]
# "nonotify" is used to prevent multiple identical notification when this script is used in multi-monitor setups
#
# REQUIREMENTS: inotifywait
#
#
#
### Example (partial) ~/.config/waybar/config
#
# "custom/maildir": {
# "exec": "~/.config/waybar/waybar-maildir.sh ~/.maildir/INBOX/new mailaccountname",
# "format": " {}",
# "return-type": "json"
# },
#
### Example (partial) ~/.config/waybar/syles.css
#
# #custom-maildir {
# background-color: #875f00
# }
# mail_dir is the directory we are watching and within which, we are counting contents
# account_name can be used in notify_command as a label for the mail account being watched
mail_dir="${1}"
account_name="${2}"
old_count=0
while [[ -n "$(pgrep waybar)" ]]; do
if [[ ! -d "${mail_dir}" ]]; then
printf -- '{"text":"?"}\n'
sleep 60
fi
count="$(ls "${mail_dir}" | wc -l)"
if [[ "${count}" > 0 ]]; then
printf -- '{"text":"%s","class":"newmail","tooltip":"%s"}\n' "${count}" "${account_name}"
else
printf -- '{"text":"-","tooltip":"%s"}\n' "${account_name}"
fi
old_count="${count}"
# 60-second timeout keeps this from becoming a zombified process when waybar is no longer running
inotifywait -t 60 -qq -e 'create,delete,move' "${mail_dir}"
sleep 1 # Allow directory to settle after new messages
done
unset -v account_name count latest_file mail_dir nonotify notify_command old_count subject