From 0c1245c178bb31e828806169c5a25984250868eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radom=C3=ADr=20Bos=C3=A1k?= Date: Sat, 17 Dec 2016 20:10:02 +0100 Subject: [PATCH] Add ability to search in entry title and body jrnl was missing ability to search in entry titles and message bodies. This commit adds a '-search TEXT' option which, if specified, causes jrnl to display only those entries which have 'TEXT' in either their title or message body. Specifying the '-search' option also implies that the compose mode won't be used. Fixes #384. --- jrnl/Journal.py | 3 ++- jrnl/cli.py | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 92a6774f..ccfe28b6 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, search_plain=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 @@ -218,6 +218,7 @@ class Journal(object): and (not starred or entry.starred) and (not start_date or entry.date >= start_date) and (not end_date or entry.date <= end_date) + and (not search_plain or search_plain in entry.title or search_plain in entry.body) ] if short: if tags: diff --git a/jrnl/cli.py b/jrnl/cli.py index 35764734..2396216b 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -37,6 +37,7 @@ def parse_args(args=None): reading = parser.add_argument_group('Reading', 'Specifying either of these parameters will display posts of your journal') reading.add_argument('-from', dest='start_date', metavar="DATE", help='View entries after this date') reading.add_argument('-until', '-to', dest='end_date', metavar="DATE", help='View entries before this date') + reading.add_argument('-search', '-s', dest='search_plain', help='View entries containing a specific string') 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') @@ -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.strict, args.starred, args.search_plain)): # 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, + search_plain=args.search_plain) journal.limit(args.limit) # Reading mode