An 0.1 todo command line script

I think I’m going to make an attempt at live-blogging this project. A post every day or two, with code snippets at least, maybe the full source because what do I give a crap about the source.

I DO need to install a code formatting plug-in for wordpress. Until then, code snippets are going to be just awful things wrapped in

<pre> tags.

So…

After I posted that yesterday (which was actually a simple electronic journal entry that I finished and said “meh, post it”) I opened an editor and started dicking around with an interactive console-based python script, just to see how far I could go in one shot.

It’s funny because it has add/list/save/load functionality (automatically loading on startup, saving on add.) There’s no removal/mark as completed function yet.

But imMEDIATELY my brain started firing off in all kinds of funky directions.

So here’s the 0.1 version (well, it’s probably 0.0.0.0.0.0.0.1, but who’s counting.) It has the most basic functionality, and a non-interactive option for just spitting the list to stdout.

Call it with nothing enters the interactive command loop:

todo.py

Call with ‘l’ (for list) and it’ll just spit the current todo list to stdout.

todo.py l

#!python

import os
import sys

filename = 'todo.dat'
items = list()

def save_items():
    fh = open(filename,'w')
    for item in items:
        if item != '':
            print(item,file =fh)
    fh.close()

def load_items():
    if not os.path.isfile(filename):
        return
    
    fh = open(filename,'r')
    eof = False
    while not eof:
        line = fh.readline()
        if not line :
            eof = True
        items.append(line.strip())

def add_item(foo):
    if len(foo) >2:
        items.append(foo[2:])
    else:
        item = ' '
        while item is not '':
            item = input("+ ")
            items.append(item)
    save_items()

def list_items():
    for item in items:
        print("\t",item)

def complete_item(foo):
    item_to_complete = None
    if len(foo) >2:
        item_to_complete = int(foo[2:])
        del items[item_to_complete]
        save_items()


def command_loop():
    bail = False
    while not bail:

        foo = input("> ")
        print(foo)

        if foo[0] == 'q' or foo[0] == 'Q':
            bail = True
        elif foo[0] == 'a':
            add_item(foo)
        elif (foo[0] == 'c' or foo[0] == 'f'):
            complete_item(foo)
        elif foo[0] == 'l':
            list_items()
            
def run():
    load_items()

    if len(sys.argv) > 1:
        if sys.argv[1] == 'l':
            list_items()
    else:
        command_loop()

if __name__ == '__main__':
    run()

So what’s this then? Here’s a script that maintains a local text file with a static name and gives you a couple commands (add, list, complete, quit) represented as one letter commands, to maintain a list, one item per line in the file. There’s a LOT wrong with it. BUT it works. It’s not production ready, and it’s particularly inelegant and it has no damn tests. All it’s supposed to be is a simple sketch, the code equivalent of a functional whiteboard diagram.

Next time I’ll either start ripping it apart for inclusion of more features, or for a serious restart. We’ll see. I’m still winging it.

Leave a Reply

Your email address will not be published. Required fields are marked *