Properly colorize tags and print body

This commit is contained in:
Aaron Lichtman 2019-11-07 04:18:17 +01:00
parent c3b18181d6
commit 68305350e4
No known key found for this signature in database
GPG key ID: 22368077DE9F9903
2 changed files with 30 additions and 17 deletions

View file

@ -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"),

View file

@ -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(),
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.split()]))
for part in text.strip().split(" ")]
pretty_printed_entries.append(" ".join(colorized_parts))
return " ".join(pretty_printed_entries)