Skip to content

Commit

Permalink
Added 'yesterday,today,tomorrow' feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Rikiub committed Sep 23, 2023
1 parent 0116565 commit a1e5e5d
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions yournal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
from datetime import date
from datetime import date, timedelta
from pathlib import Path
import subprocess
import platform
Expand All @@ -23,6 +23,8 @@
ENV_DIR_NAME = "YOURNAL_DIR"
ENV_TEMPLATE_NAME = "YOURNAL_TEMPLATE"

DATE_NOW = date.today()


def parse_vars(text_to_parse: str) -> str:
"""Parse title and dates. Requires "arrow" package."""
Expand Down Expand Up @@ -93,12 +95,12 @@ def open_file_with_editor(file: Path) -> None:
subprocess.run([*editor_env, file])


def daily_note(directory: Path, template: Path = None) -> None:
def daily_note(date: str, directory: Path, template: Path = None) -> None:
"""open/create Daily Note."""
global SKIP_TEMPLATE_PARSE

directory.mkdir(parents=True, exist_ok=True)
daily_note = directory / f"{date.today()}.md"
daily_note = directory / str(f"{date}.md")

# open
if daily_note.exists():
Expand Down Expand Up @@ -148,19 +150,29 @@ def parseArguments() -> Namespace:
""",
)

date = parser.add_argument_group("date")
date.add_argument(
"date",
help="open daily note by date. default: today",
choices=["yesterday", "today", "tomorrow"],
default="today",
nargs="?",
type=str,
)

paths = parser.add_argument_group("paths")
paths.add_argument(
"-d",
"--directory",
help="directory where save your daily notes",
default=os.getenv(ENV_DIR_NAME) or Path.cwd(),
default=Path.cwd(),
type=Path,
)
paths.add_argument(
"-t",
"--template",
help="template file to parse",
default=os.getenv(ENV_TEMPLATE_NAME) or None,
default=None,
type=Path,
)

Expand Down Expand Up @@ -191,8 +203,28 @@ def parseArguments() -> Namespace:
if __name__ == "__main__":
args = parseArguments()

# set global vars
CUSTOM_EDITOR = args.editor
IGNORE_ENV = args.ignore
SKIP_TEMPLATE_PARSE = args.skip

daily_note(directory=args.directory, template=args.template)
if not args.ignore:
directory = os.getenv(ENV_DIR_NAME)
template = os.getenv(ENV_TEMPLATE_NAME)

if directory:
args.directory = Path(directory)
if template:
args.template = Path(template)

# parse date
date = args.date
if date == "yesterday":
date = DATE_NOW - timedelta(days=1)
elif date == "today":
date = DATE_NOW
elif date == "tomorrow":
date == DATE_NOW + timedelta(days=1)

# start
daily_note(date, directory=args.directory, template=args.template)

0 comments on commit a1e5e5d

Please sign in to comment.