From 5b4958e1e2c497b732ac69d9b7ab545e5a88102e Mon Sep 17 00:00:00 2001 From: Martinez Date: Sun, 15 May 2016 00:28:13 +0200 Subject: [PATCH] add over.text.count_leading add indent support to over.text.Output --- over/text.py | 26 +++++++++++++++++++++++++- over/version.py | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/over/text.py b/over/text.py index 1bfe467..efee88a 100644 --- a/over/text.py +++ b/over/text.py @@ -236,6 +236,22 @@ def strlen(string): # -------------------------------------------------- +def count_leading(text, char): + """ + Returns the count of leading `char`s within `text`. + """ + + if not text: + return 0 + + for i, c in enumerate(text): + if c != char: + return i + + return i + 1 + +# -------------------------------------------------- + class Output: class tag: class short: @@ -270,13 +286,15 @@ class Output: - tag - name - supplied text + - indentation start marker """ - def __init__(self, name, format="[%Y-%m-%d %H:%M:%S] -- , ", colors=True, end=".\n", stream=sys.stderr): + def __init__(self, name, format="[%Y-%m-%d %H:%M:%S] -- , ", colors=True, end=".\n", indent=True, stream=sys.stderr): self.name = name self.format = format self.colors = colors self.end = end + self.indent = indent self.stream = stream def __call__(self, text, tag=None, format=None, colors=None, end=None): @@ -288,6 +306,12 @@ class Output: output = datetime.datetime.now().strftime(format) output = output.replace("", tag) output = output.replace("", self.name) + + if self.indent: + indent_width = render(output, colors=False).index("") + text = paragraph(text, indent=indent_width)[indent_width:] + + output = output.replace("", "") output = output.replace("", text) output += end diff --git a/over/version.py b/over/version.py index ea73543..f32099b 100644 --- a/over/version.py +++ b/over/version.py @@ -4,5 +4,5 @@ major = 0 # VERSION_MAJOR_IDENTIFIER minor = 0 # VERSION_MINOR_IDENTIFIER # VERSION_LAST_MM 0.0 -patch = 1 # VERSION_PATCH_IDENTIFIER +patch = 2 # VERSION_PATCH_IDENTIFIER str = ".".join(str(v) for v in (major, minor, patch))