Permalink
Browse files

External script is now passed to the notification for formatting. Cre…

…dit for idea: BetterLeftUnsaid @ bbs.archlinux.org
  • Loading branch information...
halhen committed Apr 13, 2009
1 parent 5e08a2b commit 3b0835f03176af9158397aa87e20555522e55655
Showing with 80 additions and 35 deletions.
  1. +6 −3 .statusline.sh
  2. +16 −8 README
  3. +58 −24 statnot
@@ -1,4 +1,7 @@
loadavg="`cat /proc/loadavg | awk '{print $1, $2, $3}'`"
echo "[load ${loadavg}] `date +'%R'`"
if [ $# -eq 0 ]; then
loadavg="`cat /proc/loadavg | awk '{print $1, $2, $3}'`";
echo "[load ${loadavg}] `date +'%R'`";
else
echo "NOTIFICATION: $1";
fi
24 README
@@ -26,14 +26,22 @@ Example .xinitrc when using dwm:
*Note: you should obviously disable any existing status update scripts*
When there is no notification message pending, statnot prints a regular
status line. By default ~/.statusline.sh is called, and stdout is printed.
Example .statusline.sh (prints `[load 0.27 0.15 0.10] 12:25`):
loadavg="`cat /proc/loadavg | awk '{print $1, $2, $3}'`"
echo "[load ${loadavg}] `date +'%R'`"
*Note: make sure .statusline.sh is executable*
At regular intervals, ~/.statusline.sh is called, and the stdout is printed
in the status bar. If there is a notification to be shown, it is passed as
the last argument ($1 in this example).
Example .statusline.sh (see below) prints `[load 0.27 0.15 0.10] 12:25`
by default and "NOTIFICATION: <text>" when a notification should show. If
nothing is returned, the status text, if available, is shown.
if [ $# -eq 0 ]; then
loadavg="`cat /proc/loadavg | awk '{print $1, $2, $3}'`";
echo "[load ${loadavg}] `date +'%R'`";
else
echo "NOTIFICATION: $1";
fi
*Note: if notifications doesn't show, double check your .statusline.sh*
To test if it works (watch status bar):
82 statnot
@@ -57,6 +57,8 @@ STATUS_UPDATE_INTERVAL = 2.0 # seconds
STATUS_COMMAND = ["/bin/sh", "%s/.statusline.sh" % os.getenv("HOME")]
# update_text(text) is called when the status text should be updated
# If there is a pending notification to be formatted, it is appended as
# the final argument to the STATUS_COMMAND, e.g. as $1 in default shellscript
# Uncomment the appropriate one below, or write your own.
# dummy (echo text)
@@ -94,10 +96,37 @@ def next_notification(pop = False):
return None
with notification_queue_lock:
# If there are several pending messages, discard the first 0-timeouts
while len(notification_queue) > 1 and notification_queue[0][2] == 0:
notification_queue.pop(0)
if pop:
return notification_queue.pop(0)
else:
return notification_queue[0]
def get_statustext(notification = ''):
output = ''
try:
if not notification:
command = STATUS_COMMAND
else:
command = STATUS_COMMAND + [notification]
p = subprocess.Popen(command, stdout=subprocess.PIPE)
# Get first line
output = p.stdout.readline()[:-1]
except:
sys.stderr.write("%s: could not read status message (%s)\n"
% (sys.argv[0], ' '.join(STATUS_COMMAND)))
# Error - STATUS_COMMAND didn't exist or delivered empty result
# Fallback to notification only
if not output:
output = notification
return output
def message_thread(dummy):
last_status_update = 0
@@ -107,35 +136,40 @@ def message_thread(dummy):
while 1:
notif = next_notification()
current_time = time.time()
update_status = False
if notif:
if notif[1] != current_notification_text:
current_notification_text = notif[1]
update_text(current_notification_text)
update_status = True
elif current_time > last_notification_update + notif[2]:
# If requested timeout is zero, notification shows until
# a new notification arrives or a regular status mesasge
# cleans it
# This way is a bit risky, but works. Keep an eye on this
# when changing code
if notif[2] != 0:
update_status = True
# Pop expired notification
next_notification(True)
notif = next_notification()
if update_status == True:
last_notification_update = current_time
if current_time > last_status_update + STATUS_UPDATE_INTERVAL:
update_status = True
if current_time > last_notification_update + notif[2]:
# current message has expired
next_notification(True)
if update_status:
if notif:
current_notification_text = notif[1]
else:
current_notification_text = ''
# If given time is 0, keep notification for one status message tick
# but allow new notifications to appear
if notif[2] == 0:
last_status_update = time.time()
else:
if current_time > last_status_update + STATUS_UPDATE_INTERVAL:
try:
p = subprocess.Popen(STATUS_COMMAND, stdout=subprocess.PIPE)
while 1:
output = p.stdout.readline()[:-1]
if output == '' and p.poll() != None: break
update_text(output)
except:
sys.stderr.write("%s: could not read status message (%s)\n"
% (sys.argv[0], ' '.join(STATUS_COMMAND)))
last_status_update = time.time()
update_text(get_statustext(current_notification_text))
last_status_update = current_time
time.sleep(0.1)
class NotificationFetcher(dbus.service.Object):
@@ -153,7 +187,7 @@ class NotificationFetcher(dbus.service.Object):
self._id += 1
notification_id = self._id
text = "%s %s" % (summary, body)
text = ("%s %s" % (summary, body)).strip()
add_notification( [notification_id,
text[:NOTIFICATION_MAX_LENGTH],
int(expire_timeout) / 1000.0] )

0 comments on commit 3b0835f

Please sign in to comment.