From edff5f861cc128c821a6072d6df60b1910625f24 Mon Sep 17 00:00:00 2001 From: Ricardo Ruiz Date: Thu, 9 May 2024 23:43:29 +0200 Subject: [PATCH 1/7] 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 + ) ) ) ] From 044bc64f0bd8e35fa12a58826c53f5b989cceb74 Mon Sep 17 00:00:00 2001 From: Ricardo Ruiz Date: Fri, 10 May 2024 00:13:30 +0200 Subject: [PATCH 2/7] Solved issue #1877 "-and" flag with multiple instances of the -contains option. --- jrnl/journals/Journal.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/jrnl/journals/Journal.py b/jrnl/journals/Journal.py index 27b8505c6..cac154ba1 100644 --- a/jrnl/journals/Journal.py +++ b/jrnl/journals/Journal.py @@ -298,7 +298,16 @@ def excluded(tags): and ( not contains or ( - any( + 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 From 989f5041805ce89619f716fa76d539dcaa9903d3 Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Fri, 10 May 2024 07:19:18 -0700 Subject: [PATCH 3/7] Run poe format --- jrnl/journals/Journal.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jrnl/journals/Journal.py b/jrnl/journals/Journal.py index cac154ba1..d7e9b0c0b 100644 --- a/jrnl/journals/Journal.py +++ b/jrnl/journals/Journal.py @@ -300,15 +300,15 @@ def excluded(tags): or ( strict and all( - substring in entry.title.casefold() + substring in entry.title.casefold() or substring in entry.body.casefold() for substring in contains_lower ) ) or ( - not strict + not strict and any( - substring in entry.title.casefold() + substring in entry.title.casefold() or substring in entry.body.casefold() for substring in contains_lower ) From beb860850401351b90247f6259d581fb0613e58a Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Fri, 10 May 2024 07:23:55 -0700 Subject: [PATCH 4/7] Fix unit test for contains to allow list instead of single value --- tests/unit/test_parse_args.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_parse_args.py b/tests/unit/test_parse_args.py index e30360201..2e2d5cd64 100644 --- a/tests/unit/test_parse_args.py +++ b/tests/unit/test_parse_args.py @@ -55,7 +55,7 @@ def test_empty(): def test_contains_alone(): - assert cli_as_dict("-contains whatever") == expected_args(contains="whatever") + assert cli_as_dict("-contains whatever") == expected_args(contains=["whatever"]) def test_debug_alone(): From 799fb8dd94fc0879eb443b5570ea51c47850f581 Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Fri, 10 May 2024 07:39:57 -0700 Subject: [PATCH 5/7] Add BDD tests for multiple contains with and without -and --- tests/bdd/features/search.feature | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/bdd/features/search.feature b/tests/bdd/features/search.feature index 79e2a84e4..79ddf0871 100644 --- a/tests/bdd/features/search.feature +++ b/tests/bdd/features/search.feature @@ -86,6 +86,36 @@ Feature: Searching in a journal | basic_folder.yaml | | basic_dayone.yaml | + Scenario Outline: Multiple -contains returns entries that match any + Given we use the config "" + When we run "jrnl -contains emojis -contains lorem --short" + Then we should get no error + And the output should contain "3 entries found" + And the output should be + 2020-08-29 11:11 Entry the first. + 2020-08-31 14:32 A second entry in what I hope to be a long series. + 2020-09-24 09:14 The third entry finally after weeks without writing. + + Examples: configs + | config_file | + | basic_onefile.yaml | + | basic_folder.yaml | + | basic_dayone.yaml | + + Scenario Outline: Multiple -contains with -and returns only entries that match all + Given we use the config "" + When we run "jrnl -contains emojis -contains nulla -and --short" + Then we should get no error + And the output should contain "1 entry found" + And the output should be + 2020-09-24 09:14 The third entry finally after weeks without writing. + + Examples: configs + | config_file | + | basic_onefile.yaml | + | basic_folder.yaml | + | basic_dayone.yaml | + Scenario Outline: Searching for a string within tag results Given we use the config "" When we run "jrnl @tagone -contains maybe" From afbb255cfa0430c34cf8bfab2a909669a033f8cd Mon Sep 17 00:00:00 2001 From: Ricardo Ruiz Date: Sun, 19 May 2024 13:51:37 +0200 Subject: [PATCH 6/7] Black version updated. --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index bd7df65db..b7c80f71b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,7 @@ rich = ">=12.2.0, <14.0.0" tzlocal = ">=4.0" # https://github.com/regebro/tzlocal/blob/master/CHANGES.txt [tool.poetry.dev-dependencies] -black = { version = ">=21.5b2", allow-prereleases = true } +black = { version = ">=23.11.0", allow-prereleases = true } ipdb = "*" mkdocs = ">=1.4" parse-type = ">=0.6.0" From a7fd316c02b3fb362a4421b6300f8096e486dc42 Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Mon, 20 May 2024 21:09:01 -0700 Subject: [PATCH 7/7] Revert pyproject.toml to appease poetry --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b7c80f71b..bd7df65db 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,7 @@ rich = ">=12.2.0, <14.0.0" tzlocal = ">=4.0" # https://github.com/regebro/tzlocal/blob/master/CHANGES.txt [tool.poetry.dev-dependencies] -black = { version = ">=23.11.0", allow-prereleases = true } +black = { version = ">=21.5b2", allow-prereleases = true } ipdb = "*" mkdocs = ">=1.4" parse-type = ">=0.6.0"