Added a "top" flag for optional truncated entries

This commit is contained in:
zdravi 2015-09-24 12:20:07 -07:00
parent 15ef9749ae
commit b727cdd940
2 changed files with 14 additions and 3 deletions

View file

@ -194,7 +194,7 @@ class Journal(object):
if n: if n:
self.entries = self.entries[-n:] self.entries = self.entries[-n:]
def filter(self, tags=[], start_date=None, end_date=None, starred=False, strict=False, short=False): def filter(self, tags=[], start_date=None, end_date=None, starred=False, strict=False, short=False, head=None):
"""Removes all entries from the journal that don't match the filter. """Removes all entries from the journal that don't match the filter.
tags is a list of tags, each being a string that starts with one of the tags is a list of tags, each being a string that starts with one of the
@ -233,6 +233,15 @@ class Journal(object):
else: else:
for e in self.entries: for e in self.entries:
e.body = '' e.body = ''
elif head is not None:
try:
head = int(head)
for e in self.entries:
e.body = "\n".join(e.body.split('\n')[:head])
except ValueError:
pass
self.entries = result self.entries = result
def new_entry(self, raw, date=None, sort=True): def new_entry(self, raw, date=None, sort=True):

View file

@ -41,6 +41,7 @@ def parse_args(args=None):
reading.add_argument('-and', dest='strict', action="store_true", help='Filter by tags using AND (default: OR)') 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('-starred', dest='starred', action="store_true", help='Show only starred entries')
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) 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)
reading.add_argument('-t', '--top', dest='head', action="store", default=None, help="Clips the first N lines of each journal entry.")
exporting = parser.add_argument_group('Export / Import', 'Options for transmogrifying your journal') exporting = parser.add_argument_group('Export / Import', 'Options for transmogrifying your journal')
exporting.add_argument('--short', dest='short', action="store_true", help='Show only titles or line containing the search tags') exporting.add_argument('--short', dest='short', action="store_true", help='Show only titles or line containing the search tags')
@ -61,7 +62,7 @@ def guess_mode(args, config):
if args.decrypt is not False or args.encrypt is not False or args.export is not False or any((args.short, args.tags, args.edit)): if args.decrypt is not False or args.encrypt is not False or args.export is not False or any((args.short, args.tags, args.edit)):
compose = False compose = False
export = True export = True
elif any((args.start_date, args.end_date, args.on_date, args.limit, args.strict, args.starred)): elif any((args.start_date, args.end_date, args.on_date, args.limit, args.head, args.strict, args.starred)):
# Any sign of displaying stuff? # Any sign of displaying stuff?
compose = False compose = False
elif args.text and all(word[0] in config['tagsymbols'] for word in " ".join(args.text).split()): elif args.text and all(word[0] in config['tagsymbols'] for word in " ".join(args.text).split()):
@ -233,7 +234,8 @@ def run(manual_args=None):
start_date=args.start_date, end_date=args.end_date, start_date=args.start_date, end_date=args.end_date,
strict=args.strict, strict=args.strict,
short=args.short, short=args.short,
starred=args.starred) starred=args.starred,
head=args.head)
journal.limit(args.limit) journal.limit(args.limit)
# Reading mode # Reading mode