dotfiles/home/private_bin/executable_notes
2024-02-20 04:46:26 -06:00

49 lines
1.3 KiB
Bash

#!/usr/bin/env sh
# Create a dated text file at a specific location and append text to it.
#
# Usage:
# $ notes something you want to jot down (appends that text to the file)
# $ xclip -o | notes (appends your clipboard to the file)
# $ notes (opens the file in your editor)
#
# Produces:
# YYYY-W.txt in your $NOTES_DIRECTORY (this is set below).
set -e
YEAR="$(date +"%Y")"
WEEK="$(date +"%W")"
NOTES_DIRECTORY="${NOTES_DIRECTORY:-${HOME}/stuff/notes/notes}"
NOTES_EDITOR="${EDITOR:-vim}"
NOTES_FILE="${YEAR}-${WEEK}.md"
NOTES_PATH="${NOTES_DIRECTORY}/${NOTES_FILE}"
readonly YEAR WEEK NOTES_FILE NOTES_PATH NOTES_EDITOR NOTES_DIRECTORY
if [ ! -d "${NOTES_DIRECTORY}" ]; then
while true; do
printf "%s does not exist, do you want to create it? (y/n) " "${NOTES_DIRECTORY}"
read -r yn
case "${yn}" in
[Yy]* ) mkdir -p "${NOTES_DIRECTORY}"; break;;
[Nn]* ) exit;;
* ) printf "Please answer y or n\n\n";;
esac
done
fi
if [ ! -f "${NOTES_PATH}" ]; then
printf '# %s Week %s\n' "${YEAR}" "${WEEK}" > "${NOTES_PATH}"
fi
if [ ${#} -eq 0 ]; then
if [ -p "/dev/stdin" ]; then
(cat; printf "\n") >> "${NOTES_PATH}"
else
eval "${NOTES_EDITOR}" "${NOTES_PATH}"
fi
else
printf "%s\n" "${*}" >> "${NOTES_PATH}"
fi