Enable long color codes in format strings
authorJack Miller <jack@codezen.org>
Fri, 25 Jul 2014 15:32:25 +0000 (10:32 -0500)
committerJack Miller <jack@codezen.org>
Fri, 25 Jul 2014 15:32:25 +0000 (10:32 -0500)
So instead of %1 - %8, there's now %[122] for example. Still needs
testing and configuration behind it.

canto_curses/config.py
canto_curses/story.py
canto_curses/tag.py
canto_curses/theme.py

index e27cb32..a52a52a 100644 (file)
@@ -21,7 +21,7 @@ from canto_next.hooks import call_hook
 from canto_next.rwlock import RWLock, write_lock, read_lock
 from canto_next.remote import assign_to_dict, access_dict
 
-DEFAULT_FSTRING = "%1%?{en}([%i] :)%?{ren}([%x] :)%?{sel}(%{selected}:%{unselected})%?{rd}(%{read}:%{unread})%?{m}(%{marked}:%{unmarked})%t%?{m}(%{marked_end}:%{unmarked_end})%?{rd}(%{read_end}:%{unread_end})%?{sel}(%{selected_end}:%{unselected_end})%0"
+DEFAULT_FSTRING = "%[1]%?{en}([%i] :)%?{ren}([%x] :)%?{sel}(%{selected}:%{unselected})%?{rd}(%{read}:%{unread})%?{m}(%{marked}:%{unmarked})%t%?{m}(%{marked_end}:%{unmarked_end})%?{rd}(%{read_end}:%{unread_end})%?{sel}(%{selected_end}:%{unselected_end})%0"
 
 DEFAULT_TAG_FSTRING = "%1%?{sel}(%{selected}:%{unselected})%?{c}([+]:[-])%?{en}([%{to}]:)%?{aen}([%{vto}]:) %t [%B%2%n%1%b]%?{sel}(%{selected_end}:%{unselected_end})%0"
 
@@ -170,19 +170,15 @@ class CantoCursesConfig(SubThread):
             {
                 "defbg" : self.validate_color,
                 "deffg" : self.validate_color,
-                "0" : self.validate_color,
-                "1" : self.validate_color,
-                "2" : self.validate_color,
-                "3" : self.validate_color,
-                "4" : self.validate_color,
-                "5" : self.validate_color,
-                "6" : self.validate_color,
-                "7" : self.validate_color,
+                # See also setup for numeric settings below
             },
 
             "kill_daemon_on_exit" : self.validate_bool
         }
 
+        for i in range(1, 256):
+            self.validators["color"][str(i)] = self.validate_color
+
         self.config = {
             "browser" :
             {
index 9c0203f..8b23e95 100644 (file)
@@ -293,7 +293,7 @@ class Story(PluginHandler):
         # evaluation.
 
         passthru = {}
-        for c in "RrDdUuBbSs012345678":
+        for c in "RrDdUuBbSs012345678[":
             passthru[c] = "%" + c
 
         # Add refactored themability variables:
index 45b15b6..b005807 100644 (file)
@@ -283,7 +283,7 @@ class Tag(list):
         # evaluation.
 
         passthru = {}
-        for c in "RrDdUuBbSs012345678":
+        for c in "RrDdUuBbSs012345678[":
             passthru[c] = "%" + c
 
         for attr in [ "selected", "unselected", "selected_end", "unselected_end" ]:
index c69e16f..5a3942a 100644 (file)
@@ -115,6 +115,9 @@ def theme_print_one(pad, uni, width):
     escaped = False
     code = False
 
+    long_code = False
+    lc = ""
+
     for i, c in enumerate(uni):
         ec = encoder(c)
         if escaped:
@@ -167,8 +170,28 @@ def theme_print_one(pad, uni, width):
                 for attr in attr_map:
                     if attr_count[attr]:
                         pad.attron(attr_map[attr])
-
+            elif c == "[":
+                long_code = True
             code = False
+        elif long_code:
+            if c == "]":
+                try:
+                    long_color = int(lc)
+                except:
+                    log.error("Unknown long code: %s! Ignoring..." % lc)
+                else:
+                    if long_color < 1 or long_color > 256:
+                        log.error("long color code must be >= 1 and <= 256")
+                    else:
+                        try:
+                            pad.attron(curses.color_pair(long_color))
+                            color_stack.append(long_color)
+                        except:
+                            log.error("Could not set pair. Perhaps need to set TERM='xterm-256color'?")
+                long_code = False
+                lc = ""
+            else:
+                lc += c
         elif c == "\\":
             escaped = True
         elif c == "%":