November 11, 2014

Creating clj-tdo, a command line todo list, part 2.

In the previous post we created a very simple command line todo list in clojure.

In this post we will be making a few improvements. You can get the code in its current state from here, update to the part-1 tag.

Pulling out task.clj

Lets start by pulling out some of the task related functionality into its own namespace. Create a new file src/clj-tdo/task.clj.

If we pull out the Task record, the overdue?,due-on? and finish functions we should end up with something like this.

I've also added a create function to get rid of the date mangling and task creation from handlers.clj.

We now need to make some changes to handlers.clj in order to make use of the functions we created in task.clj. First, we require [clj-tdo.task :as task].

Then we need to update the usages of overdue?, due-on? and finish so that they reference the namespace. This should leave us with a handlers.clj like so.

I think its time to show the horrible prn-task function some love. In its current state, it looks like this.

Initially we can start by moving it into src/clj-tdo/task.clj. We will also need to move the style-str function and the referenced clansi.core namespace. This doesn't really feel like the right place to put style-str, we will find somewhere better for it eventually. src/clj-tdo/task.clj now looks like this.

I think we should improve prn-task by pulling out some of the repeated logic into a new namespace, lets create src/clj-tdo/rendering.clj. I think this is also a reasonable place to move the style-str function to. Lets start with this.

Obviously we need to update src/clj-tdo/task.clj so that it uses style-str from src/clj-tdo/rendering.clj, I'll leave that out here though.

The next thing I'd like to do is remove the duplication of checking a condition and outputing a "checkbox". For that I'll create a checkbox function in src/clj-tdo/rendering.clj.

I'd also like to make padding strings a little neater. So I'll create pad-right.

I also add a field function and then realise I can make use of it in checkbox.

Now, I update the prn-task function to make use of the functions we just created.

Notice that I also made the "printer" a function argument, in our case we will be passing through println in src/clj-tdo/handlers.clj. I'm still not too happy with the style and colour mapping but we can look at that next time.

The code in its current state is available here, update to the part-2 tag.

Tags: list clojure todo