112 lines
3.4 KiB
Bash
Executable File
112 lines
3.4 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
# This file defines todo - a minimalist to-do manager.
|
|
|
|
# Copyright (C) 2020 James Tomasino
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
version="2020.07.10"
|
|
|
|
show_help() {
|
|
cat > /dev/stdout << END
|
|
todo - Command Line Todo List Manager
|
|
|
|
OPTIONAL FLAGS:
|
|
[no flags] Show current TODO list
|
|
-e open TODO list in \$EDITOR
|
|
-a message Add a message to TODO list
|
|
-d [number] or [string] Delete message(s) that match parameter
|
|
-x Show archived messages
|
|
-v Show current version number
|
|
-h Show this help
|
|
END
|
|
}
|
|
|
|
TODO="${TODO:-${HOME}/todo.txt}"
|
|
|
|
# If we are in a tmux session, name the file with the session name
|
|
# If not in tmux, use the full $TODO env var for path/file
|
|
if printf "%s" "$TERM" | grep -Fq screen && test "$TMUX" ; then
|
|
sessname=$(tmux display -p '#S')
|
|
# if session name is numeric, it's not descriptive enough
|
|
# to warrant renaming the todo file. Ignore it and use default
|
|
if printf "%s" "$sessname" | grep -Eq '^[+-]?[0-9]+$'; then
|
|
TODOFILE=$TODO
|
|
TODOARCHIVEFILE=${TODO%.*}.archive.txt
|
|
else
|
|
todopath=$(dirname "$TODO")
|
|
TODOFILE=$todopath/$sessname".txt"
|
|
TODOARCHIVEFILE=$todopath/$sessname".archive.txt"
|
|
fi
|
|
else
|
|
TODOFILE=$TODO
|
|
TODOARCHIVEFILE=${TODO%.*}.archive.txt
|
|
fi
|
|
|
|
while getopts "hvea:d:x" arg; do
|
|
case "$arg" in
|
|
h) # help
|
|
show_help
|
|
exit 0
|
|
;;
|
|
v) # version
|
|
printf "%s\\n" "$version"
|
|
exit 0
|
|
;;
|
|
e) # edit file
|
|
printf "Opening %s in %s\\n" "$TODOFILE" "${EDITOR:-nano}"
|
|
${EDITOR:-nano} "${TODOFILE}"
|
|
exit 0
|
|
;;
|
|
a) # add item
|
|
shift
|
|
message="$*"
|
|
printf "%s\\n" "$message" >> "$TODOFILE"
|
|
exit 0
|
|
;;
|
|
x) # view archive
|
|
if [ -f "$TODOARCHIVEFILE" ] ; then
|
|
cat "$TODOARCHIVEFILE"
|
|
fi
|
|
exit 0
|
|
;;
|
|
d) # delete item(s) matching pattern
|
|
if printf "%s" "$OPTARG" | grep -Eq '^[+-]?[0-9]+$'; then
|
|
match=$(sed -n "${OPTARG}p" "$TODOFILE" 2> /dev/null)
|
|
sed -i.bak -e "${OPTARG}d" "$TODOFILE" 2> /dev/null && rm "$TODOFILE".bak
|
|
if [ -n "$match" ]; then
|
|
printf "%s - %s\\n" "$(date +"%Y-%m-%d %H:%M:%S")" "$match" >> "$TODOARCHIVEFILE"
|
|
fi
|
|
else
|
|
match=$(sed -n "/$OPTARG/p" "$TODOFILE" 2> /dev/null)
|
|
sed -i.bak -e "/$OPTARG/d" "$TODOFILE" 2> /dev/null && rm "$TODOFILE".bak
|
|
if [ -n "$match" ]; then
|
|
printf "%s - %s\\n" "$(date +"%Y-%m-%d %H:%M:%S")" "$match" >> "$TODOARCHIVEFILE"
|
|
fi
|
|
fi
|
|
exit 0
|
|
;;
|
|
*)
|
|
exit 1
|
|
esac
|
|
done
|
|
|
|
# When no arguments are passed, we default to showing the file
|
|
if [ -f "$TODOFILE" ] ; then
|
|
awk '{ print NR, "-", $0 }' "$TODOFILE"
|
|
fi
|