From b75415075c00bb17e14c5666a380b7e940db8c84 Mon Sep 17 00:00:00 2001 From: Olof Johansson Date: Tue, 26 Feb 2013 07:36:03 +0100 Subject: [PATCH] Add nofmt_printer to color.py The current printer always expands on the arguments which is a problem for strings containing %. Instead of forcing manual string expansion before printing allow for a no format printer option which simply accepts and prints the string. Part of fix for issue #131: http://code.google.com/p/git-repo/issues/detail?id=131 Change-Id: I08ef94b9c4ddab58ac12d2bd32ebd2c413e4f83b --- color.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/color.py b/color.py index d856313f..7970198a 100644 --- a/color.py +++ b/color.py @@ -126,6 +126,13 @@ class Coloring(object): s._out.write(c(fmt, *args)) return f + def nofmt_printer(self, opt=None, fg=None, bg=None, attr=None): + s = self + c = self.nofmt_colorer(opt, fg, bg, attr) + def f(fmt): + s._out.write(c(fmt)) + return f + def colorer(self, opt=None, fg=None, bg=None, attr=None): if self._on: c = self._parse(opt, fg, bg, attr) @@ -138,6 +145,17 @@ class Coloring(object): return fmt % args return f + def nofmt_colorer(self, opt=None, fg=None, bg=None, attr=None): + if self._on: + c = self._parse(opt, fg, bg, attr) + def f(fmt): + return ''.join([c, fmt, RESET]) + return f + else: + def f(fmt): + return fmt + return f + def _parse(self, opt, fg, bg, attr): if not opt: return _Color(fg, bg, attr)