Add order feature

Use -desc to order entries descendantly by date.
This commit is contained in:
Juan Pablo Garcia 2017-12-07 14:59:39 -04:00
parent 2732ae5419
commit 60ce0a03ea
4 changed files with 24 additions and 4 deletions

View file

@ -77,6 +77,10 @@ everything that happened from the start of last year to the start of last march.
jrnl -starred
and to order them descendantly by date ::
jrnl -desc
Using Tags
----------

View file

@ -53,6 +53,18 @@ Feature: Basic reading and writing to a journal
When we run "jrnl -on 2013-06-10 --short"
Then the output should be "2013-06-10 15:40 Life is good."
Scenario: -desc displays the entries descendantly by date
Given we use the config "basic.yaml"
When we run "jrnl --desc"
Then the output should be
"""
2013-06-10 15:40 Life is good.
| But I'm better.
2013-06-09 15:39 My first entry.
| Everything is alright
"""
Scenario: -s displays the short version of entries (only the title)
Given we use the config "basic.yaml"
When we run "jrnl -on 2013-06-10 -s"

View file

@ -122,10 +122,11 @@ class Journal(object):
def __unicode__(self):
return self.pprint()
def pprint(self, short=False):
def pprint(self, short=False, desc=False):
"""Prettyprints the journal's entries"""
sep = "\n"
pp = sep.join([e.pprint(short=short) for e in self.entries])
sorted_entries = sorted(self.entries, key=lambda entry: entry.date, reverse=desc)
pp = sep.join([e.pprint(short=short) for e in sorted_entries])
if self.config['highlight']: # highlight tags
if self.search_tags:
for tag in self.search_tags:

View file

@ -38,6 +38,7 @@ def parse_args(args=None):
reading.add_argument('-on', dest='on_date', metavar="DATE", help='View entries on this date')
reading.add_argument('-and', dest='strict', action="store_true", help='Filter by tags using AND (default: OR)')
reading.add_argument('-starred', dest='starred', action="store_true", help='Show only starred entries')
reading.add_argument('-desc', dest='desc', action="store_true", help='Order entries downwards')
reading.add_argument('-n', dest='limit', default=None, metavar="N", help="Shows the last n entries matching the filter. '-n 3' and '-3' have the same effect.", nargs="?", type=int)
exporting = parser.add_argument_group('Export / Import', 'Options for transmogrifying your journal')
@ -238,13 +239,15 @@ def run(manual_args=None):
starred=args.starred)
journal.limit(args.limit)
desc = args.desc if args.desc is not None else False
# Reading mode
if not mode_compose and not mode_export and not mode_import:
print(util.py2encode(journal.pprint()))
print(util.py2encode(journal.pprint(desc=desc)))
# Various export modes
elif args.short:
print(util.py2encode(journal.pprint(short=True)))
print(util.py2encode(journal.pprint(short=True, desc=desc)))
elif args.tags:
print(util.py2encode(plugins.get_exporter("tags").export(journal)))