Created
October 29, 2014 11:56
-
-
Save cirrusUK/79948a35197837285341 to your computer and use it in GitHub Desktop.
notify for dunst i3blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
''' | |
This program is designed to work in tandem with dunst and i3blocks to extend | |
the functionality of the existing i3blocks notification blocklet and allow | |
multiple notifications of different priority levels to stack and be displayed. | |
Copyright (C) 2014 Tomasz Kramkowski | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License along | |
with this program; if not, write to the Free Software Foundation, Inc., | |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
''' | |
import os | |
import sys | |
import json | |
path = os.environ["HOME"] + "/.cache/i3blocks-dunst/notifications" | |
strlimit = 114 | |
def full_text(notification): | |
if len(notification["summary"] + notification["body"]) + 1 > strlimit: | |
return "[{}] {}".format(notification["summary"], | |
notification["body"])[:strlimit - 3] + "..." | |
else: | |
return "[{}] {}".format(notification["summary"], notification["body"]) | |
def short_text(notification): | |
if len(notification["summary"]) > strlimit: | |
return notification["summary"][:strlimit - 3] + "..." | |
else: | |
return notification["summary"] | |
def color(notification): | |
if notification["urgency"] == "LOW": | |
return "#FFFFFF" | |
elif notification["urgency"] == "NORMAL": | |
return "#00FF00" | |
elif notification["urgency"] == "CRITICAL": | |
return "#FF0000" | |
else: | |
return "#0000FF" | |
def load_notifications(): | |
if not os.path.exists(os.path.dirname(path)): | |
os.makedirs(os.path.dirname(path)) | |
if not os.path.isfile(path): | |
f = open(path, "w+") | |
else: | |
f = open(path, "r") | |
global notifications | |
try: | |
notifications = json.load(f) | |
except: | |
notifications = list() | |
f.close() | |
def save_notifications(): | |
f = open(path, "w") | |
json.dump(notifications, f) | |
f.close() | |
def calledby_i3blocks(): | |
for k in os.environ: | |
if k.startswith("BLOCK_"): | |
return True | |
return False | |
def gettop(): | |
for k, v in enumerate(notifications): | |
if v["urgency"] == "CRITICAL": | |
return k | |
for k, v in enumerate(notifications): | |
if v["urgency"] == "NORMAL": | |
return k | |
for k, v in enumerate(notifications): | |
if v["urgency"] == "LOW": | |
return k | |
return -1 | |
if __name__ == "__main__": | |
load_notifications() | |
if calledby_i3blocks(): | |
if os.environ["BLOCK_BUTTON"]: | |
if len(notifications) > 0: | |
notifications.pop(gettop()) | |
save_notifications() | |
if len(notifications) <= 0: | |
print("") | |
print("") | |
print("") | |
sys.exit(0) | |
top = gettop() | |
amount = "" | |
if len(notifications) > 1: | |
amount = "({})".format(len(notifications)) | |
print(full_text(notifications[top]) + amount) | |
print(short_text(notifications[top]) + amount) | |
print(color(notifications[top])) | |
sys.exit(0) | |
else: | |
notifications.append({ | |
"app" : sys.argv[1], | |
"summary" : sys.argv[2], | |
"body" : sys.argv[3], | |
"icon" : sys.argv[4], | |
"urgency" : sys.argv[5] | |
}) | |
save_notifications() | |
os.system("pkill -RTMIN+12 i3blocks") | |
sys.exit(0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment