Skip to content

Instantly share code, notes, and snippets.

@Klowner
Created February 29, 2024 19:47
Show Gist options
  • Save Klowner/51a6a9fac6454843db3373401fb6f56a to your computer and use it in GitHub Desktop.
Save Klowner/51a6a9fac6454843db3373401fb6f56a to your computer and use it in GitHub Desktop.
Simple command line daily journaling script
#!/usr/bin/env sh
# Journal by Mark Riedesel
set -e
SCRIPT_DIR="$( cd "$(dirname $0)" &> /dev/null && pwd )"
EDITOR=${EDITOR:-nvim}
ENTRY_SUBDIR="entries"
ENTRY_DIR="${SCRIPT_DIR}/${ENTRY_SUBDIR}"
function build_date_string() {
date_string=$(printf "%s " "$@")
date +%Y-%m-%d -d "${date_string}"
}
function get_entry_path() {
date_string=$(build_date_string $@)
echo "${ENTRY_DIR}/${date_string}.md"
}
function entry_edit() {
entry_path="$(get_entry_path $@)"
# Open entry in specified editor
$EDITOR "$entry_path"
ask_commit_if_modified
}
function ask_commit_if_modified() {
update_modified_entries
if [ ${#MODIFIED_ENTRIES[@]} -gt 0 ]; then
read -r -p "${#MODIFIED_ENTRIES[@]} modified entries, commit? [Y/n]" response
case "${response}" in
[nN][oO]|[nN])
;;
*)
(cd $SCRIPT_DIR; git add $ENTRY_SUBDIR; git commit -m "$(make_commit_message)")
;;
esac
fi
}
function update_modified_entries() {
IFS=$'\n' read -r -d '' -a MODIFIED_ENTRIES < \
<(cd $SCRIPT_DIR; git ls-files --modified --other -- $ENTRY_SUBDIR && printf '\0')
}
function make_commit_message() {
commit_message='update entries:'
modified_dates=()
for entry in "${MODIFIED_ENTRIES[@]}"; do
entry="${entry%.*}"
entry="${entry##*/}"
modified_dates+=($entry)
done
printf -v modified_dates_joined '%s, ' "${modified_dates[@]}"
commit_message="${commit_message} ${modified_dates_joined%, }"
echo "${commit_message}"
}
function run_git() {
(cd $SCRIPT_DIR; git ${@})
}
function git_sync() {
ask_commit_if_modified
(cd $SCRIPT_DIR; git pull --rebase && git push)
}
function entry_print() {
entry_path="$(get_entry_path "${@}")"
if ! command -v lowdown &> /dev/null; then
cat $entry_path
else
lowdown -tterm $entry_path
fi
}
function entry_status() {
update_modified_entries
if [ ${#MODIFIED_ENTRIES[@]} -gt 0 ]; then
for entry in "${MODIFIED_ENTRIES[@]}"; do
echo "$entry"
done
fi
}
if [ ! -d "${ENTRY_DIR}" ]; then
mkdir -p "${ENTRY_DIR}"
fi
case "${1:-help}" in
"edit" | "e") entry_edit "${@:2}";;
"git") run_git "${@:2}";;
"path") get_entry_path "${@:2}";;
"sync") git_sync;;
"status" | "s") entry_status;;
"print" | "echo") entry_print "${@:2}";;
"help")
echo "journal commands:"
echo " echo [date='today'] echo entry to terminal (uses lowdown for formatting if available)"
echo " edit [date='today'] open specified day for editing (\$EDITOR=${EDITOR})"
echo " path [date='today'] echo the absolute path to the requested day's journal file"
echo " sync push/pull remote git repositories"
echo " status list modified journal entries"
echo " git <commands> run git in $SCRIPT_DIR"
;;
*)
echo "unknown command: $1" >&2
exit 1
;;
esac
# vim: set ts=2:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment