Improving my helper script.

by Tim

It's really nice being able to write and build your own custom tools to help you out. I updated my little helper script to be able to write posts in other dates other than today, and add the title in the cli prompt if wanted. I'm writing this post for the future!

And maybe I'll write a few posts ahead too.

But, code

#!/bin/python3
from datetime import date, timedelta
from subprocess import call
from pathlib import Path

import click

@click.command()
@click.option('-n', default=0, help='Number of days ahead to create this post, default being 0 (today)')
@click.option('-t', default='x', help='Sets the title. Defaults to x.')
def make_post(n, t):
    d = date.today() + timedelta(days=n)
    filename = str(d) + ".md"

    frontmatter = '+++\ntitle = "{0}"\ndate = {1}\n+++\n\n'

    frontmatter = frontmatter.format(t, d)

    empty = True
    
    if Path(filename).is_file():
        empty = False

    with open(filename, 'a') as f:
        if empty:
            f.write(frontmatter)
    call(['vim', filename])

if __name__ == '__main__':
    make_post()

Just nice little improvements, like giving it an actual cli interface with click.