Filters journal entries by a phrase.

This commit is contained in:
Gregory Crosswhite 2014-11-03 14:00:57 -08:00
parent 003fb507ae
commit 9c387a59bd
2 changed files with 8 additions and 3 deletions

View file

@ -131,7 +131,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, phrase=''):
"""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
@ -141,6 +141,8 @@ class Journal(object):
starred limits journal to starred entries starred limits journal to starred entries
phrase limits journal to entries with the given phrase
If strict is True, all tags must be present in an entry. If false, the If strict is True, all tags must be present in an entry. If false, the
entry is kept if any tag is present.""" entry is kept if any tag is present."""
self.search_tags = set([tag.lower() for tag in tags]) self.search_tags = set([tag.lower() for tag in tags])
@ -155,6 +157,7 @@ class Journal(object):
and (not starred or entry.starred) and (not starred or entry.starred)
and (not start_date or entry.date >= start_date) and (not start_date or entry.date >= start_date)
and (not end_date or entry.date <= end_date) and (not end_date or entry.date <= end_date)
and (phrase in entry.title or phrase in entry.body)
] ]
if short: if short:
if tags: if tags:

View file

@ -37,6 +37,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('-p', dest='phrase', metavar="WORD", help="Shows entries with the given phrase.", nargs="*", type=str, default=None)
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')
@ -64,7 +65,7 @@ def guess_mode(args, config):
elif 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)): elif 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.strict, args.starred)) or args.phrase is not None:
# 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,
phrase=' '.join(args.phrase) if args.phrase is not None else '')
journal.limit(args.limit) journal.limit(args.limit)
# Reading mode # Reading mode