Revert "make color handling more robust"

This reverts commit ce930a27a1.
This commit is contained in:
Suhas 2021-02-06 18:56:27 -05:00
parent 457375dbe6
commit 166ca60568
2 changed files with 9 additions and 33 deletions

View file

@ -99,25 +99,9 @@ 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"]),
date_color, self.journal.config["colors"]["date"],
bold=True, bold=True,
) )
@ -129,13 +113,13 @@ class Entry:
+ highlight_tags_with_background_color( + highlight_tags_with_background_color(
self, self,
self.title, self.title,
title_color, self.journal.config["colors"]["title"],
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"), body_color self, self.body.rstrip(" \n"), self.journal.config["colors"]["body"]
) )
body_text = [ body_text = [
colorize( colorize(
@ -146,7 +130,7 @@ class Entry:
subsequent_indent=indent, subsequent_indent=indent,
drop_whitespace=True, drop_whitespace=True,
), ),
body_color, self.journal.config["colors"]["body"],
) )
or indent or indent
for line in body.rstrip(" \n").splitlines() for line in body.rstrip(" \n").splitlines()
@ -158,29 +142,27 @@ 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, body_color) + line colorize(indent, self.journal.config["colors"]["body"]) + 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"),
title_color, self.journal.config["colors"]["title"],
is_title=True, is_title=True,
) )
) )
body = highlight_tags_with_background_color( body = highlight_tags_with_background_color(
self, self.body.rstrip("\n "), body_color self, self.body.rstrip("\n "), self.journal.config["colors"]["body"]
) )
# Suppress bodies that are just blanks and new lines. # Suppress bodies that are just blanks and new lines.

View file

@ -2,7 +2,6 @@
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
@ -16,16 +15,11 @@ ERROR_COLOR = colorama.Fore.RED
RESET_COLOR = colorama.Fore.RESET RESET_COLOR = colorama.Fore.RESET
def colorize(string, color: Union[str, None], bold=False): def colorize(string, color, 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."""
if color is not None:
color_escape = getattr(colorama.Fore, color.upper(), 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: