Building simplicidade.org: notes, projects, and occasional rants

todo.sh

I'm trying to use the todo.sh organizer to sort some of my work. Usually I use small 3 by 4 cards, and they are great to collect stuff to do, but they are not that great when I need to organize them into projects and define priorities.

First impressions are good. It's a simple script with a simple command line interface.

One of the first things I did was adding a short alias, t, as suggested at their site. This cuts down a lot of typing but being a lazy bastard myself, I wanted more.

So I wrote this:

# todo.sh completion by Pedro Melo <[email protected]>
# 
# for updates see: http://www.simplicidade.org/notes/archives/2006/07/todosh.html

_todo_sh()
{
  local cur prev commands options command

  TODOSHRC=${TODOSHRC:-${HOME}/.todo}
  if [ -r $TODOSHRC ] ; then
    . $TODOSHRC
  fi

  if [ ! -r $TODO_FILE ] ; then
    echo "ERROR: cannot read todo.txt file."
    echo "Make sure TODOSHRC is set with the correct .todo config file";
    return 0
  fi

  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  prev=${COMP_WORDS[COMP_CWORD-1]}

  commands='add append archive contexts del do list listpri \
            prepend pri projects replace remdup report'
  options="-d -p -q -v"

  if [[ $COMP_CWORD -eq 1 ]] ; then
    if [[ ${cur} == -* ]] ; then
     COMPREPLY=( $( compgen -W "$options" -- $cur ) )
    else
      COMPREPLY=( $( compgen -W "$commands" -- $cur ) )
    fi

    return 0
  fi

  case "${prev}" in
    @(add|list))
      local projects=$(egrep -o 'p:\w+' $TODO_FILE | sort | uniq -c | sort -rn | awk '{ print $2 }')
      local contexts=$(egrep -o '@\w+' $TODO_FILE | sort | uniq -c | sort -rn | awk '{ print $2 }')
      COMPREPLY=( $(compgen -W "${projects} ${contexts}" -- ${cur}) )
      return 0
      ;;

    @(append|del|do|prepend|pri|replace))
      local n_todos=$(wc -l $TODO_FILE)
      local tasks=$(seq 1 $n_todos)
      COMPREPLY=( $(compgen -W "${tasks}" -- ${cur}) )
      return 0
      ;;

    listpri)
      local pris=$(egrep -o '\(\w+\)' ~/Documents/todo/todo.txt | cut -c2 | sort | uniq -c | sort -rn | awk '{ print $2 }')
      COMPREPLY=( $(compgen -W "${pris}" -- ${cur}) )
      return 0
      ;;

    *)
      ;;
  esac

  return 0
}
complete -F _todo_sh -o default todo.sh

It's a bash_completion script. Copy & paste it into your own ~/.bash_completion.d/todo or the system /etc/bash_completion.d/todo.

It completes all the options and commands. It also completes @contexts and p:projects in list and add. It completes task numbers on all the commands that use them, and completes priorities in listpri.

It assumes that the todo.sh configuration file is at ~/.todo. If in your case you have it in a different place, you can set the environment variable TODOSHRC.

If you, like me, added your own alias shortcut to todo.sh, add this line after the alias definition:

complete -F _todo_sh -o default t

In my case, I was using the alias t for todo.sh. If you use another alias, replace the last t with your chosen alias.

The completion can be made smarter. Some commands need to expand to a task number and then free text, and inside the free text, expanding to @contexts or p:projects would be useful.