From ddb3cf2510d9fc147cfc3e4e1ac260aea9da8d98 Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Wed, 12 Oct 2011 00:24:14 +0200 Subject: [PATCH] [eit.colorful] add some whacky classes to inject some colors into argparse output --- server/eit/colorful.py | 45 +++++++++++++++++++++++++++++++++++++ server/eit/commands/help.py | 18 ++++++++++----- 2 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 server/eit/colorful.py diff --git a/server/eit/colorful.py b/server/eit/colorful.py new file mode 100644 index 000000000..ee1652e0f --- /dev/null +++ b/server/eit/colorful.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +""" + + @author: Fabio Erculiani + @contact: lxnay@sabayon.org + @copyright: Fabio Erculiani + @license: GPL-2 + + B{Entropy Infrastructure Toolkit}. + +NOTE: this colorful stuff introduces some unwanted effects. +But it's better than living in a black/white world. +""" +import sys +import argparse + +import textwrap as _textwrap + +from entropy.output import decolorize + +class ColorfulFormatter(argparse.RawDescriptionHelpFormatter): + """ + This is just a whacky HelpFormatter flavour to add some coloring. + """ + + def _split_lines(self, text, width): + text = self._whitespace_matcher.sub(' ', text).strip() + width_span = len(text) - len(decolorize(text)) + return _textwrap.wrap(text, width + width_span) + +if sys.hexversion >= 0x3000000: + str_class = str +else: + str_class = unicode +class ColorfulStr(str_class): + """ + This String object has been introduced to fake + argparse width calculations and allow colorful + help. + """ + def __new__(cls, seq): + return str_class.__new__(cls, seq) + + def __len__(self): + return len(decolorize(self)) diff --git a/server/eit/commands/help.py b/server/eit/commands/help.py index 120f5a3bc..6a3020c5c 100644 --- a/server/eit/commands/help.py +++ b/server/eit/commands/help.py @@ -12,7 +12,9 @@ import argparse from entropy.i18n import _ +from entropy.output import purple, teal, darkgreen, decolorize +from eit.colorful import ColorfulFormatter, ColorfulStr from eit.commands.descriptor import EitCommandDescriptor from eit.commands.command import EitCommand @@ -35,19 +37,23 @@ class EitHelp(EitCommand): parser = argparse.ArgumentParser( description=_("Entropy Infrastructure Toolkit"), epilog="http://www.sabayon.org", - formatter_class=argparse.RawDescriptionHelpFormatter) + formatter_class=ColorfulFormatter) descriptors = EitCommandDescriptor.obtain() descriptors.sort(key = lambda x: x.get_name()) group = parser.add_argument_group("command", "available commands") for descriptor in descriptors: - aliases_str = ", ".join(descriptor.get_class().ALIASES) + aliases = descriptor.get_class().ALIASES + aliases_str = ", ".join( + [teal(x) for x in aliases]) if aliases_str: aliases_str = " [%s]" % (aliases_str,) - name = "%s%s" % (descriptor.get_name(), aliases_str) - group.add_argument(name, - help=descriptor.get_description(), - action="store_true") + name = u"%s%s" % (purple(descriptor.get_name()), + aliases_str) + desc = darkgreen(descriptor.get_description()) + group.add_argument(ColorfulStr(name), + help=ColorfulStr(desc), + action="store_true") parser.print_help() if not self._args: return 1