Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - add bracketed date support when composing new entry (#1915) #1918

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions jrnl/journals/Journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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)\\[([^\\]]+)\\] ")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: code is based on Journal._parse with slight adaptions. E.g. the default_hour and default_minute handling is added.

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("*")
Expand Down
15 changes: 15 additions & 0 deletions tests/bdd/features/datetime.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: is this behavior fine?

| 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
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: could be integrated into test above as config

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
Expand Down
25 changes: 25 additions & 0 deletions tests/bdd/features/star.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<config_file>"
When we run "jrnl <command>"
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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I assume the dayone Journal should also be supported without issues? Will have to check that

# | 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 "<config_file>"
When we run "jrnl -starred"
Expand Down