histd/histd.sh

60 lines
1.2 KiB
Bash
Raw Normal View History

2023-02-16 23:27:28 +05:00
#!/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 -
}
function backup {
# Creates an archive with all notes
ARCHIVE_PATH=~/histd-$(date +%F).tar.xz
cd $BASE_DIR
tar cfJ $ARCHIVE_PATH ../histd
cd -
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 \;
}
# 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
else
echo Command not found
fi