Refactor ansicolor, add demo.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demo/demo_ansicolor.py Sat Apr 13 15:24:47 2013 +0200
@@ -0,0 +1,14 @@
+#!/usr/bin/env python3
+
+from pycolib.ansicolor import highlight, names
+
+
+def print_colors():
+ for c in range(0,8):
+ print(
+ highlight(1,c), names[c].ljust(20),
+ highlight(1,8+c), names[c].ljust(20),
+ highlight(0), sep='')
+
+if __name__ == '__main__':
+ print_colors()
--- a/pycolib/ansicolor.py Sat Apr 13 14:11:26 2013 +0200
+++ b/pycolib/ansicolor.py Sat Apr 13 15:24:47 2013 +0200
@@ -6,13 +6,13 @@
disabled = False # set True to disable all colors
-def color(enable, fg=None, bg=None):
+def highlight(enable, fg=None, bg=None):
'''
- color(1) -- switch to bold, keep previous color
- color(1, RED) -- red foreground
- color(1, BOLD + YELLOW, BLUE) -- yellow on blue, bold
- color(0) -- reset
+ highlight(1) -- switch to bold, keep previous color
+ highlight(1, RED) -- red foreground
+ highlight(1, BOLD + YELLOW, BLUE) -- yellow on blue, bold
+ highlight(0) -- reset
'''
global disabled
@@ -42,14 +42,6 @@
else:
return "\033[0m"
-def set_color(enable, fg=None, bg=None):
- print(color(enable, fg, bg), end='')
-
+def set_highlight(enable, fg=None, bg=None):
+ print(highlight(enable, fg, bg), end='')
-if __name__ == '__main__':
- for c in range(0,8):
- print(
- color(1,c), names[c].ljust(20),
- color(1,8+c), names[c].ljust(20),
- color(0), sep='')
-
--- a/pycolib/coloredformatter.py Sat Apr 13 14:11:26 2013 +0200
+++ b/pycolib/coloredformatter.py Sat Apr 13 15:24:47 2013 +0200
@@ -1,7 +1,7 @@
import logging
from copy import copy
-from pycolib.ansicolor import color, BOLD, WHITE, YELLOW, RED
+from pycolib.ansicolor import highlight, BOLD, WHITE, YELLOW, RED
class ColoredFormatter(logging.Formatter):
@@ -29,7 +29,7 @@
formatted = logging.Formatter.formatTime(self, record, datefmt)
fg, bg = self.get_color('time', record)
if fg or bg:
- formatted = color(1, fg, bg) + formatted + color(0)
+ formatted = highlight(1, fg, bg) + formatted + highlight(0)
return formatted
@@ -40,12 +40,12 @@
### message color
fg, bg = self.get_color('message', record)
if fg or bg:
- record.msg = color(1, fg, bg) + record.msg + color(0)
+ record.msg = highlight(1, fg, bg) + record.msg + highlight(0)
### levelname color
fg, bg = self.get_color('levelname', record)
if fg or bg:
- record.levelname = color(1, fg, bg) + record.levelname + color(0)
+ record.levelname = highlight(1, fg, bg) + record.levelname + highlight(0)
res = logging.Formatter.format(self, record)