From 68305350e46049353174456c96bd3d5f5f6c7a71 Mon Sep 17 00:00:00 2001 From: Aaron Lichtman Date: Thu, 7 Nov 2019 04:18:17 +0100 Subject: [PATCH] Properly colorize tags and print body --- jrnl/Entry.py | 27 +++++++++++++++++++-------- jrnl/util.py | 20 +++++++++++--------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/jrnl/Entry.py b/jrnl/Entry.py index 62ca1ab6..14b61378 100755 --- a/jrnl/Entry.py +++ b/jrnl/Entry.py @@ -95,15 +95,26 @@ class Entry: self.body.rstrip(" \n"), self.journal.config['colors']['body'], bold=False) - body = "\n".join([ + body_text = [colorize( ansiwrap.fill( line, self.journal.config['linewrap'], initial_indent=indent, subsequent_indent=indent, - drop_whitespace=True) or indent - for line in body.rstrip(" \n").splitlines() - ]) + drop_whitespace=True), + self.journal.config['colors']['body']) or indent + for line in body.rstrip(" \n").splitlines()] + + # ansiwrap doesn't handle lines with only the "\n" character and some + # ANSI escapes properly, so we have this nasty hack here to make sure the + # beginning of each line has the indent character, and it's colored + # properly. textwrap doesn't have this issue, however, it doesn't wrap + # the strings properly as it counts ANSI escapes as literal characters. + # TL;DR: I'm sorry. + body = "\n".join([colorize(indent, self.journal.config['colors']['body']) + line + if not ansiwrap.strip_color(line).startswith(indent) + else line + for line in body_text]) else: title = date_str + " " + highlight_tags_maintain_background_color(self, self.title.rstrip("\n"), @@ -134,10 +145,10 @@ class Entry: def __eq__(self, other): if not isinstance(other, Entry) \ - or self.title.strip() != other.title.strip() \ - or self.body.rstrip() != other.body.rstrip() \ - or self.date != other.date \ - or self.starred != other.starred: + or self.title.strip() != other.title.strip() \ + or self.body.rstrip() != other.body.rstrip() \ + or self.date != other.date \ + or self.starred != other.starred: return False return True diff --git a/jrnl/util.py b/jrnl/util.py index 30ae80a6..c9559e59 100644 --- a/jrnl/util.py +++ b/jrnl/util.py @@ -233,17 +233,19 @@ def highlight_tags_maintain_background_color(entry, text, color, bold=False): else: search_texts = re.split(entry.tag_regex(config['tagsymbols']), text) - # TODO: Condense this all into a list comprehension once the broken regression test is fixed. + # TODO: Condense this all into a list comprehension pretty_printed_entries = [] for text in search_texts: - pretty_printed_entries.append(" ".join([colorize(part.strip(), - color, - bold) - if len(part) > 0 and part[0] not in config['tagsymbols'] - else colorize(part.strip(), - config['colors']['tags'], - not bold) - for part in text.split()])) + colorized_parts = [colorize(part.strip(), + color, + bold) + if len(part) > 0 and part[0] not in config['tagsymbols'] + else colorize(part.strip(), + config['colors']['tags'], + not bold) + for part in text.strip().split(" ")] + + pretty_printed_entries.append(" ".join(colorized_parts)) return " ".join(pretty_printed_entries)