Skip to content

Instantly share code, notes, and snippets.

@mattd
Created May 13, 2024 19:05
Show Gist options
  • Save mattd/f56c178e8721a0fd0394b1318642a759 to your computer and use it in GitHub Desktop.
Save mattd/f56c178e8721a0fd0394b1318642a759 to your computer and use it in GitHub Desktop.
open_dated_note
#!/usr/bin/env python3
import glob
import os
import shutil
import subprocess
import time
today_frags = time.strftime("%Y-%m-%d").split("-")
def get_next_ordinal(notes):
if len(notes) == 0:
ordinal = 1
else:
ordinal_frag = notes[-1].split("-")[-1].split(".")[0]
ordinal = int(ordinal_frag) + 1
return f'{ordinal:02}'
def get_sorted_notes_for_today():
all_notes = glob.glob("*.md")
notes_for_today = [
note for note in all_notes if today_frags == note.split("-")[0:3]
]
return sorted(notes_for_today)
def get_filename():
notes = get_sorted_notes_for_today()
ordinal = get_next_ordinal(notes)
return "-".join(today_frags) + "-{ordinal}.md".format(ordinal=ordinal)
def open_dated_note():
command_frags = os.environ["EDITOR"].split(" ")
command_frags[0] = shutil.which(command_frags[0])
command_frags.append(get_filename())
subprocess.run(command_frags)
open_dated_note()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment