From bbbca2214bff25cd49311655232bc040f5de29b1 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sat, 27 Jul 2013 16:34:33 +0200 Subject: [PATCH] output gay colored results of searches --- .../application/formatters/iformatter.py | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/weboob/tools/application/formatters/iformatter.py b/weboob/tools/application/formatters/iformatter.py index 00face88..e08d9d59 100644 --- a/weboob/tools/application/formatters/iformatter.py +++ b/weboob/tools/application/formatters/iformatter.py @@ -18,14 +18,18 @@ # along with weboob. If not, see . - - import os import sys import subprocess if sys.platform == 'win32': import WConio +try: + from termcolor import colored +except ImportError: + def colored(s, *args, **kwargs): + return s + try: import tty import termios @@ -81,6 +85,14 @@ class IFormatter(object): BOLD = property(get_bold) NC = property(get_nc) + def colored(self, string, color, attrs=None, on_color=None): + if self.outfile != sys.stdout or not (os.isatty(self.outfile.fileno())): + return string + + if isinstance(attrs, basestring): + attrs = [attrs] + return colored(string, color, on_color=on_color, attrs=attrs) + def __init__(self, display_keys=True, display_header=True, outfile=sys.stdout): self.display_keys = display_keys self.display_header = display_header @@ -199,16 +211,18 @@ class PrettyFormatter(IFormatter): title = self.get_title(obj) desc = self.get_description(obj) - if desc is None: - title = '%s%s%s' % (self.NC, title, self.BOLD) - if alias is not None: - result = u'%s* (%s) %s (%s)%s' % (self.BOLD, alias, title, obj.backend, self.NC) + result = u'%s %s %s (%s)' % (self.colored('%2s' % alias, 'red', 'bold'), + self.colored(u'—', 'cyan', 'bold'), + self.colored(title, 'yellow', 'bold'), + self.colored(obj.backend, 'blue', 'bold')) else: - result = u'%s* (%s) %s%s' % (self.BOLD, obj.fullid, title, self.NC) + result = u'%s %s %s' % (self.colored(obj.fullid, 'red', 'bold'), + self.colored(u'—', 'cyan', 'bold'), + self.colored(title, 'yellow', 'bold')) if desc is not None: - result += u'\n\t%s' % desc + result += u'\n\t%s' % self.colored(desc, 'white') return result