From b727cdd9404542a695d25b489db88dafb8d41f33 Mon Sep 17 00:00:00 2001 From: zdravi Date: Thu, 24 Sep 2015 12:20:07 -0700 Subject: [PATCH] Added a "top" flag for optional truncated entries --- jrnl/Journal.py | 11 ++++++++++- jrnl/cli.py | 6 ++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 92a6774f..1059c19e 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -194,7 +194,7 @@ class Journal(object): if 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. tags is a list of tags, each being a string that starts with one of the @@ -233,6 +233,15 @@ class Journal(object): else: for e in self.entries: 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 def new_entry(self, raw, date=None, sort=True): diff --git a/jrnl/cli.py b/jrnl/cli.py index 35764734..a284c1f6 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -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('-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('-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.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)): compose = False 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? compose = False 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, strict=args.strict, short=args.short, - starred=args.starred) + starred=args.starred, + head=args.head) journal.limit(args.limit) # Reading mode