histd/histd.sh

81 lines
1.8 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
2023-02-16 23:37:32 +05:00
cd - > /dev/null
2023-02-16 23:27:28 +05:00
}
function backup {
# Creates an archive with all notes
ARCHIVE_PATH=~/histd-$(date +%F).tar.xz
cd $BASE_DIR
tar cfJ $ARCHIVE_PATH ../histd
2023-02-16 23:37:32 +05:00
cd - > /dev/null
2023-02-16 23:27:28 +05:00
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 \;
}
2023-02-16 23:45:47 +05:00
function list {
tree $BASE_DIR
}
2023-02-16 23:43:26 +05:00
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"
2023-02-16 23:45:47 +05:00
echo "- histd.sh list - list all notes"
2023-02-16 23:43:26 +05:00
echo "- histd.sh help - show this message"
}
2023-02-16 23:27:28 +05:00
# 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
2023-02-16 23:45:47 +05:00
elif [[ $1 == "list" ]]; then
list
2023-02-16 23:43:26 +05:00
elif [[ $1 == "help" ]]; then
help
2023-02-16 23:27:28 +05:00
else
echo Command not found
2023-02-16 23:43:26 +05:00
echo
help
2023-02-16 23:27:28 +05:00
fi