Skip to content

Instantly share code, notes, and snippets.

@samstokes
Last active March 4, 2024 00:51
Show Gist options
  • Save samstokes/5392339 to your computer and use it in GitHub Desktop.
Save samstokes/5392339 to your computer and use it in GitHub Desktop.
A todo management system in a gist
#!/bin/bash -e
if [[ $# > 0 ]]; then
case "$1" in
-h | -\? | --help )
{
echo "Add a todo:"
echo " todo Reformulate the widget plans."
echo "See what you have to do:"
echo " todo"
echo "After you've done item 4:"
echo " todo -4"
echo "Count items left to do:"
echo " todo -c"
echo "Print todos without line numbers:"
echo " todo --raw"
echo "Read todos from cat: and git: lines:"
echo " todo -g"
echo "Grep git in specific directories for TODOs:"
echo " todo -g src test"
echo "Edit the todo list directly (using $EDITOR):"
echo " todo -e"
} >&2
;;
-c | --count )
if [ -r TODO ]; then
exec wc -l TODO
else
echo 0 TODO
fi
;;
--raw )
if [ -r TODO ]; then
exec cat TODO
fi
;;
-e | --edit )
shift
exec $EDITOR TODO "$@"
;;
-g | --git )
shift
sed -n 's/^cat:\(.*\)/\1/p' TODO | xargs cat
if [[ $# -gt 0 ]]; then
for i in "$@"; do echo "$i"; done
else
sed -n 's/^git:\(.*\)/\1/p' TODO
fi | xargs git grep TODO || echo No TODOs known to git!
exec "$0"
;;
-* | x* )
lineno=$(cut -c2- <<<"$1")
sed -i "$lineno d" TODO
echo Deleted item $lineno. >&2
echo
exec "$0"
;;
* )
echo "$@" >> TODO
;;
esac
elif [ -r TODO ]; then
if grep -q . TODO; then
awk 'BEGIN { n = 1 } { printf "[%d]\t%s\n", n, $0; n = n + 1 }' TODO
else
echo Nothing to do!
fi
else
echo Nothing to do!
fi
@samstokes
Copy link
Author

If I'm working on something in a terminal (coding, code review, documenting), I often want somewhere to jot down things I just thought of, so I can come back to them later. They tend to be too fine-grained / low-level / transient to keep in a proper task manager or bug tracker. A file in the working directory is just right.

This makes it a tiny bit easier to maintain such a file, without sacrificing the power of plain text. Want to filter tasks? grep. Need to edit or reorder? $EDITOR is there for you.

It's also, I think, a nice example of how useful - and readable - a brief shell script can be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment