2021-08-13 12:55:12 -04:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
notes() {
|
2021-10-29 10:41:50 -04:00
|
|
|
local NOTES_DIR="${NOTES_DIR:-${HOME}/notes}"
|
2021-09-27 09:50:24 -04:00
|
|
|
local -r EDITOR="${EDITOR:-vim}"
|
|
|
|
local EDITOR_OPTIONS=()
|
|
|
|
|
|
|
|
if [[ "${EDITOR}" =~ 'vim$' ]]; then
|
|
|
|
EDITOR_OPTIONS=(
|
|
|
|
'-c'
|
|
|
|
'set spell'
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
|
2021-08-13 12:55:12 -04:00
|
|
|
|
|
|
|
for ARG in "$@"; do
|
|
|
|
(
|
|
|
|
set -euo pipefail
|
|
|
|
mkdir -p "$NOTES_DIR"
|
|
|
|
cd "$NOTES_DIR"
|
|
|
|
|
|
|
|
case "${ARG}" in
|
2021-10-29 10:41:50 -04:00
|
|
|
'category')
|
|
|
|
shift
|
|
|
|
export NOTES_DIR="$NOTES_DIR/$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
notes "$@"
|
|
|
|
exit 255
|
|
|
|
;;
|
|
|
|
|
2021-09-27 09:50:24 -04:00
|
|
|
'fzf')
|
|
|
|
"${EDITOR}" "${EDITOR_OPTIONS[@]}" "$(notes ls | fzf)"
|
|
|
|
;;
|
|
|
|
|
2021-08-13 12:55:12 -04:00
|
|
|
'ls')
|
|
|
|
ag -g '' -l "${NOTES_DIR}" | sort -hr
|
|
|
|
;;
|
|
|
|
|
|
|
|
'status')
|
|
|
|
git status
|
|
|
|
;;
|
|
|
|
|
|
|
|
'commit')
|
|
|
|
git add .
|
2022-05-09 14:27:36 -04:00
|
|
|
git commit -m "notes commit $(strftime '%a %d %b %Y %r %Z')"
|
2021-08-13 12:55:12 -04:00
|
|
|
;;
|
|
|
|
|
|
|
|
'push')
|
|
|
|
git push
|
|
|
|
;;
|
|
|
|
|
|
|
|
'pull')
|
|
|
|
git pull
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
>&2 echo "invalid argument ${ARG}"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
2021-10-29 10:41:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
local EXIT_CODE=$?
|
|
|
|
if [ $EXIT_CODE != 0 ]; then
|
|
|
|
if [ $EXIT_CODE = 255 ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $EXIT_CODE
|
|
|
|
fi
|
2021-08-13 12:55:12 -04:00
|
|
|
done
|
|
|
|
|
|
|
|
# return if the above loop didn't return a bad code
|
|
|
|
if [ -n "$*" ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
(
|
|
|
|
set -euo pipefail
|
|
|
|
|
2022-05-09 14:27:36 -04:00
|
|
|
readonly NOW="$EPOCHSECONDS"
|
|
|
|
readonly FILE_PATH="${NOTES_DIR}/$(strftime '%Y/%m/%Y-%m-%d.md' "$NOW")"
|
2021-08-13 12:55:12 -04:00
|
|
|
|
|
|
|
mkdir -p "$(dirname "${FILE_PATH}")"
|
|
|
|
if ! [ -e "${FILE_PATH}" ]; then
|
2022-05-09 14:27:36 -04:00
|
|
|
echo -e "# $(strftime '%a %d %b %Y' "$NOW")\n\n## todo\n\n" > "${FILE_PATH}"
|
2021-08-13 12:55:12 -04:00
|
|
|
fi
|
|
|
|
|
2021-08-20 09:29:28 -04:00
|
|
|
"${EDITOR}" "${EDITOR_OPTIONS[@]}" "${FILE_PATH}"
|
2021-08-13 12:55:12 -04:00
|
|
|
)
|
|
|
|
}
|