Pretty print journal entry titles and dates.

Changes appearance of all jrnl viewing commands, such as $ jrnl --short and
$ jrnl -n {NUM}.

Fix #508
This commit is contained in:
Aaron Lichtman 2019-10-25 06:59:24 +02:00
parent ef23d7eabe
commit e7da8c12a0
No known key found for this signature in database
GPG key ID: 22368077DE9F9903
3 changed files with 20 additions and 6 deletions

View file

@ -5,7 +5,7 @@ from __future__ import unicode_literals
import re import re
import textwrap import textwrap
from datetime import datetime from datetime import datetime
from .util import split_title from .util import split_title, bold, colorize_red
class Entry: class Entry:
@ -79,7 +79,10 @@ class Entry:
else: else:
indent = "" indent = ""
if not short and self.journal.config['linewrap']: if not short and self.journal.config['linewrap']:
title = textwrap.fill(date_str + " " + self.title, self.journal.config['linewrap']) # Color date red and make sure first line of title is bolded
title = textwrap.fill(colorize_red(date_str) + " " + bold(self.title), self.journal.config['linewrap'])
# Make sure all lines after the first are bolded, too
title = "".join([bold(part) + "\n" for part in title.split("\n")])
body = "\n".join([ body = "\n".join([
textwrap.fill( textwrap.fill(
line, line,
@ -90,7 +93,7 @@ class Entry:
for line in self.body.rstrip(" \n").splitlines() for line in self.body.rstrip(" \n").splitlines()
]) ])
else: else:
title = date_str + " " + self.title.rstrip("\n ") title = colorize_red(date_str) + " " + bold(self.title.rstrip("\n "))
body = self.body.rstrip("\n ") body = self.body.rstrip("\n ")
# Suppress bodies that are just blanks and new lines. # Suppress bodies that are just blanks and new lines.

View file

@ -152,12 +152,12 @@ class Journal(object):
for tag in self.search_tags: for tag in self.search_tags:
tagre = re.compile(re.escape(tag), re.IGNORECASE) tagre = re.compile(re.escape(tag), re.IGNORECASE)
pp = re.sub(tagre, pp = re.sub(tagre,
lambda match: util.colorize(match.group(0)), lambda match: util.colorize_cyan(match.group(0)),
pp, re.UNICODE) pp, re.UNICODE)
else: else:
pp = re.sub( pp = re.sub(
Entry.Entry.tag_regex(self.config['tagsymbols']), Entry.Entry.tag_regex(self.config['tagsymbols']),
lambda match: util.colorize(match.group(0)), lambda match: util.colorize_cyan(match.group(0)),
pp pp
) )
return pp return pp

View file

@ -180,11 +180,22 @@ def get_text_from_editor(config, template=""):
return raw return raw
def colorize(string): def colorize_cyan(string):
"""Returns the string wrapped in cyan ANSI escape""" """Returns the string wrapped in cyan ANSI escape"""
return u"\033[36m{}\033[39m".format(string) return u"\033[36m{}\033[39m".format(string)
def colorize_red(string):
"""Returns the string wrapped in red ANSI escape"""
return u"\033[91m{}\033[0m".format(string)
def bold(string):
"""Returns the string wrapped in bold ANSI escape. Bug / feature that it
also colors the text blue, for some unknown reason."""
return u"\033[1m{}\033[0m".format(string)
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