Fix -contains to allow multiple terms with "OR" logic unless -and is added (#1890)

* Store multiple -contains arguments (OR default).

Allow multiple occurrences of the -contains argument
to be stored in a list. Previously, only the last occurrence was
considered. Additionally, the behavior has been modified to default to
OR logic, meaning that if multiple -contains arguments are provided,
entries matching any of them will be included in the results.

* Solved issue #1877 "-and" flag with multiple instances of the -contains option.

* Run poe format

* Fix unit test for contains to allow list instead of single value

* Add BDD tests for multiple contains with and without -and

* Black version updated.

* Revert pyproject.toml to appease poetry

---------

Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com>
This commit is contained in:
Ricardo Ruiz 2024-05-21 06:16:33 +02:00 committed by GitHub
parent 0deadc03c8
commit 8957ceb74d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 48 additions and 5 deletions

View file

@ -265,6 +265,7 @@ def parse_args(args: list[str] = []) -> argparse.Namespace:
reading.add_argument(
"-contains",
dest="contains",
action="append",
metavar="TEXT",
help="Show entries containing specific text (put quotes around text with "
"spaces)",

View file

@ -246,7 +246,7 @@ class Journal:
exclude_starred=False,
exclude_tagged=False,
strict=False,
contains=None,
contains=[],
exclude=[],
):
"""Removes all entries from the journal that don't match the filter.
@ -276,7 +276,7 @@ class Journal:
return 0 < len([tag for tag in tags if tag in excluded_tags])
if contains:
contains_lower = contains.casefold()
contains_lower = [substring.casefold() for substring in contains]
# Create datetime object for comparison below
# this approach allows various formats
@ -298,8 +298,20 @@ class Journal:
and (
not contains
or (
contains_lower in entry.title.casefold()
or contains_lower in entry.body.casefold()
strict
and all(
substring in entry.title.casefold()
or substring in entry.body.casefold()
for substring in contains_lower
)
)
or (
not strict
and any(
substring in entry.title.casefold()
or substring in entry.body.casefold()
for substring in contains_lower
)
)
)
]