markdone/md.sh

108 lines
2.3 KiB
Bash
Raw Permalink Normal View History

2018-04-27 19:01:35 +00:00
#! /bin/bash
set -e
2018-04-27 20:07:15 +00:00
# Used to set the base directory to query. Defaults to current directory
MARKDONE_BASE_DIR=${MARKDONE_BASE_DIR:-.}
2018-04-27 19:01:35 +00:00
function list_notes {
echo "Notes"
echo "****************"
query="$@"
if [ "$query" == "" ]; then
query="."
fi
2018-04-27 20:07:15 +00:00
grep -l "$query" -R "$MARKDONE_BASE_DIR"
2018-04-27 19:01:35 +00:00
}
function list_tasks {
echo "Tasks"
echo "****************"
scope=$1
query=""
case "$scope" in
all)
inner_task=" xX"
query="${@:2}"
;;
'done'|closed|complete)
inner_task="xX"
query="${@:2}"
;;
open|incomplete)
inner_task=" "
query="${@:2}"
;;
*)
inner_task=" "
query="$@"
;;
esac
query=".*$query"
2018-04-27 20:07:15 +00:00
grep "^[[:blank:]]*[^[:blank:]-][[:blank:]]*[*]\?[[:blank:]]*\[[$inner_task]\?\]$query" -R "$MARKDONE_BASE_DIR" -h
2018-04-27 19:01:35 +00:00
}
function echo_help {
if [ "$1" == "notes" ]; then
cat << EOF
usage: md.sh notes [query]
query: regex to search for in notes. [defaults to .*]
Tags are generally represented as !tagname
EOF
elif [ "$1" == "tasks" ]; then
cat << EOF
usage: md.sh tasks [scope] [query]
scope: used to limit to completed or include all tasks
open|incomplete: include only open or incomplete tasks [default]
done|closed|complete: include only completed tasks
all: include all tasks, regardless of status
query: regex to search for in each task. [defaults to .*]
Tags are generally represented as !tagname
EOF
else
cat << EOF
usage: md.sh type [args]
type: the type of markdone object to query
notes: list or query notes
tasks: list or query tasks
help: display this help text or help for a sub command
EOF
fi
}
function main {
subcmd=$1
args=${@:2}
case "$subcmd" in
notes)
list_notes $args
;;
tasks)
list_tasks $args
;;
all):
list_notes $args
echo ""
list_tasks $args
;;
help)
echo_help $args
;;
*)
echo "Unknown command: $subcmd"
echo_help $args
exit 1
;;
esac
}
main $@