mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 04:58:32 +02:00
make color handling more robust
This commit is contained in:
parent
15efc56779
commit
ce930a27a1
2 changed files with 34 additions and 9 deletions
|
@ -7,6 +7,7 @@ from datetime import datetime
|
|||
import re
|
||||
|
||||
import ansiwrap
|
||||
import colorama
|
||||
|
||||
from .color import colorize
|
||||
from .color import highlight_tags_with_background_color
|
||||
|
@ -99,9 +100,25 @@ class Entry:
|
|||
else:
|
||||
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(
|
||||
self.date.strftime(self.journal.config["timeformat"]),
|
||||
self.journal.config["colors"]["date"],
|
||||
date_color,
|
||||
bold=True,
|
||||
)
|
||||
|
||||
|
@ -113,13 +130,13 @@ class Entry:
|
|||
+ highlight_tags_with_background_color(
|
||||
self,
|
||||
self.title,
|
||||
self.journal.config["colors"]["title"],
|
||||
title_color,
|
||||
is_title=True,
|
||||
),
|
||||
self.journal.config["linewrap"],
|
||||
)
|
||||
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 = [
|
||||
colorize(
|
||||
|
@ -130,7 +147,7 @@ class Entry:
|
|||
subsequent_indent=indent,
|
||||
drop_whitespace=True,
|
||||
),
|
||||
self.journal.config["colors"]["body"],
|
||||
body_color,
|
||||
)
|
||||
or indent
|
||||
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
|
||||
# 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
|
||||
colorize(indent, body_color) + line
|
||||
if not ansiwrap.strip_color(line).startswith(indent)
|
||||
else line
|
||||
for line in body_text
|
||||
]
|
||||
)
|
||||
else:
|
||||
|
||||
title = (
|
||||
date_str
|
||||
+ " "
|
||||
+ highlight_tags_with_background_color(
|
||||
self,
|
||||
self.title.rstrip("\n"),
|
||||
self.journal.config["colors"]["title"],
|
||||
title_color,
|
||||
is_title=True,
|
||||
)
|
||||
)
|
||||
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.
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
import re
|
||||
from string import punctuation
|
||||
from string import whitespace
|
||||
from typing import Union
|
||||
|
||||
import colorama
|
||||
|
||||
|
@ -15,11 +16,16 @@ ERROR_COLOR = colorama.Fore.RED
|
|||
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
|
||||
the user is "NONE" or the color doesn't exist in the colorama.Fore attributes,
|
||||
it returns the string without any modification."""
|
||||
|
||||
if color is not None:
|
||||
color_escape = getattr(colorama.Fore, color.upper(), None)
|
||||
else:
|
||||
color_escape = None
|
||||
|
||||
if not color_escape:
|
||||
return string
|
||||
elif not bold:
|
||||
|
|
Loading…
Add table
Reference in a new issue