Refactors prettyprinting into the pprint method, __str__ will only produce journal-file-ready strings again.

This commit is contained in:
Manuel Ebert 2012-05-31 11:22:34 +02:00
parent 08b7624513
commit cc786e5376

View file

@ -18,20 +18,37 @@ class Entry:
self.tags = set(tags)
def __str__(self):
"""Returns a string representation of the entry to be written into a journal file."""
date_str = self.date.strftime(self.journal.config['timeformat'])
if self.journal.config['linewrap']:
title = textwrap.fill(date_str + " " + self.title, self.journal.config['linewrap'])
seplen = len(title.splitlines()[-1])
body = textwrap.fill(self.body, self.journal.config['linewrap'], initial_indent="| ", subsequent_indent="| ")
else:
title = date_str + " " + self.title
seplen = len(title)
body = self.body.strip()
separator = "\n" #+ "-"*seplen + "\n"
return "{title}{sep}{body}\n".format(
title=title,
sep=separator if self.body else "",
sep="\n" if self.body else "",
body=body
)
def pprint(self):
"""Returns a pretty-printed version of the entry."""
date_str = self.date.strftime(self.journal.config['timeformat'])
if self.journal.config['linewrap']:
title = textwrap.fill(date_str + " " + self.title, self.journal.config['linewrap'])
body = "\n".join([
textwrap.fill(line+" ",
self.journal.config['linewrap'],
initial_indent="| ",
subsequent_indent="| ",
drop_whitespace=False)
for line in self.body.splitlines()
])
else:
title = date_str + " " + self.title
body = self.body.strip()
return "{title}{sep}{body}\n".format(
title=title,
sep="\n" if self.body else "",
body=body
)