Convert to symbolic colors
authorJack Miller <jack@codezen.org>
Thu, 4 Jun 2015 03:10:02 +0000 (22:10 -0500)
committerJack Miller <millerjo@us.ibm.com>
Fri, 5 Jun 2015 00:12:09 +0000 (19:12 -0500)
Use color "unread", for example, instead of embedding a color code
directly. This gives finer grain control.

canto_curses/color.py [new file with mode: 0644]
canto_curses/config.py
canto_curses/html.py
canto_curses/reader.py
canto_curses/story.py
canto_curses/tag.py
canto_curses/text.py
canto_curses/theme.py

diff --git a/canto_curses/color.py b/canto_curses/color.py
new file mode 100644 (file)
index 0000000..602a6d4
--- /dev/null
@@ -0,0 +1,28 @@
+# -*- coding: utf-8 -*-
+#Canto-curses - ncurses RSS reader
+#   Copyright (C) 2014 Jack Miller <jack@codezen.org>
+#
+#   This program is free software; you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License version 2 as 
+#   published by the Free Software Foundation.
+
+from canto_next.hooks import on_hook
+
+from .config import config
+
+# color code, convert a symbolic color name (e.g. "unread") into a code to put
+# in a theme string
+
+class CantoColorManager:
+    def __init__(self):
+        self.color_conf = config.get_opt("color")
+        on_hook("curses_opt_change", self.on_opt_change, self)
+
+    def on_opt_change(self, config):
+        if "color" in config:
+            self.color_conf = config["color"]
+
+    def __call__(self, name):
+        return "%" + str(self.color_conf[name])
+
+cc = CantoColorManager()
index 9c1cffb..c489248 100644 (file)
@@ -364,25 +364,23 @@ class CantoCursesConfig(SubThread):
             {
                 "defbg" : -1,
                 "deffg" : -1,
-                "0" : curses.COLOR_WHITE,
-                "1" : curses.COLOR_BLUE,
-                "2" : curses.COLOR_YELLOW,
-                "3" : curses.COLOR_BLUE,
-                "4" : curses.COLOR_GREEN,
-                "5" : curses.COLOR_MAGENTA,
-                "6" :
-                {
-                    "fg" : curses.COLOR_WHITE,
-                    "bg" : curses.COLOR_RED,
-                },
-                "7" : curses.COLOR_BLACK,
+                "unread" : 5,
+                "read" : 4,
+                "pending" : 1,
+                "text" : 8,
+                "error" : 2,
+                "reader_quote" : 6,
+                "reader_link" : 3,
+                "reader_image_link" : 5,
+                "reader_italics" : 8,
+                "enum_hints" : 8,
             },
 
             "kill_daemon_on_exit" : False
         }
 
-        for i in range(8, 256):
-            self.template_config["color"][str(i)] = i
+        for i in range(1, 257):
+            self.template_config["color"][str(i)] = i - 1
 
         self.config = eval(repr(self.template_config))
 
index 1ce4ac0..789449a 100644 (file)
@@ -10,6 +10,8 @@ from html.parser import HTMLParser
 import html.entities
 import re
 
+from .color import cc
+
 import logging
 
 log = logging.getLogger("HTML")
@@ -91,7 +93,7 @@ class CantoHTML(HTMLParser):
                     return
                 self.link_open = True
                 self.link_href = attrs["href"]
-                self.result += "%5"
+                self.result += cc("reader_link")
             else:
                 self.links.append(("link", self.link_href, self.link_text))
                 self.link_text = ""
@@ -106,7 +108,7 @@ class CantoHTML(HTMLParser):
                 if "alt" not in attrs:
                     attrs["alt"] = ""
                 self.links.append(("image", attrs["src"], attrs["alt"]))
-                self.handle_data_clean("%4" + attrs["alt"] +\
+                self.handle_data_clean(cc("reader_image_link") + attrs["alt"] +\
                         "[" + str(len(self.links)) + "]%0")
 
         elif tag in ["h" + str(x) for x in range(1,7)]:
@@ -160,9 +162,9 @@ class CantoHTML(HTMLParser):
 
         elif tag in ["i", "small", "em"]:
             if open:
-                self.result += "%6%B"
+                self.result += "%B" + cc("reader_italics")
             else:
-                self.result += "%b%0"
+                self.result += "%0%b"
         elif tag in ["b", "strong"]:
             if open:
                 self.result += "%B"
index bd13d8c..cecdacb 100644 (file)
@@ -14,6 +14,7 @@ from .theme import prep_for_display
 from .html import htmlparser
 from .text import TextBox
 from .tagcore import tag_updater
+from .color import cc
 
 import traceback
 import logging
@@ -184,7 +185,7 @@ class Reader(TextBox):
                 self.links += links
 
                 if reader_conf['show_description']:
-                    s += self.quote_rgx.sub("%5\"\\1\"%0", content)
+                    s += self.quote_rgx.sub(cc("reader_quote") + "\"\\1\"%0", content)
 
                 if reader_conf['enumerate_links']:
                     s += "\n\n"
@@ -197,9 +198,9 @@ class Reader(TextBox):
                                 text + "]: " + url + "\n\n"
 
                         if t == "link":
-                            link_text = "%4" + link_text + "%0"
+                            link_text = cc("reader_link") + link_text + "%0"
                         elif t == "image":
-                            link_text = "%3" + link_text + "%0"
+                            link_text = cc("reader_image_link") + link_text + "%0"
 
                         s += link_text
 
index 0ce13c3..3a1202a 100644 (file)
@@ -12,6 +12,7 @@ from canto_next.hooks import on_hook, unhook_all
 from .theme import FakePad, WrapPad, theme_print, theme_len, theme_reset, theme_border, prep_for_display
 from .tagcore import tag_updater
 from .config import story_needed_attrs
+from .color import cc
 
 import traceback
 import logging
@@ -261,9 +262,9 @@ class Story(PluginHandler):
             s += "%B[*]"
 
         if "read" in self.content["canto-state"]:
-            s += "%2"
+            s += cc("read")
         else:
-            s += "%1%B"
+            s += cc("unread") + "%B"
 
         s += prep_for_display(self.content["title"])
 
@@ -361,9 +362,9 @@ class Story(PluginHandler):
                 if lines == 0:
                     header = ""
                     if self.enumerated:
-                        header += "%1[" + str(self.offset) + "]%0"
+                        header += cc("enum_hints") + "[" + str(self.offset) + "]%0"
                     if self.rel_enumerated:
-                        header += "%1[" + str(self.rel_offset) + "]%0"
+                        header += cc("enum_hints") + "[" + str(self.rel_offset) + "]%0"
                     if header:
                         pad.move(0, 0)
                         theme_print(pad, header, width, "","", False, False)
index f5b6fd5..6211481 100644 (file)
@@ -15,6 +15,7 @@ from .parser import try_parse, try_eval, prep_for_display
 from .theme import FakePad, WrapPad, theme_print, theme_reset, theme_border
 from .config import config
 from .story import Story
+from .color import cc
 
 import traceback
 import logging
@@ -234,7 +235,6 @@ class Tag(PluginHandler, list):
         self.callbacks["set_var"]("needs_redraw", True)
 
     def eval(self):
-
         # Make sure to strip out the category from category:name
         tag = self.tag.split(':', 1)[1]
 
@@ -253,9 +253,9 @@ class Tag(PluginHandler, list):
 
         s += " " + tag + " "
 
-        s += "[%B%1" + str(unread) + "%0%b]"
+        s += "[%B" + cc("unread") + str(unread) + "%0%b]"
         if self.updates_pending:
-            s += " [%8%B" + str(self.updates_pending) + "%b%0]"
+            s += " [%B" + cc("pending") + str(self.updates_pending) + "%0%b]"
 
         if self.selected:
             s += "%r"
@@ -310,9 +310,9 @@ class Tag(PluginHandler, list):
                 if lines == 0:
                     header = ""
                     if self.enumerated:
-                        header += "%1[" + str(self.visible_tag_offset) + "]%0"
+                        header += cc("enum_hints") + "[" + str(self.visible_tag_offset) + "]%0"
                     if self.abs_enumerated:
-                        header += "%1[" + str(self.tag_offset) + "]%0"
+                        header += cc("enum_hints") + "[" + str(self.tag_offset) + "]%0"
                     if header:
                         pad.move(0, 0)
                         theme_print(pad, header, width, "", "", False, False)
index c4dfa7e..95fb367 100644 (file)
@@ -11,7 +11,7 @@ from canto_next.hooks import on_hook, unhook_all
 from .theme import FakePad, WrapPad, theme_print, theme_lstrip, theme_border, theme_reset
 from .command import register_commands, unregister_command
 from .guibase import GuiBase
-from .theme import theme_print
+from .color import cc
 
 import logging
 import curses
@@ -241,7 +241,7 @@ class ErrorBox(VarBox):
         VarBox.init(self, pad, callbacks, "error_msg")
 
     def update_text(self):
-        self.text = "%6" + self.value + "%0"
+        self.text = cc("error") + self.value + "%0"
 
     def get_opt_name(self):
         return "errorbox"
index 9db9de2..00f8b3a 100644 (file)
@@ -9,6 +9,7 @@
 from canto_next.encoding import encoder, locale_enc
 from .widecurse import waddch, wcwidth
 from .html import html_entity_convert, char_ref_convert
+from .config import config
 
 import curses