mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 13:08:31 +02:00
Fix colorization of tags in title and body
This commit is contained in:
parent
4b934e36eb
commit
94a2b78b6d
12 changed files with 83 additions and 29 deletions
|
@ -63,7 +63,6 @@ Feature: Basic reading and writing to a journal
|
||||||
When we run "jrnl -on 2013-06-10 -s"
|
When we run "jrnl -on 2013-06-10 -s"
|
||||||
Then the output should be
|
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.
|
2013-06-10 15:40 Life is good.
|
||||||
"""
|
"""
|
||||||
|
And we should get no error
|
||||||
|
|
|
@ -13,3 +13,5 @@ indent_character: "|"
|
||||||
colors:
|
colors:
|
||||||
date: red
|
date: red
|
||||||
title: green
|
title: green
|
||||||
|
body: yellow
|
||||||
|
tags: black
|
||||||
|
|
|
@ -14,3 +14,5 @@ indent_character: "|"
|
||||||
colors:
|
colors:
|
||||||
date: red
|
date: red
|
||||||
title: green
|
title: green
|
||||||
|
body: yellow
|
||||||
|
tags: black
|
||||||
|
|
|
@ -13,3 +13,5 @@ indent_character: "|"
|
||||||
colors:
|
colors:
|
||||||
date: not-a-color
|
date: not-a-color
|
||||||
title: also-not-a-color
|
title: also-not-a-color
|
||||||
|
body: still-no-color
|
||||||
|
tags: me-too
|
||||||
|
|
|
@ -14,3 +14,5 @@ indent_character: "|"
|
||||||
colors:
|
colors:
|
||||||
date: red
|
date: red
|
||||||
title: green
|
title: green
|
||||||
|
body: yellow
|
||||||
|
tags: black
|
||||||
|
|
|
@ -14,3 +14,5 @@ indent_character: "|"
|
||||||
colors:
|
colors:
|
||||||
date: red
|
date: red
|
||||||
title: green
|
title: green
|
||||||
|
body: yellow
|
||||||
|
tags: black
|
||||||
|
|
|
@ -14,3 +14,5 @@ indent_character: "|"
|
||||||
colors:
|
colors:
|
||||||
date: red
|
date: red
|
||||||
title: green
|
title: green
|
||||||
|
body: yellow
|
||||||
|
tags: black
|
||||||
|
|
|
@ -14,3 +14,5 @@ indent_character: "|"
|
||||||
colors:
|
colors:
|
||||||
date: red
|
date: red
|
||||||
title: green
|
title: green
|
||||||
|
body: yellow
|
||||||
|
tags: black
|
||||||
|
|
|
@ -25,4 +25,4 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x
|
||||||
Scenario: Upgrading a config without colors to colors
|
Scenario: Upgrading a config without colors to colors
|
||||||
Given we use the config "no_colors.yaml"
|
Given we use the config "no_colors.yaml"
|
||||||
When we run "jrnl -n 1"
|
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'}"
|
||||||
|
|
|
@ -5,7 +5,7 @@ from __future__ import unicode_literals
|
||||||
import re
|
import re
|
||||||
import ansiwrap
|
import ansiwrap
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from .util import split_title, colorize
|
from .util import split_title, colorize, highlight_tags_maintain_background_color
|
||||||
|
|
||||||
|
|
||||||
class Entry:
|
class Entry:
|
||||||
|
@ -73,16 +73,28 @@ class Entry:
|
||||||
def pprint(self, short=False):
|
def pprint(self, short=False):
|
||||||
"""Returns a pretty-printed version of the entry.
|
"""Returns a pretty-printed version of the entry.
|
||||||
If short is true, only print the title."""
|
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']:
|
if self.journal.config['indent_character']:
|
||||||
indent = self.journal.config['indent_character'].rstrip() + " "
|
indent = self.journal.config['indent_character'].rstrip() + " "
|
||||||
else:
|
else:
|
||||||
indent = ""
|
indent = ""
|
||||||
|
|
||||||
|
date_str = colorize(self.date.strftime(self.journal.config['timeformat']),
|
||||||
|
self.journal.config['colors']['date'])
|
||||||
|
|
||||||
if not short and self.journal.config['linewrap']:
|
if not short and self.journal.config['linewrap']:
|
||||||
# Color date and color / bold title and make sure first line of title is bolded
|
# Color date and color / bold title
|
||||||
title = ansiwrap.fill(colorize(date_str, self.journal.config['colors']['date']) + " " +
|
title = ansiwrap.fill(date_str + " " +
|
||||||
colorize(self.title, self.journal.config['colors']['title'], bold=True),
|
highlight_tags_maintain_background_color(self,
|
||||||
|
self.title,
|
||||||
|
self.journal.config['colors']['title'],
|
||||||
|
bold=True),
|
||||||
self.journal.config['linewrap'])
|
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([
|
body = "\n".join([
|
||||||
ansiwrap.fill(
|
ansiwrap.fill(
|
||||||
line,
|
line,
|
||||||
|
@ -90,12 +102,18 @@ class Entry:
|
||||||
initial_indent=indent,
|
initial_indent=indent,
|
||||||
subsequent_indent=indent,
|
subsequent_indent=indent,
|
||||||
drop_whitespace=True) or indent
|
drop_whitespace=True) or indent
|
||||||
for line in self.body.rstrip(" \n").splitlines()
|
for line in body.splitlines()
|
||||||
])
|
])
|
||||||
else:
|
else:
|
||||||
title = colorize(date_str, self.journal.config['colors']['date']) + " " +\
|
title = date_str + " " + \
|
||||||
colorize(self.title.rstrip("\n"), self.journal.config['colors']['title'], bold=True)
|
highlight_tags_maintain_background_color(self,
|
||||||
body = self.body.rstrip("\n ")
|
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.
|
# 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)
|
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(
|
return "{title}{sep}{body}\n".format(
|
||||||
title=title,
|
title=title,
|
||||||
sep="\n" if has_body else "",
|
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):
|
def __repr__(self):
|
||||||
|
|
|
@ -145,22 +145,7 @@ class Journal(object):
|
||||||
|
|
||||||
def pprint(self, short=False):
|
def pprint(self, short=False):
|
||||||
"""Prettyprints the journal's entries"""
|
"""Prettyprints the journal's entries"""
|
||||||
sep = "\n"
|
return "\n".join([e.pprint(short=short) for e in self.entries])
|
||||||
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
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Journal with {0} entries>".format(len(self.entries))
|
return "<Journal with {0} entries>".format(len(self.entries))
|
||||||
|
|
38
jrnl/util.py
38
jrnl/util.py
|
@ -212,6 +212,44 @@ def colorize(string, color, bold=False):
|
||||||
return colorama.Style.BRIGHT + color_escape + string + colorama.Style.RESET_ALL
|
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):
|
def slugify(string):
|
||||||
"""Slugifies a string.
|
"""Slugifies a string.
|
||||||
Based on public domain code from https://github.com/zacharyvoase/slugify
|
Based on public domain code from https://github.com/zacharyvoase/slugify
|
||||||
|
|
Loading…
Add table
Reference in a new issue