feat: added --add-xyz options for non-interactive use
This commit is contained in:
42
src/main.sh
42
src/main.sh
@@ -35,14 +35,35 @@ __lines() {
|
|||||||
|
|
||||||
# Program starts here
|
# Program starts here
|
||||||
if [ "${1:-}" = "--help" ]; then
|
if [ "${1:-}" = "--help" ]; then
|
||||||
|
bn="$(basename "$0")"
|
||||||
shift
|
shift
|
||||||
echo "Usage: $(basename $0) [--help | --new [FILTER..] | --list [FILTER..] | [FILTER..] ]
|
echo "Usage: $bn [OPTION] [FILTER]...
|
||||||
|
|
||||||
|
[OPTION]
|
||||||
--help Show this help and exit
|
--help Show this help and exit
|
||||||
--new Create new entry and do not exit
|
|
||||||
--list List entries and exit
|
Git Integration:
|
||||||
--git-init Activate git usage and exit
|
--git-init Activate git usage and exit
|
||||||
--git <cmd> Run git command and exit
|
--git <cmd> Run git command and exit
|
||||||
|
|
||||||
|
Interactive Mode:
|
||||||
|
--new [FILTER..] Create new entry interactively and start
|
||||||
|
[FILTER..] Start with the specified filter
|
||||||
|
|
||||||
|
Non-Interactive Mode:
|
||||||
|
--list [FILTER..] List entries and exit
|
||||||
|
--add-note <summary> Read note from stdin and add it with the
|
||||||
|
specified summary
|
||||||
|
--add-task <summary> [<due>] Read task from stdin and add it with the
|
||||||
|
specified summary and optional due date
|
||||||
|
--add-jour <summary> Read journal from stdin and add it with the
|
||||||
|
specified summary
|
||||||
|
--collection <nr> Select collection to which the note, task, or
|
||||||
|
journal entry is added non-interactively. The
|
||||||
|
argument <nr> is the ordinal describing the
|
||||||
|
collection. It defaults to the starting value
|
||||||
|
of 1.
|
||||||
|
|
||||||
[FILTER]
|
[FILTER]
|
||||||
You may specify any of these filters. Filters can be negated using the
|
You may specify any of these filters. Filters can be negated using the
|
||||||
--no-... versions, e.g., --no-tasks. Multiple filters are applied in
|
--no-... versions, e.g., --no-tasks. Multiple filters are applied in
|
||||||
@@ -55,7 +76,17 @@ if [ "${1:-}" = "--help" ]; then
|
|||||||
--journal Show journal only
|
--journal Show journal only
|
||||||
--completed Show completed tasks only
|
--completed Show completed tasks only
|
||||||
--open Show open tasks only
|
--open Show open tasks only
|
||||||
--filter <query> Specify custom query"
|
--filter <query> Specify custom query
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
$bn --git log
|
||||||
|
$bn --new
|
||||||
|
$bn --journal
|
||||||
|
$bn --no-tasks --filter \"Beauregard\"
|
||||||
|
$bn --list --open
|
||||||
|
$bn --add-task \"Improve code to respect timezone information\" \"next month\"
|
||||||
|
cat proof.tex | $bn --add-journal \"Proof of Fixed-point Theorem\" --collection 2
|
||||||
|
"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -68,6 +99,9 @@ fi
|
|||||||
# Command line arguments
|
# Command line arguments
|
||||||
. "sh/cli.sh"
|
. "sh/cli.sh"
|
||||||
|
|
||||||
|
# Parse command-line filter (if any)
|
||||||
|
. "sh/filter.sh"
|
||||||
|
|
||||||
# Attachment handling
|
# Attachment handling
|
||||||
. "sh/attachment.sh"
|
. "sh/attachment.sh"
|
||||||
|
|
||||||
|
|||||||
142
src/sh/cli.sh
142
src/sh/cli.sh
@@ -1,5 +1,5 @@
|
|||||||
# Git
|
case "${1:-}" in
|
||||||
if [ "${1:-}" = "--git-init" ]; then
|
"--git-init")
|
||||||
shift
|
shift
|
||||||
if [ -n "${GIT:-}" ]; then
|
if [ -n "${GIT:-}" ]; then
|
||||||
err "Git already enabled"
|
err "Git already enabled"
|
||||||
@@ -13,9 +13,8 @@ if [ "${1:-}" = "--git-init" ]; then
|
|||||||
git -C "$ROOT" add -A
|
git -C "$ROOT" add -A
|
||||||
git -C "$ROOT" commit -m 'Initial commit: Start git tracking'
|
git -C "$ROOT" commit -m 'Initial commit: Start git tracking'
|
||||||
exit
|
exit
|
||||||
fi
|
;;
|
||||||
|
"--git")
|
||||||
if [ "${1:-}" = "--git" ]; then
|
|
||||||
shift
|
shift
|
||||||
if [ -z "${GIT:-}" ]; then
|
if [ -z "${GIT:-}" ]; then
|
||||||
err "Git not supported, run \`$0 --git-init\` first"
|
err "Git not supported, run \`$0 --git-init\` first"
|
||||||
@@ -23,78 +22,101 @@ if [ "${1:-}" = "--git" ]; then
|
|||||||
fi
|
fi
|
||||||
$GIT "$@"
|
$GIT "$@"
|
||||||
exit
|
exit
|
||||||
fi
|
;;
|
||||||
|
"--new")
|
||||||
# Generate new entry
|
|
||||||
if [ "${1:-}" = "--new" ]; then
|
|
||||||
shift
|
shift
|
||||||
__new
|
__new
|
||||||
fi
|
export next_filter=1
|
||||||
|
;;
|
||||||
# Switch to list mode
|
"--list")
|
||||||
if [ "${1:-}" = "--list" ]; then
|
|
||||||
shift
|
shift
|
||||||
|
export next_filter=1
|
||||||
export list_option=1
|
export list_option=1
|
||||||
fi
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
# Build query
|
if [ -z "${next_filter:-}" ]; then
|
||||||
while [ -n "${1:-}" ]; do
|
# else [FILTER] are the next options
|
||||||
|
# Here, we have --add-xyz with --collection or nothign
|
||||||
case "${1:-}" in
|
case "${1:-}" in
|
||||||
"--completed")
|
"--add-note" | "--add-task" | "--add-jour" | "--collection")
|
||||||
shift
|
noninteractive=1
|
||||||
cliquery="${cliquery:-} $FLAG_COMPLETED"
|
|
||||||
;;
|
;;
|
||||||
"--no-completed")
|
esac
|
||||||
|
if [ -n "${noninteractive:-}" ]; then
|
||||||
|
while [ -n "${1:-}" ]; do
|
||||||
|
case "$1" in
|
||||||
|
"--add-note" | "--add-task" | "--add-jour")
|
||||||
|
if [ -n "${add_option:-}" ]; then
|
||||||
|
err "What do you want to add?"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
add_option="$1"
|
||||||
shift
|
shift
|
||||||
cliquery="${cliquery:-} !$FLAG_COMPLETED"
|
summary=${1-}
|
||||||
|
if [ -z "$summary" ]; then
|
||||||
|
err "You did not give a summary"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
shift
|
||||||
|
if [ "$add_option" = "--add-task" ] && [ -n "${1:-}" ]; then
|
||||||
|
case "$1" in
|
||||||
|
"--"*)
|
||||||
|
continue
|
||||||
;;
|
;;
|
||||||
"--open")
|
*)
|
||||||
|
due=$(printf "%s" "$1" | tr -dc "[:alnum:][:blank:]")
|
||||||
shift
|
shift
|
||||||
cliquery="${cliquery:-} $FLAG_OPEN"
|
if [ -z "$due" ] || ! date -d "$due" >/dev/null 2>&1; then
|
||||||
|
err "Invalid due date"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
;;
|
;;
|
||||||
"--no-open")
|
esac
|
||||||
shift
|
fi
|
||||||
cliquery="${cliquery:-} !$FLAG_OPEN"
|
|
||||||
;;
|
;;
|
||||||
"--tasks")
|
"--collection")
|
||||||
shift
|
shift
|
||||||
cliquery="${cliquery:-} $FLAG_OPEN | $FLAG_COMPLETED"
|
collection="$(printf "%s" "$COLLECTION_LABELS" |
|
||||||
;;
|
cut -d ";" -f "${1:-}" 2>/dev/null |
|
||||||
"--no-tasks")
|
cut -d "=" -f 1 2>/dev/null)"
|
||||||
shift
|
if [ -z "$collection" ]; then
|
||||||
cliquery="${cliquery:-} !$FLAG_COMPLETED !$FLAG_OPEN"
|
err "Invalid collection"
|
||||||
;;
|
exit 1
|
||||||
"--notes")
|
fi
|
||||||
shift
|
|
||||||
cliquery="${cliquery:-} $FLAG_NOTE"
|
|
||||||
;;
|
|
||||||
"--no-notes")
|
|
||||||
shift
|
|
||||||
cliquery="${cliquery:-} !$FLAG_NOTE"
|
|
||||||
;;
|
|
||||||
"--journal")
|
|
||||||
shift
|
|
||||||
cliquery="${cliquery:-} $FLAG_JOURNAL"
|
|
||||||
;;
|
|
||||||
"--no-journal")
|
|
||||||
shift
|
|
||||||
cliquery="${cliquery:-} !$FLAG_JOURNAL"
|
|
||||||
;;
|
|
||||||
"--filter")
|
|
||||||
shift
|
|
||||||
cliquery="${cliquery:-} $1"
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
"--no-filter")
|
|
||||||
shift
|
|
||||||
cliquery="${cliquery:-} !$1"
|
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
err "Unknown option \"$1\""
|
err "Unknown non-interactive option: $1"
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
query=${cliquery:-!$FLAG_COMPLETED}
|
fi
|
||||||
export query
|
fi
|
||||||
|
|
||||||
|
if [ -n "${noninteractive:-}" ]; then
|
||||||
|
if [ -z "${add_option:-}" ]; then
|
||||||
|
err "Specified collection, but nothing to add"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [ -z "${collection:-}" ]; then
|
||||||
|
collection="$(
|
||||||
|
printf "%s" "$COLLECTION_LABELS" |
|
||||||
|
cut -d ";" -f 1 |
|
||||||
|
cut -d "=" -f 1
|
||||||
|
)"
|
||||||
|
fi
|
||||||
|
case "$add_option" in
|
||||||
|
"--add-note")
|
||||||
|
__add_note "$collection" "$summary"
|
||||||
|
;;
|
||||||
|
"--add-task")
|
||||||
|
__add_task "$collection" "$summary" "${due:-}"
|
||||||
|
;;
|
||||||
|
"--add-jour")
|
||||||
|
__add_jour "$collection" "$summary"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|||||||
61
src/sh/filter.sh
Normal file
61
src/sh/filter.sh
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
# Build query
|
||||||
|
while [ -n "${1:-}" ]; do
|
||||||
|
case "${1:-}" in
|
||||||
|
"--completed")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} $FLAG_COMPLETED"
|
||||||
|
;;
|
||||||
|
"--no-completed")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} !$FLAG_COMPLETED"
|
||||||
|
;;
|
||||||
|
"--open")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} $FLAG_OPEN"
|
||||||
|
;;
|
||||||
|
"--no-open")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} !$FLAG_OPEN"
|
||||||
|
;;
|
||||||
|
"--tasks")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} $FLAG_OPEN | $FLAG_COMPLETED"
|
||||||
|
;;
|
||||||
|
"--no-tasks")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} !$FLAG_COMPLETED !$FLAG_OPEN"
|
||||||
|
;;
|
||||||
|
"--notes")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} $FLAG_NOTE"
|
||||||
|
;;
|
||||||
|
"--no-notes")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} !$FLAG_NOTE"
|
||||||
|
;;
|
||||||
|
"--journal")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} $FLAG_JOURNAL"
|
||||||
|
;;
|
||||||
|
"--no-journal")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} !$FLAG_JOURNAL"
|
||||||
|
;;
|
||||||
|
"--filter")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} $1"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
"--no-filter")
|
||||||
|
shift
|
||||||
|
cliquery="${cliquery:-} !$1"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
err "Unknown option \"$1\""
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
query=${cliquery:-!$FLAG_COMPLETED}
|
||||||
|
export query
|
||||||
@@ -1,5 +1,100 @@
|
|||||||
# Interface to modify iCalendar files
|
# Interface to modify iCalendar files
|
||||||
|
|
||||||
|
# Wrapper to add entry from markdown file
|
||||||
|
#
|
||||||
|
# @input $1: path to markdown file
|
||||||
|
# @input $2: collection to add to
|
||||||
|
__add_from_md() {
|
||||||
|
tmpmd="$1"
|
||||||
|
shift
|
||||||
|
collection="$1"
|
||||||
|
shift
|
||||||
|
file=""
|
||||||
|
while [ -f "$file" ] || [ -z "$file" ]; do
|
||||||
|
uuid=$($UUIDGEN)
|
||||||
|
file="$ROOT/$collection/$uuid.ics"
|
||||||
|
done
|
||||||
|
tmpfile="$tmpmd.ics"
|
||||||
|
if awk -v uid="$uuid" "$AWK_NEW" "$tmpmd" >"$tmpfile"; then
|
||||||
|
if [ ! -d "$ROOT/$collection" ]; then
|
||||||
|
mkdir -p "$ROOT/$collection"
|
||||||
|
fi
|
||||||
|
mv "$tmpfile" "$file"
|
||||||
|
if [ -n "${GIT:-}" ]; then
|
||||||
|
$GIT add "$file"
|
||||||
|
$GIT commit -q -m "File added" -- "$file"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
rm -f "$tmpfile"
|
||||||
|
err "Failed to create new entry."
|
||||||
|
fi
|
||||||
|
rm "$tmpmd"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Noninteractively add note, and fill description from stdin
|
||||||
|
#
|
||||||
|
# @input $1: Collection
|
||||||
|
# @input $2: Summary
|
||||||
|
__add_note() {
|
||||||
|
collection="$1"
|
||||||
|
shift
|
||||||
|
summary="$1"
|
||||||
|
shift
|
||||||
|
tmpmd=$(mktemp --suffix='.md')
|
||||||
|
{
|
||||||
|
echo "# $summary"
|
||||||
|
echo ""
|
||||||
|
} >"$tmpmd"
|
||||||
|
if [ ! -t 0 ]; then
|
||||||
|
cat /dev/stdin >>"$tmpmd"
|
||||||
|
fi
|
||||||
|
__add_from_md "$tmpmd" "$collection"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Noninteractively add task, and fill description from stdin
|
||||||
|
#
|
||||||
|
# @input $1: Collection
|
||||||
|
# @input $2: Summary
|
||||||
|
# @input $3: Due date (optional)
|
||||||
|
__add_task() {
|
||||||
|
collection="$1"
|
||||||
|
shift
|
||||||
|
summary="$1"
|
||||||
|
shift
|
||||||
|
due="${1:-}"
|
||||||
|
tmpmd=$(mktemp --suffix='.md')
|
||||||
|
{
|
||||||
|
echo "::: <| $due"
|
||||||
|
echo "# $summary"
|
||||||
|
echo ""
|
||||||
|
} >"$tmpmd"
|
||||||
|
if [ ! -t 0 ]; then
|
||||||
|
cat /dev/stdin >>"$tmpmd"
|
||||||
|
fi
|
||||||
|
__add_from_md "$tmpmd" "$collection"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Noninteractively add jounral, and fill description from stdin
|
||||||
|
#
|
||||||
|
# @input $1: Collection
|
||||||
|
# @input $2: Summary
|
||||||
|
__add_jour() {
|
||||||
|
collection="$1"
|
||||||
|
shift
|
||||||
|
summary="$1"
|
||||||
|
shift
|
||||||
|
tmpmd=$(mktemp --suffix='.md')
|
||||||
|
{
|
||||||
|
echo "::: |> <!-- keep this line to associate the entry to _today_ -->"
|
||||||
|
echo "# $summary"
|
||||||
|
echo ""
|
||||||
|
} >"$tmpmd"
|
||||||
|
if [ ! -t 0 ]; then
|
||||||
|
cat /dev/stdin >>"$tmpmd"
|
||||||
|
fi
|
||||||
|
__add_from_md "$tmpmd" "$collection"
|
||||||
|
}
|
||||||
|
|
||||||
# Toggle completed status of VTODO
|
# Toggle completed status of VTODO
|
||||||
#
|
#
|
||||||
# @input $1: Relative path to iCalendar file
|
# @input $1: Relative path to iCalendar file
|
||||||
|
|||||||
Reference in New Issue
Block a user