[eit.colorful] add some whacky classes to inject some colors into argparse output
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
|
||||
@author: Fabio Erculiani <lxnay@sabayon.org>
|
||||
@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))
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user