From e682d2799415d581e4728ff0d992303382d8fdc0 Mon Sep 17 00:00:00 2001 From: Jims Date: Tue, 12 Nov 2019 11:53:12 -0500 Subject: [PATCH 1/3] added case-insensitive searching of entry title and body using a -S argument --- jrnl/Journal.py | 4 +++- jrnl/cli.py | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/jrnl/Journal.py b/jrnl/Journal.py index f6813222..ab52cd00 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -187,7 +187,7 @@ class Journal: tag_counts = {(tags.count(tag), tag) for tag in tags} 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, search_plain=None, exclude=[]): """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 @@ -209,6 +209,7 @@ class Journal: # 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 excluded = lambda tags: len([tag for tag in tags if tag in excluded_tags]) > 0 + result = [ entry for entry in self.entries if (not tags or tagged(entry.tags)) @@ -216,6 +217,7 @@ class Journal: and (not start_date or entry.date >= start_date) and (not end_date or entry.date <= end_date) and (not exclude or not excluded(entry.tags)) + and (not search_plain or (search_plain.lower() in entry.title.lower() or search_plain.lower() in entry.body.lower())) ] self.entries = result diff --git a/jrnl/cli.py b/jrnl/cli.py index 3cf67030..ea8de891 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -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.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('-S', '-search', dest='search_plain', 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('-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') @@ -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)): compose = False 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.search_plain)): # Any sign of displaying stuff? compose = False 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, short=args.short, starred=args.starred, - exclude=args.excluded) + exclude=args.excluded, + search_plain=args.search_plain) journal.limit(args.limit) # Reading mode From fd5a08a4b27b83de3fa1c0067e9cf79a782d605d Mon Sep 17 00:00:00 2001 From: Jims Date: Thu, 14 Nov 2019 18:39:39 -0500 Subject: [PATCH 2/3] added search test --- features/searching.feature | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 features/searching.feature diff --git a/features/searching.feature b/features/searching.feature new file mode 100644 index 00000000..52e1ba54 --- /dev/null +++ b/features/searching.feature @@ -0,0 +1,11 @@ +Feature: Searching + + Scenario: Searching for a string + Given we use the config "basic.yaml" + When we run "jrnl -S life" + Then we should get no error + and the output should be + """ + 2013-06-10 15:40 Life is good. + | But I'm better. + """ From 9b6b788af459fa826b70a66bf750f2228784e32b Mon Sep 17 00:00:00 2001 From: Jims Date: Sun, 1 Dec 2019 23:03:38 -0500 Subject: [PATCH 3/3] Renamed searching to contains. Made changes as per pull-request: https://github.com/jrnl-org/jrnl/pull/740 --- features/contains.feature | 29 +++++++++++++++++++++++++++++ features/searching.feature | 11 ----------- jrnl/Journal.py | 6 ++++-- jrnl/cli.py | 6 +++--- 4 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 features/contains.feature delete mode 100644 features/searching.feature diff --git a/features/contains.feature b/features/contains.feature new file mode 100644 index 00000000..5813be20 --- /dev/null +++ b/features/contains.feature @@ -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" diff --git a/features/searching.feature b/features/searching.feature deleted file mode 100644 index 52e1ba54..00000000 --- a/features/searching.feature +++ /dev/null @@ -1,11 +0,0 @@ -Feature: Searching - - Scenario: Searching for a string - Given we use the config "basic.yaml" - When we run "jrnl -S life" - Then we should get no error - and the output should be - """ - 2013-06-10 15:40 Life is good. - | But I'm better. - """ diff --git a/jrnl/Journal.py b/jrnl/Journal.py index ab52cd00..7360b385 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -187,7 +187,7 @@ class Journal: tag_counts = {(tags.count(tag), tag) for tag in tags} 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, search_plain=None, 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. tags is a list of tags, each being a string that starts with one of the @@ -209,6 +209,8 @@ class Journal: # 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 excluded = lambda tags: len([tag for tag in tags if tag in excluded_tags]) > 0 + if contains: + contains_lower = contains.casefold() result = [ entry for entry in self.entries @@ -217,7 +219,7 @@ class Journal: and (not start_date or entry.date >= start_date) and (not end_date or entry.date <= end_date) and (not exclude or not excluded(entry.tags)) - and (not search_plain or (search_plain.lower() in entry.title.lower() or search_plain.lower() in entry.body.lower())) + and (not contains or (contains_lower in entry.title.casefold() or contains_lower in entry.body.casefold())) ] self.entries = result diff --git a/jrnl/cli.py b/jrnl/cli.py index ea8de891..b1454679 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -32,7 +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.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('-S', '-search', dest='search_plain', help='View entries containing a specific string') + 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('-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') @@ -65,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)): compose = False export = True - elif any((args.start_date, args.end_date, args.on_date, args.limit, args.strict, args.starred, args.search_plain)): + elif any((args.start_date, args.end_date, args.on_date, args.limit, args.strict, args.starred, args.contains)): # Any sign of displaying stuff? compose = False elif args.text and all(word[0] in config['tagsymbols'] for word in " ".join(args.text).split()): @@ -238,7 +238,7 @@ def run(manual_args=None): short=args.short, starred=args.starred, exclude=args.excluded, - search_plain=args.search_plain) + contains=args.contains) journal.limit(args.limit) # Reading mode