From edff5f861cc128c821a6072d6df60b1910625f24 Mon Sep 17 00:00:00 2001 From: Ricardo Ruiz Date: Thu, 9 May 2024 23:43:29 +0200 Subject: [PATCH] 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. --- jrnl/args.py | 1 + jrnl/journals/Journal.py | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/jrnl/args.py b/jrnl/args.py index 53a77462e..436851822 100644 --- a/jrnl/args.py +++ b/jrnl/args.py @@ -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)", diff --git a/jrnl/journals/Journal.py b/jrnl/journals/Journal.py index d10548140..27b8505c6 100644 --- a/jrnl/journals/Journal.py +++ b/jrnl/journals/Journal.py @@ -246,7 +246,7 @@ def filter( 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 @@ def excluded(tags): 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,11 @@ def excluded(tags): and ( not contains or ( - contains_lower in entry.title.casefold() - or contains_lower in entry.body.casefold() + any( + substring in entry.title.casefold() + or substring in entry.body.casefold() + for substring in contains_lower + ) ) ) ]