ui.py

changeset 14
5a988a2c2624
parent 11
0bff53095f28
child 15
aaf1a281a3fe
--- a/ui.py	Sat Jan 20 20:53:28 2018 +0000
+++ b/ui.py	Sat Jan 20 21:27:05 2018 +0000
@@ -8,10 +8,22 @@
 import logging
 from threading import Lock
 
-traynames={False: 'B.', True: 'B!'}
+INACTIVE=0
+ACTIVE=1
+ERROR=2
+
+traynames={INACTIVE: 'B.', ACTIVE: 'B!', ERROR: 'B?'}
+
+def combine_state(state1, state2):
+    if state1==ERROR or state2==ERROR:
+        return ERROR
+    elif state1==ACTIVE or state2==ACTIVE:
+        return ACTIVE
+    else:
+        return INACTIVE
 
 def make_title(status):
-    active=False
+    state=INACTIVE
     if status['type']=='scheduled':
         # Operation scheduled
         when=status['when']
@@ -31,17 +43,22 @@
                 whenstr='at %02d:%02d' % (twhen.tm_hour, twhen.tm_min)
         detail=''
         if 'detail' in status and status['detail']:
+            if status['detail']=='retry':
+                state=ERROR
             detail=status['detail']+' '
         title="%s (%s%s %s)" % (status['name'], detail, status['operation'], whenstr)
     elif status['type']=='current':
         # Operation running
-        title="%s (running: %s)" % (status['name'], status['operation'])
-        active=True
+        progress=''
+        if 'progress_current' in status and 'progress_total' in status:
+            progress='%d%%' % (status.progress_current/status.progress_total)
+        title="%s (running: %s%s)" % (status['name'], status['operation'], progress)
+        active=ACTIVE
     else: # status['type']=='nothing':
         # Should be unscheduled, nothing running
         title=status['name']
 
-    return title, active
+    return title, state
 
 
 class BorgendTray(rumps.App):
@@ -66,16 +83,16 @@
                 b.set_status_update_callback(cb)
                 self.statuses[index]=b.status()
 
-            menu, active=self.__rebuild_menu()
+            menu, state=self.__rebuild_menu()
 
-            super().__init__(traynames[active], menu=menu, quit_button=None)
+            super().__init__(traynames[state], menu=menu, quit_button=None)
 
     def __rebuild_menu(self):
         menu=[]
-        active=False
+        state=INACTIVE
         for index in range(len(self.backups)):
             b=self.backups[index]
-            title, this_active=make_title(self.statuses[index])
+            title, this_state=make_title(self.statuses[index])
             logging.info('TITLE: %s' % title)
             # Python closures suck dog's balls...
             # first and the last program I write in Python until somebody
@@ -83,12 +100,12 @@
             cbm=lambda sender, _b=b: self.__menu_select_backup(sender, _b)
             item=rumps.MenuItem(title, callback=cbm)
             menu.append(item)
-            active=active or this_active
+            state=combine_state(state, this_state)
 
         menu_quit=rumps.MenuItem("Quit...", callback=self.my_quit)
         menu.append(menu_quit)
 
-        return menu, active
+        return menu, state
 
 
     def my_quit(self, _):
@@ -106,7 +123,6 @@
             logging.debug('Rebuilding menu')
             self.menu.clear()
             menu, active=self.__rebuild_menu()
-            print(active)
             self.menu.update(menu)
             self.title=traynames[active]
 

mercurial