Skip to content

Commit

Permalink
Merge pull request #24 from anntzer/ansi
Browse files Browse the repository at this point in the history
ANSI rendering of rst markup.
  • Loading branch information
evanunderscore authored Mar 7, 2017
2 parents d7f6535 + ee96d8e commit d035c70
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
36 changes: 34 additions & 2 deletions defopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import (
absolute_import, division, unicode_literals, print_function)

import contextlib
import inspect
import logging
import re
Expand All @@ -25,9 +26,19 @@
except ImportError: # pragma: no cover
from funcsigs import signature as _inspect_signature

try:
from colorama import colorama_text as _colorama_text
except ImportError:
@contextlib.contextmanager
def _colorama_text(*args):
yield

if sys.version_info.major == 2: # pragma: no cover
def _get_type_hints(*args, **kwargs):
return {}
_basestring = basestring
else:
_basestring = str

__version__ = '3.0.0'

Expand Down Expand Up @@ -85,7 +96,8 @@ def run(*funcs, **kwargs):
func.__name__, formatter_class=_Formatter)
_populate_parser(func, subparser, parsers, short)
subparser.set_defaults(_func=func)
args = parser.parse_args(argv)
with _colorama_text():
args = parser.parse_args(argv)
# Workaround for http://bugs.python.org/issue9253#msg186387
if not main and not hasattr(args, '_func'):
parser.error('too few arguments')
Expand Down Expand Up @@ -312,8 +324,28 @@ def _parse_doc(func):
return _Doc(doctext, tuples)


def _itertext_ansi(node):
# Implementation modified from `ElementTree.Element.itertext`.
tag = node.tag
if not isinstance(tag, _basestring) and tag is not None:
return
ansi_code = {"strong": "\033[1m", "emphasis": "\033[3m"}.get(tag, "")
t = node.text
if t:
yield ansi_code
yield t
if ansi_code:
yield "\033[0m" # Reset.
for e in node:
for t in _itertext_ansi(e):
yield t
t = e.tail
if t:
yield t


def _get_text(node):
return ''.join(node.itertext())
return ''.join(_itertext_ansi(node))


def _indent(text):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ universal=1
source=defopt

[coverage:report]
fail_under=98
fail_under=97
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
install_requires=[
'docutils',
'sphinxcontrib-napoleon>=0.5.1',
'funcsigs;python_version<"3"',
'funcsigs;python_version<"3.3"',
'enum34;python_version<"3.4"',
'typing;python_version<"3.5"',
'colorama>=0.3.4;sys.platform=="win32"',
],
tests_require=[
'mock;python_version<"3"',
'mock;python_version<"3.3"',
],
classifiers=[
'Programming Language :: Python',
Expand Down
7 changes: 7 additions & 0 deletions test_defopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,13 @@ def foo(bar):
self.assertIn('%(prog)s', self._get_help(foo))
self.assertNotIn('%%', self._get_help(foo))

def test_rst_ansi(self):
def foo():
"""*italic* **bold**"""
return bar
self.assertIn('\033[3mitalic\033[0m \033[1mbold\033[0m',
self._get_help(foo))

def _get_help(self, func):
parser = ArgumentParser(formatter_class=defopt._Formatter)
defopt._populate_parser(func, parser, {}, {})
Expand Down

0 comments on commit d035c70

Please sign in to comment.