diff --git a/features/core.feature b/features/core.feature index dd072ba4..06afa2ac 100644 --- a/features/core.feature +++ b/features/core.feature @@ -63,7 +63,6 @@ Feature: Basic reading and writing to a journal When we run "jrnl -on 2013-06-10 -s" Then the output should be """ - [ERROR: date set to invalid color: not-a-color] - [ERROR: title set to invalid color: also-not-a-color] 2013-06-10 15:40 Life is good. """ + And we should get no error diff --git a/features/data/configs/basic.yaml b/features/data/configs/basic.yaml index 01dd1c0c..d249187c 100644 --- a/features/data/configs/basic.yaml +++ b/features/data/configs/basic.yaml @@ -13,3 +13,5 @@ indent_character: "|" colors: date: red title: green + body: yellow + tags: black diff --git a/features/data/configs/bug153.yaml b/features/data/configs/bug153.yaml index 6b3096a1..e43cbba8 100644 --- a/features/data/configs/bug153.yaml +++ b/features/data/configs/bug153.yaml @@ -14,3 +14,5 @@ indent_character: "|" colors: date: red title: green + body: yellow + tags: black diff --git a/features/data/configs/invalid_color.yaml b/features/data/configs/invalid_color.yaml index 4c2c78fc..25c0e58d 100644 --- a/features/data/configs/invalid_color.yaml +++ b/features/data/configs/invalid_color.yaml @@ -13,3 +13,5 @@ indent_character: "|" colors: date: not-a-color title: also-not-a-color + body: still-no-color + tags: me-too diff --git a/features/data/configs/markdown-headings-335.yaml b/features/data/configs/markdown-headings-335.yaml index b180fcd3..bb114675 100644 --- a/features/data/configs/markdown-headings-335.yaml +++ b/features/data/configs/markdown-headings-335.yaml @@ -14,3 +14,5 @@ indent_character: "|" colors: date: red title: green + body: yellow + tags: black diff --git a/features/data/configs/tags-216.yaml b/features/data/configs/tags-216.yaml index 37cfa11f..d967801c 100644 --- a/features/data/configs/tags-216.yaml +++ b/features/data/configs/tags-216.yaml @@ -14,3 +14,5 @@ indent_character: "|" colors: date: red title: green + body: yellow + tags: black diff --git a/features/data/configs/tags-237.yaml b/features/data/configs/tags-237.yaml index 27b662dd..6fc8b814 100644 --- a/features/data/configs/tags-237.yaml +++ b/features/data/configs/tags-237.yaml @@ -14,3 +14,5 @@ indent_character: "|" colors: date: red title: green + body: yellow + tags: black diff --git a/features/data/configs/tags.yaml b/features/data/configs/tags.yaml index bb0c39c1..0be45a6d 100644 --- a/features/data/configs/tags.yaml +++ b/features/data/configs/tags.yaml @@ -14,3 +14,5 @@ indent_character: "|" colors: date: red title: green + body: yellow + tags: black diff --git a/features/upgrade.feature b/features/upgrade.feature index 587102eb..ae1ac931 100644 --- a/features/upgrade.feature +++ b/features/upgrade.feature @@ -25,4 +25,4 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x Scenario: Upgrading a config without colors to colors Given we use the config "no_colors.yaml" When we run "jrnl -n 1" - Then the config should have "colors" set to "{'date':'red', 'title':'blue'}" + Then the config should have "colors" set to "{'date':'none', 'title':'none', 'body':'none', 'tags':'none'}" diff --git a/jrnl/Entry.py b/jrnl/Entry.py index c419cf10..66f0c997 100755 --- a/jrnl/Entry.py +++ b/jrnl/Entry.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals import re import ansiwrap from datetime import datetime -from .util import split_title, colorize +from .util import split_title, colorize, highlight_tags_maintain_background_color class Entry: @@ -73,16 +73,28 @@ class Entry: def pprint(self, short=False): """Returns a pretty-printed version of the entry. If short is true, only print the title.""" - date_str = self.date.strftime(self.journal.config['timeformat']) + + # Handle indentation if self.journal.config['indent_character']: indent = self.journal.config['indent_character'].rstrip() + " " else: indent = "" + + date_str = colorize(self.date.strftime(self.journal.config['timeformat']), + self.journal.config['colors']['date']) + if not short and self.journal.config['linewrap']: - # Color date and color / bold title and make sure first line of title is bolded - title = ansiwrap.fill(colorize(date_str, self.journal.config['colors']['date']) + " " + - colorize(self.title, self.journal.config['colors']['title'], bold=True), + # Color date and color / bold title + title = ansiwrap.fill(date_str + " " + + highlight_tags_maintain_background_color(self, + self.title, + self.journal.config['colors']['title'], + bold=True), self.journal.config['linewrap']) + body = highlight_tags_maintain_background_color(self, + self.body.rstrip(" \n"), + self.journal.config['colors']['body'], + bold=False) body = "\n".join([ ansiwrap.fill( line, @@ -90,12 +102,18 @@ class Entry: initial_indent=indent, subsequent_indent=indent, drop_whitespace=True) or indent - for line in self.body.rstrip(" \n").splitlines() + for line in body.splitlines() ]) else: - title = colorize(date_str, self.journal.config['colors']['date']) + " " +\ - colorize(self.title.rstrip("\n"), self.journal.config['colors']['title'], bold=True) - body = self.body.rstrip("\n ") + title = date_str + " " + \ + highlight_tags_maintain_background_color(self, + self.title.rstrip("\n"), + self.journal.config['colors']['title'], + bold=True) + body = highlight_tags_maintain_background_color(self, + self.body.rstrip("\n "), + self.journal.config['colors']['body'], + bold=False) # Suppress bodies that are just blanks and new lines. has_body = len(self.body) > 20 or not all(char in (" ", "\n") for char in self.body) @@ -106,7 +124,7 @@ class Entry: return "{title}{sep}{body}\n".format( title=title, sep="\n" if has_body else "", - body=colorize(body, self.journal.config['colors']['body']) if has_body else "", + body=body if has_body else "", ) def __repr__(self): diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 99c7f250..4288c0f2 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -145,22 +145,7 @@ class Journal(object): def pprint(self, short=False): """Prettyprints the journal's entries""" - sep = "\n" - pp = sep.join([e.pprint(short=short) for e in self.entries]) - if self.config['highlight']: # highlight tags - if self.search_tags: - for tag in self.search_tags: - tagre = re.compile(re.escape(tag), re.IGNORECASE) - pp = re.sub(tagre, - lambda match: util.colorize(match.group(0), self.config['colors']['tags']), - pp, re.UNICODE) - else: - pp = re.sub( - Entry.Entry.tag_regex(self.config['tagsymbols']), - lambda match: util.colorize(match.group(0), self.config['colors']['tags']), - pp - ) - return pp + return "\n".join([e.pprint(short=short) for e in self.entries]) def __repr__(self): return "".format(len(self.entries)) diff --git a/jrnl/util.py b/jrnl/util.py index a3230f10..1b3e04ac 100644 --- a/jrnl/util.py +++ b/jrnl/util.py @@ -212,6 +212,44 @@ def colorize(string, color, bold=False): return colorama.Style.BRIGHT + color_escape + string + colorama.Style.RESET_ALL +def highlight_tags_maintain_background_color(entry, text, color, bold=False): + """ + Takes a string and colorizes the tags in it based upon the config value for + color.tags, while colorizing the rest of the text based on color. + :param entry: Entry object, for access to journal config + :param text: Text to be colorized + :param color: Color for non-tag text, passed to colorize() + :param bold: Bold flag text, passed to colorize() + :return: Colorized str + """ + config = entry.journal.config + if config['highlight']: # highlight tags + if entry.journal.search_tags: + for tag in entry.search_tags: + text_split = re.split(re.compile(re.escape(tag), re.IGNORECASE), + text, + flags=re.UNICODE) + text = [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] + else: + text_split = re.split(entry.tag_regex(config['tagsymbols']), text) + text = [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] + return " ".join(text) + + def slugify(string): """Slugifies a string. Based on public domain code from https://github.com/zacharyvoase/slugify