make color handling more robust

This commit is contained in:
Suhas 2021-02-04 08:52:13 -05:00
parent 15efc56779
commit ce930a27a1
2 changed files with 34 additions and 9 deletions

View file

@ -7,6 +7,7 @@ from datetime import datetime
import re import re
import ansiwrap import ansiwrap
import colorama
from .color import colorize from .color import colorize
from .color import highlight_tags_with_background_color from .color import highlight_tags_with_background_color
@ -99,9 +100,25 @@ class Entry:
else: else:
indent = "" indent = ""
if "colors" in self.journal.config and "date" in self.journal.config["colors"]:
date_color = self.journal.config["colors"]["date"]
else:
date_color = None
title_color = (
self.journal.config["colors"]["title"]
if "colors" in self.journal.config
and "title" in self.journal.config["colors"]
else None
)
body_color = (
self.journal.config["colors"]["body"]
if "colors" in self.journal.config
and "title" in self.journal.config["colors"]
else None
)
date_str = colorize( date_str = colorize(
self.date.strftime(self.journal.config["timeformat"]), self.date.strftime(self.journal.config["timeformat"]),
self.journal.config["colors"]["date"], date_color,
bold=True, bold=True,
) )
@ -113,13 +130,13 @@ class Entry:
+ highlight_tags_with_background_color( + highlight_tags_with_background_color(
self, self,
self.title, self.title,
self.journal.config["colors"]["title"], title_color,
is_title=True, is_title=True,
), ),
self.journal.config["linewrap"], self.journal.config["linewrap"],
) )
body = highlight_tags_with_background_color( body = highlight_tags_with_background_color(
self, self.body.rstrip(" \n"), self.journal.config["colors"]["body"] self, self.body.rstrip(" \n"), body_color
) )
body_text = [ body_text = [
colorize( colorize(
@ -130,7 +147,7 @@ class Entry:
subsequent_indent=indent, subsequent_indent=indent,
drop_whitespace=True, drop_whitespace=True,
), ),
self.journal.config["colors"]["body"], body_color,
) )
or indent or indent
for line in body.rstrip(" \n").splitlines() for line in body.rstrip(" \n").splitlines()
@ -142,27 +159,29 @@ class Entry:
# properly. textwrap doesn't have this issue, however, it doesn't wrap # properly. textwrap doesn't have this issue, however, it doesn't wrap
# the strings properly as it counts ANSI escapes as literal characters. # the strings properly as it counts ANSI escapes as literal characters.
# TL;DR: I'm sorry. # TL;DR: I'm sorry.
body = "\n".join( body = "\n".join(
[ [
colorize(indent, self.journal.config["colors"]["body"]) + line colorize(indent, body_color) + line
if not ansiwrap.strip_color(line).startswith(indent) if not ansiwrap.strip_color(line).startswith(indent)
else line else line
for line in body_text for line in body_text
] ]
) )
else: else:
title = ( title = (
date_str date_str
+ " " + " "
+ highlight_tags_with_background_color( + highlight_tags_with_background_color(
self, self,
self.title.rstrip("\n"), self.title.rstrip("\n"),
self.journal.config["colors"]["title"], title_color,
is_title=True, is_title=True,
) )
) )
body = highlight_tags_with_background_color( body = highlight_tags_with_background_color(
self, self.body.rstrip("\n "), self.journal.config["colors"]["body"] self, self.body.rstrip("\n "), body_color
) )
# Suppress bodies that are just blanks and new lines. # Suppress bodies that are just blanks and new lines.

View file

@ -2,6 +2,7 @@
import re import re
from string import punctuation from string import punctuation
from string import whitespace from string import whitespace
from typing import Union
import colorama import colorama
@ -15,11 +16,16 @@ ERROR_COLOR = colorama.Fore.RED
RESET_COLOR = colorama.Fore.RESET RESET_COLOR = colorama.Fore.RESET
def colorize(string, color, bold=False): def colorize(string, color: Union[str, None], bold=False):
"""Returns the string colored with colorama.Fore.color. If the color set by """Returns the string colored with colorama.Fore.color. If the color set by
the user is "NONE" or the color doesn't exist in the colorama.Fore attributes, the user is "NONE" or the color doesn't exist in the colorama.Fore attributes,
it returns the string without any modification.""" it returns the string without any modification."""
color_escape = getattr(colorama.Fore, color.upper(), None)
if color is not None:
color_escape = getattr(colorama.Fore, color.upper(), None)
else:
color_escape = None
if not color_escape: if not color_escape:
return string return string
elif not bold: elif not bold: