histd/histd.sh

80 lines
1.8 KiB
Bash
Executable file

#!/bin/bash
# Histd: how I spent this day.
# A simple but useful personal diary CLI utility.
function edit_note {
# Creates the required directories and opens a text editor
# so that the user can describe the day.
# 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"
# Check editor
if [[ $EDITOR == "" ]]; then
echo EDITOR is undefined
return
fi
# Open editor
cd $BASE_DIR
$EDITOR $PATH_TO_FILE
cd - > /dev/null
}
function backup {
# Creates an archive with all notes
ARCHIVE_PATH=~/histd-$(date +%F).tar.xz
cd $BASE_DIR
tar cfJ $ARCHIVE_PATH ../histd
cd - > /dev/null
echo Saved to $ARCHIVE_PATH
}
function merge {
# This function concatenates all files and prefixes each with the filename.
# The result will be printed to stdout.
find $BASE_DIR -type f -name "*.md" -exec echo "# {}" \; -exec cat "{}" \; -exec echo \;
}
function list {
tree $BASE_DIR
}
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 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 == "help" ]]; then
help
else
echo Command not found
echo
help
fi