diff --git a/home/private_bin/executable_notes b/home/private_bin/executable_notes new file mode 100644 index 0000000..3b02bfd --- /dev/null +++ b/home/private_bin/executable_notes @@ -0,0 +1,48 @@ +#!/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