histd/histd.sh

96 lines
2.1 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.
# Check if a text editor is specified
function _check_editor {
if [[ $EDITOR == "" ]]; then
echo EDITOR is undefined
exit 1
fi
}
2023-03-13 00:30:57 +05:00
# Create the required directories and open a text editor
# so that the user can describe the day.
2023-02-16 23:27:28 +05:00
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
2023-02-16 23:27:28 +05:00
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
}
2023-03-13 00:30:57 +05:00
# Create an archive with all notes
2023-02-16 23:27:28 +05:00
function backup {
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
}
2023-03-13 00:30:57 +05:00
# Concatenate all files and prefix each with the filename.
# The result will be printed to stdout.
2023-02-16 23:27:28 +05:00
function merge {
find $BASE_DIR -type f -name "*.md" -exec echo "# {}" \; -exec cat "{}" \; -exec echo \;
}
2023-03-13 00:30:57 +05:00
# Display tree of all notes
2023-02-16 23:45:47 +05:00
function list {
tree $BASE_DIR
}
2023-03-13 00:30:57 +05:00
# Open last note
2023-03-13 00:16:14 +05:00
function last {
_check_editor
LAST_FILE=$(find $BASE_DIR -type f -name "*.md" | tail -1)
2023-03-13 00:16:14 +05:00
cd $BASE_DIR
$EDITOR $LAST_FILE
cd - > /dev/null
}
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-03-13 00:16:14 +05:00
echo "- histd.sh last - open last note"
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-03-13 00:16:14 +05:00
elif [[ $1 == "last" ]]; then
last
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