mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 21:18:32 +02:00
Merge pull request #740 from empireshades/master
Full text search (case insensitive) with "-contains"
This commit is contained in:
commit
cb9d546141
3 changed files with 38 additions and 3 deletions
29
features/contains.feature
Normal file
29
features/contains.feature
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
Feature: Contains
|
||||||
|
|
||||||
|
Scenario: Searching for a string
|
||||||
|
Given we use the config "basic.yaml"
|
||||||
|
When we run "jrnl -contains life"
|
||||||
|
Then we should get no error
|
||||||
|
and the output should be
|
||||||
|
"""
|
||||||
|
2013-06-10 15:40 Life is good.
|
||||||
|
| But I'm better.
|
||||||
|
"""
|
||||||
|
|
||||||
|
Scenario: Searching for a string within tag results
|
||||||
|
Given we use the config "tags.yaml"
|
||||||
|
When we run "jrnl @idea -contains software"
|
||||||
|
Then we should get no error
|
||||||
|
and the output should contain "software"
|
||||||
|
|
||||||
|
Scenario: Searching for a string within AND tag results
|
||||||
|
Given we use the config "tags.yaml"
|
||||||
|
When we run "jrnl -and @journal @idea -contains software"
|
||||||
|
Then we should get no error
|
||||||
|
and the output should contain "software"
|
||||||
|
|
||||||
|
Scenario: Searching for a string within NOT tag results
|
||||||
|
Given we use the config "tags.yaml"
|
||||||
|
When we run "jrnl -not @dan -contains software"
|
||||||
|
Then we should get no error
|
||||||
|
and the output should contain "software"
|
|
@ -188,7 +188,7 @@ class Journal:
|
||||||
tag_counts = {(tags.count(tag), tag) for tag in tags}
|
tag_counts = {(tags.count(tag), tag) for tag in tags}
|
||||||
return [Tag(tag, count=count) for count, tag in sorted(tag_counts)]
|
return [Tag(tag, count=count) for count, tag in sorted(tag_counts)]
|
||||||
|
|
||||||
def filter(self, tags=[], start_date=None, end_date=None, starred=False, strict=False, short=False, exclude=[]):
|
def filter(self, tags=[], start_date=None, end_date=None, starred=False, strict=False, short=False, contains=None, exclude=[]):
|
||||||
"""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
|
||||||
|
@ -210,6 +210,9 @@ class Journal:
|
||||||
# If strict mode is on, all tags have to be present in entry
|
# If strict mode is on, all tags have to be present in entry
|
||||||
tagged = self.search_tags.issubset if strict else self.search_tags.intersection
|
tagged = self.search_tags.issubset if strict else self.search_tags.intersection
|
||||||
excluded = lambda tags: len([tag for tag in tags if tag in excluded_tags]) > 0
|
excluded = lambda tags: len([tag for tag in tags if tag in excluded_tags]) > 0
|
||||||
|
if contains:
|
||||||
|
contains_lower = contains.casefold()
|
||||||
|
|
||||||
result = [
|
result = [
|
||||||
entry for entry in self.entries
|
entry for entry in self.entries
|
||||||
if (not tags or tagged(entry.tags))
|
if (not tags or tagged(entry.tags))
|
||||||
|
@ -217,6 +220,7 @@ class Journal:
|
||||||
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 (not exclude or not excluded(entry.tags))
|
and (not exclude or not excluded(entry.tags))
|
||||||
|
and (not contains or (contains_lower in entry.title.casefold() or contains_lower in entry.body.casefold()))
|
||||||
]
|
]
|
||||||
|
|
||||||
self.entries = result
|
self.entries = result
|
||||||
|
|
|
@ -32,6 +32,7 @@ def parse_args(args=None):
|
||||||
reading = parser.add_argument_group('Reading', 'Specifying either of these parameters will display posts of your journal')
|
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('-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('-until', '-to', dest='end_date', metavar="DATE", help='View entries before this date')
|
||||||
|
reading.add_argument('-contains', dest='contains', 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('-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('-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')
|
||||||
|
@ -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, args.contains)):
|
||||||
# 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()):
|
||||||
|
@ -236,7 +237,8 @@ def run(manual_args=None):
|
||||||
strict=args.strict,
|
strict=args.strict,
|
||||||
short=args.short,
|
short=args.short,
|
||||||
starred=args.starred,
|
starred=args.starred,
|
||||||
exclude=args.excluded)
|
exclude=args.excluded,
|
||||||
|
contains=args.contains)
|
||||||
journal.limit(args.limit)
|
journal.limit(args.limit)
|
||||||
|
|
||||||
# Reading mode
|
# Reading mode
|
||||||
|
|
Loading…
Add table
Reference in a new issue