%C escape suspends colors as well as attributes
[canto-curses.git] / canto_curses / tag.py
index 418da89..a636d08 100644 (file)
@@ -31,6 +31,8 @@ log = logging.getLogger("TAG")
 class TagPlugin(Plugin):
     pass
 
+alltags = []
+
 class Tag(PluginHandler, list):
     def __init__(self, tagcore, callbacks):
         list.__init__(self)
@@ -39,6 +41,7 @@ class Tag(PluginHandler, list):
         self.tagcore = tagcore
         self.tag = tagcore.tag
         self.is_tag = True
+        self.updates_pending = 0
 
         self.pad = None
         self.footpad = None
@@ -95,15 +98,12 @@ class Tag(PluginHandler, list):
         on_hook("curses_opt_change", self.on_opt_change, self)
         on_hook("curses_tag_opt_change", self.on_tag_opt_change, self)
         on_hook("curses_attributes", self.on_attributes, self)
+        on_hook("curses_items_added", self.on_items_added, self)
 
         # Upon creation, this Tag adds itself to the
         # list of all tags.
 
-        config_lock.acquire_write()
-        callbacks["get_var"]("alltags").append(self)
-
-        config.eval_tags()
-        config_lock.release_write()
+        alltags.append(self)
 
         self.sync(True)
 
@@ -111,6 +111,7 @@ class Tag(PluginHandler, list):
         self.update_plugin_lookups()
 
     def die(self):
+        log.debug("tag %s die()" % self.tag)
         # Reset so items get die() called and everything
         # else is notified about items disappearing.
 
@@ -118,6 +119,8 @@ class Tag(PluginHandler, list):
             s.die()
         del self[:]
 
+        alltags.remove(self)
+
         unhook_all(self)
 
     def on_item_state_change(self, item):
@@ -151,6 +154,14 @@ class Tag(PluginHandler, list):
                 self.need_redraw()
                 break
 
+    def on_items_added(self, tagcore, added):
+        cur_ids = self.get_ids()
+        if tagcore == self.tagcore:
+            for story_id in added:
+                if story_id not in cur_ids:
+                    self.updates_pending += 1
+            self.need_redraw()
+
     # We override eq so that empty tags don't evaluate
     # as equal and screw up things like enumeration.
 
@@ -268,6 +279,7 @@ class Tag(PluginHandler, list):
                     'n' : unread,
                     "extra_tags" : extra_tags,
                     'tag' : self,
+                    'pending' : self.updates_pending,
                     'prep' : prep_for_display}
 
         # Prep all text values for display.
@@ -329,7 +341,7 @@ class Tag(PluginHandler, list):
 
             if not self.collapsed and self.border:
                 theme_print(pad, theme_border("ts") * (width - 2), width,\
-                        "%B%1"+ theme_border("tl"), theme_border("tr") + "%0%b")
+                        "%B"+ theme_border("tl"), theme_border("tr") + "%b")
                 lines += 1
         except Exception as e:
             tb = traceback.format_exc()
@@ -343,7 +355,7 @@ class Tag(PluginHandler, list):
     def render_footer(self, width, pad):
         if not self.collapsed and self.border:
             theme_print(pad, theme_border("bs") * (width - 2), width,\
-                    "%B%1" + theme_border("bl"), theme_border("br") + "%0%b")
+                    "%B" + theme_border("bl"), theme_border("br") + "%b")
             theme_reset()
             return 1
         return 0
@@ -359,10 +371,19 @@ class Tag(PluginHandler, list):
 
             self.tagcore.lock.acquire_read()
 
+            self.tagcore.ack_changes()
+
             for story in self:
                 if story.id in self.tagcore:
                     current_stories.append((self.tagcore.index(story.id), story))
                 elif story == sel:
+
+                    # If we preserve the selection in an "undead" state, then
+                    # we keep set tagcore changed so that the next sync operation
+                    # will re-evaluate it.
+
+                    self.tagcore.changed()
+
                     if current_stories:
                         place = max([ x[0] for x in current_stories ]) + .5
                     else:
@@ -380,7 +401,8 @@ class Tag(PluginHandler, list):
             call_hook("curses_stories_added", [ self, added_stories ])
 
             conf = config.get_conf()
-            if conf["update"]["style"] == "maintain":
+            if conf["update"]["style"] == "maintain" or self.tagcore.was_reset:
+                self.tagcore.was_reset = False
                 current_stories.sort()
 
             current_stories = [ x[1] for x in current_stories ]
@@ -408,4 +430,4 @@ class Tag(PluginHandler, list):
         for s in self:
             s.sync()
 
-        self.tagcore.ack_changes()
+        self.updates_pending = 0