histd/histd.sh

95 lines
2.1 KiB
Bash
Executable file

#!/bin/bash
# Histd: how I spent this day.
# A simple but useful personal diary CLI utility.
# Check if a text editor is specified
function _check_editor {
if [[ $EDITOR == "" ]]; then
echo EDITOR is undefined
exit 1
fi
}
# Create the required directories and open a text editor
# so that the user can describe the day.
function edit_note {
# Create dirs (base_dir/year/month)
WORKDIR="$BASE_DIR/$(date +%Y)/$(date +%m)"
mkdir -p $WORKDIR
# Generate file name
PATH_TO_FILE="$WORKDIR/$(date +%d).md"
# Open editor
_check_editor
cd $BASE_DIR
$EDITOR $PATH_TO_FILE
cd - > /dev/null
}
# Create an archive with all notes
function backup {
ARCHIVE_PATH=~/histd-$(date +%F).tar.xz
cd $BASE_DIR
tar cfJ $ARCHIVE_PATH ../histd
cd - > /dev/null
echo Saved to $ARCHIVE_PATH
}
# Concatenate all files and prefix each with the filename.
# The result will be printed to stdout.
function merge {
find $BASE_DIR -type f -name "*.md" -exec echo "# {}" \; -exec cat "{}" \; -exec echo \;
}
# Display tree of all notes
function list {
tree $BASE_DIR
}
# Open last note
function last {
_check_editor
LAST_FILE=$(find $BASE_DIR -type f -name "*.md" | tail -1)
cd $BASE_DIR
$EDITOR $LAST_FILE
cd - > /dev/null
}
function help {
echo "Histd - How I Spent This Day"
echo "A simple personal diary app"
echo
echo "Usage:"
echo "- histd.sh - open a text editor to describe the current day"
echo "- histd.sh backup - create an archive of all notes"
echo "- histd.sh merge - print all notes"
echo "- histd.sh list - list all notes"
echo "- histd.sh last - open last note"
echo "- histd.sh help - show this message"
}
# Prepare the environment
BASE_DIR=~/.local/share/histd
mkdir -p $BASE_DIR
# Parse commands
if [[ $# -eq 0 ]]; then
edit_note
elif [[ $1 == "backup" ]]; then
backup
elif [[ $1 == "merge" ]]; then
merge
elif [[ $1 == "list" ]]; then
list
elif [[ $1 == "last" ]]; then
last
elif [[ $1 == "help" ]]; then
help
else
echo Command not found
echo
help
fi