From 72b04cf5af6039986ed6b071e6a28df2a3ea5a21 Mon Sep 17 00:00:00 2001 From: Boris Feld Date: Sat, 11 Feb 2012 22:51:20 +0100 Subject: [PATCH 1/4] Add a dedent util function, useful when using indent otherwise than as a context. --- clint/textui/core.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/clint/textui/core.py b/clint/textui/core.py index af9cab2..906adb8 100644 --- a/clint/textui/core.py +++ b/clint/textui/core.py @@ -58,8 +58,11 @@ class Writer(object): def __exit__(self, type, value, traceback): - self.shared['indent_strings'].pop() + self._dedent() + @classmethod + def _dedent(cls): + cls.shared['indent_strings'].pop() def __call__(self, s, newline=True, stream=STDOUT): @@ -91,3 +94,7 @@ def puts_err(s='', newline=True, stream=STDERR): def indent(indent=4, quote=''): """Indentation context manager.""" return Writer(indent=indent, quote=quote) + +def dedent(): + return Writer._dedent() + From 761dce723797a95dc77d03d7f52317ff19e2fb94 Mon Sep 17 00:00:00 2001 From: FELD Boris Date: Sun, 12 Feb 2012 17:20:42 +0100 Subject: [PATCH 2/4] Try to reimplement clint.textui.core in a more simpler way --- clint/textui/core.py | 94 ++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 52 deletions(-) diff --git a/clint/textui/core.py b/clint/textui/core.py index 906adb8..3105497 100644 --- a/clint/textui/core.py +++ b/clint/textui/core.py @@ -13,12 +13,15 @@ from __future__ import absolute_import import sys +from contextlib import contextmanager + from .formatters import max_width, min_width from .cols import columns from ..utils import tsplit -__all__ = ('puts', 'puts_err', 'indent', 'columns', 'max_width', 'min_width') +__all__ = ('puts', 'puts_err', 'indent', 'dedent', 'columns', 'max_width', + 'min_width') STDOUT = sys.stdout.write @@ -26,75 +29,62 @@ STDERR = sys.stderr.write NEWLINES = ('\n', '\r', '\r\n') +INDENT_STRINGS = [] +# Private -class Writer(object): - """WriterUtilized by context managers.""" +def _indent(indent=0, quote='', indent_char=' '): + """Indent util function, compute new indent_string""" + if indent > 0: + indent_string = ''.join(( + str(quote), + (indent_char * (indent - len(quote))) + )) + else: + indent_string = ''.join(( + ('\x08' * (-1 * (indent - len(quote)))), + str(quote)) + ) - shared = dict(indent_level=0, indent_strings=[]) - - - def __init__(self, indent=0, quote='', indent_char=' '): - self.indent = indent - self.indent_char = indent_char - self.indent_quote = quote - if self.indent > 0: - self.indent_string = ''.join(( - str(quote), - (self.indent_char * (indent - len(self.indent_quote))) - )) - else: - self.indent_string = ''.join(( - ('\x08' * (-1 * (indent - len(self.indent_quote)))), - str(quote)) - ) - - if len(self.indent_string): - self.shared['indent_strings'].append(self.indent_string) + if len(indent_string): + INDENT_STRINGS.append(indent_string) +class IndentContext(object): def __enter__(self): return self - def __exit__(self, type, value, traceback): - self._dedent() - - @classmethod - def _dedent(cls): - cls.shared['indent_strings'].pop() - - def __call__(self, s, newline=True, stream=STDOUT): - - if newline: - s = tsplit(s, NEWLINES) - s = map(str, s) - indent = ''.join(self.shared['indent_strings']) - - s = (str('\n' + indent)).join(s) - - _str = ''.join(( - ''.join(self.shared['indent_strings']), - str(s), - '\n' if newline else '' - )) - stream(_str) + dedent() +# Public def puts(s='', newline=True, stream=STDOUT): """Prints given string to stdout via Writer interface.""" - Writer()(s, newline, stream=stream) + if newline: + s = tsplit(s, NEWLINES) + s = map(str, s) + indent = ''.join(INDENT_STRINGS) + s = (str('\n' + indent)).join(s) + + _str = ''.join(( + ''.join(INDENT_STRINGS), + str(s), + '\n' if newline else '' + )) + stream(_str) def puts_err(s='', newline=True, stream=STDERR): """Prints given string to stderr via Writer interface.""" - Writer()(s, newline, stream=stream) + puts(s, newline, stream) +def dedent(): + """Dedent next strings, use only if you use indent otherwise than as a + context.""" + INDENT_STRINGS.pop() def indent(indent=4, quote=''): """Indentation context manager.""" - return Writer(indent=indent, quote=quote) - -def dedent(): - return Writer._dedent() - + _indent(indent, quote) + return IndentContext() From ea9b2186d5b30f653d5651d0787bdfca47c10d71 Mon Sep 17 00:00:00 2001 From: FELD Boris Date: Sun, 12 Feb 2012 17:32:59 +0100 Subject: [PATCH 3/4] More simple implementation of indent context --- clint/textui/core.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/clint/textui/core.py b/clint/textui/core.py index 3105497..09a678e 100644 --- a/clint/textui/core.py +++ b/clint/textui/core.py @@ -49,14 +49,6 @@ def _indent(indent=0, quote='', indent_char=' '): if len(indent_string): INDENT_STRINGS.append(indent_string) -class IndentContext(object): - - def __enter__(self): - return self - - def __exit__(self, type, value, traceback): - dedent() - # Public def puts(s='', newline=True, stream=STDOUT): @@ -84,7 +76,12 @@ def dedent(): context.""" INDENT_STRINGS.pop() +@contextmanager +def _indent_context(): + yield + dedent() + def indent(indent=4, quote=''): """Indentation context manager.""" _indent(indent, quote) - return IndentContext() + return _indent_context() From 7c48045b574f46905a1ce3d999168cdd747fa341 Mon Sep 17 00:00:00 2001 From: FELD Boris Date: Sun, 12 Feb 2012 17:46:33 +0100 Subject: [PATCH 4/4] Update clint.textui.core's docstrings --- clint/textui/core.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/clint/textui/core.py b/clint/textui/core.py index 09a678e..6ddacc3 100644 --- a/clint/textui/core.py +++ b/clint/textui/core.py @@ -52,7 +52,7 @@ def _indent(indent=0, quote='', indent_char=' '): # Public def puts(s='', newline=True, stream=STDOUT): - """Prints given string to stdout via Writer interface.""" + """Prints given string to stdout.""" if newline: s = tsplit(s, NEWLINES) s = map(str, s) @@ -68,7 +68,7 @@ def puts(s='', newline=True, stream=STDOUT): stream(_str) def puts_err(s='', newline=True, stream=STDERR): - """Prints given string to stderr via Writer interface.""" + """Prints given string to stderr.""" puts(s, newline, stream) def dedent(): @@ -78,10 +78,11 @@ def dedent(): @contextmanager def _indent_context(): + """Indentation context manager.""" yield dedent() def indent(indent=4, quote=''): - """Indentation context manager.""" + """Indentation manager, return an indentation context manager.""" _indent(indent, quote) return _indent_context()