From d12d5c0c104ea417cf7f57d5e662255a3802c89c Mon Sep 17 00:00:00 2001 From: Christian Wutte <958417+chwt@users.noreply.github.com> Date: Sun, 22 Sep 2024 22:30:57 +0200 Subject: [PATCH] Feature - add bracketed date support when composing new entry (#1915) Bracketed dates like [2020-01-01] will be parsed and used as the entry date, thus be removed from the entry text. --- jrnl/journals/Journal.py | 27 +++++++++++++++++++++++++-- tests/bdd/features/datetime.feature | 15 +++++++++++++++ tests/bdd/features/star.feature | 25 +++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/jrnl/journals/Journal.py b/jrnl/journals/Journal.py index d7e9b0c0b..1ae80e362 100644 --- a/jrnl/journals/Journal.py +++ b/jrnl/journals/Journal.py @@ -187,6 +187,7 @@ def _parse(self, journal_txt: str) -> list[Entry]: new_date = time.parse(date_blob, bracketed=True) if new_date: + # Fill in text of entry created in previous loop if entries: entries[-1].text = journal_txt[last_entry_pos : match.start()] last_entry_pos = match.end() @@ -363,7 +364,7 @@ def new_entry(self, raw: str, date=None, sort: bool = True) -> Entry: raw = raw.replace("\\n ", "\n").replace("\\n", "\n") # Split raw text into title and body sep = re.search(r"\n|[?!.]+ +\n?", raw) - first_line = raw[: sep.end()].strip() if sep else raw + first_line = raw[:sep.end()].strip() if sep else raw starred = False if not date: @@ -376,7 +377,29 @@ def new_entry(self, raw: str, date=None, sort: bool = True) -> Entry: ) if date: # Parsed successfully, strip that from the raw text starred = raw[:colon_pos].strip().endswith("*") - raw = raw[colon_pos + 1 :].strip() + raw = raw[(colon_pos + 1):].strip() + else: + date_blob_re = re.compile("(?:^|\n)\\[([^\\]]+)\\] ") + match = date_blob_re.search(raw) + if match is not None: + date_blob = match.groups()[0] + try: + date = datetime.datetime.strptime( + date_blob, self.config["timeformat"] + ) + except ValueError: + # Passing in a date that had brackets around it + date = time.parse( + date_blob, + bracketed=True, + default_hour=self.config["default_hour"], + default_minute=self.config["default_minute"], + ) + + if date: # Parsed successfully, strip that from the raw text + starred = raw[:match.start()].strip().endswith("*") + raw = raw[match.end():].strip() + starred = ( starred or first_line.startswith("*") diff --git a/tests/bdd/features/datetime.feature b/tests/bdd/features/datetime.feature index 1e7ae7221..900e436fb 100644 --- a/tests/bdd/features/datetime.feature +++ b/tests/bdd/features/datetime.feature @@ -51,6 +51,13 @@ Feature: Reading and writing to journal with custom date formats | little_endian_dates.yaml | 2032-02-01: Test. | 01.02.2032 09:00 Test. | | little_endian_dates.yaml | 2020-01-01: Test. | 01.01.2020 09:00 Test. | | little_endian_dates.yaml | 2020-12-31: Test. | 31.12.2020 09:00 Test. | + # @todo: is it fine that default time be used here and not 00:00? + | little_endian_dates.yaml | [2020-12-31] bracket. | 31.12.2020 09:00 bracket. | + | little_endian_dates.yaml | [2020-12-31 10:00 PM] brkt. | 31.12.2020 22:00 brkt. | + | little_endian_dates.yaml | [2020-12-31]: brkt colon. | 31.12.2020 09:00 brkt colon. | + | little_endian_dates.yaml | [2020-12-31 12:34]: b colon. | 31.12.2020 12:34 b colon. | + | little_endian_dates.yaml | [2019-02-29] brkt neg. | [2019-02-29] brkt neg. | + | little_endian_dates.yaml | [2020-08-32] brkt neg. | [2020-08-32] brkt neg. | Scenario Outline: Searching for dates with custom date @@ -78,6 +85,14 @@ Feature: Reading and writing to journal with custom date formats Then the output should contain "10.05.2013 09:00 I saw Elvis." And the output should contain "He's alive." + Scenario: Writing an entry at the prompt with custom date in bracket format + Given we use the config "little_endian_dates.yaml" + When we run "jrnl" and type "[2013-05-10 12:34] I saw Elvis. He's alive." + Then we should get no error + When we run "jrnl -999" + Then the output should contain "10.05.2013 12:34 I saw Elvis." + And the output should contain "He's alive." + Scenario: Viewing today's entries does not print the entire journal # see: https://github.com/jrnl-org/jrnl/issues/741 diff --git a/tests/bdd/features/star.feature b/tests/bdd/features/star.feature index d160b9aa4..3cb44d675 100644 --- a/tests/bdd/features/star.feature +++ b/tests/bdd/features/star.feature @@ -16,6 +16,31 @@ Feature: Starring entries | empty_folder.yaml | | dayone.yaml | + Scenario Outline: Starring an entry with bracketed date will mark it in the journal file + Given we use the config "" + When we run "jrnl " + Then we should get no error + When we run "jrnl -on 2013-07-20 -starred" + Then the output should contain "2013-07-20 09:00 Best day of my life!" + + Examples: configs + | config_file | command | + | simple.yaml | [2013-07-20] Best day of my life! * | + | empty_folder.yaml | [2013-07-20] Best day of my life! * | + # Note: this one fail due to whitespace, cmp. next config +# | dayone.yaml | [2013-07-20] Best day of my life! * | + | dayone.yaml | [2013-07-20] Best day of my life!* | + | simple.yaml | [2013-07-20]*: Best day of my life! | + | empty_folder.yaml | [2013-07-20]*: Best day of my life! | + | dayone.yaml | [2013-07-20]*: Best day of my life! | + | simple.yaml | [2013-07-20] * : Best day of my life! | + | empty_folder.yaml | [2013-07-20] * : Best day of my life! | + | dayone.yaml | [2013-07-20] * : Best day of my life! | + | simple.yaml | [2013-07-20] * Best day of my life! | + | empty_folder.yaml | [2013-07-20] * Best day of my life! | + # Note: this one fails in having the star in the output too +# | dayone.yaml | [2013-07-20] * Best day of my life! | + Scenario Outline: Filtering by starred entries will show only starred entries Given we use the config "" When we run "jrnl -starred"