Skip to content

Instantly share code, notes, and snippets.

@AnyTimeTraveler
Last active January 12, 2019 20:43
Show Gist options
  • Save AnyTimeTraveler/7229f1c65120266b291c9cb0cb3000d6 to your computer and use it in GitHub Desktop.
Save AnyTimeTraveler/7229f1c65120266b291c9cb0cb3000d6 to your computer and use it in GitHub Desktop.
A small script that manages your diary. Put it into a folder, initialize a git repository there and push it to a remote. The rest will be done by the script.
#!/bin/sh
# Get your own path
path=$(dirname $(realpath $0))
# Make sure that git uses the path of the script and not the current directory
git="git --work-tree=$path --git-dir=$path/.git/"
# Get today's date
day="$(date +'%d')"
month="$(date +'%m')"
year="$(date +'%Y')"
# Parse arguments and replace the dates accordingly
if [ "$#" -eq 1 ]; then
if [ "$1" = "y" ]; then
day="$(date -d "yesterday 13:00" +'%d')"
month="$(date -d "yesterday 13:00" +'%m')"
year="$(date -d "yesterday 13:00" +'%Y')"
elif [ "$1" = "--help" ]; then
cat << EOF
usage: $0
(to write today's entry)
or $0 y
(to write yesterday's entry)
or $0 [day]
(to work with the entry of entry a specific day)
or $0 [day] [month]
(to work with the entry of entry a specific day and month)
or $0 [day] [month] [year]
(to work with the entry of the given date)
Missing arguments will be replaced by the current date.
For example:
Executing '$0 01' would open day 01 of the current month and year.
Uses your \$EDITOR variable to edit entries.
It should be set in your .bashrc, or .zshrc, ...
Report bugs to: [email protected]
EOF
exit 0
else
day=$1
fi
elif [ "$#" -eq 2 ]; then
day=$1
month=$2
elif [ "$#" -eq 3 ]; then
day=$1
month=$2
year=$3
elif [ "$#" -gt 3 ]; then
echo "Invalid amount of parameters!"
exit -1
fi
# Make sure the directory exists
mkdir -p $path/$year/$month/
fullpath="$path/$year/$month/$day"
# Pull to make sure no foreign edits were missed
echo "Updating remote changes..."
git --work-tree="$path" --git-dir="$path/.git/" pull
echo
# Edit the actual file
$EDITOR $fullpath
# Check if file exists before doing other checks
if [ ! -f $fullpath ]; then
exit
fi
# Determine if the file is new and set commit message
$git add $fullpath
line="$($git status --porcelain | grep $year/$month/$day)"
$git reset --quiet -- $fullpath
if [[ "${line:0:1}" == "A" ]]; then
message="Add"
elif [[ "${line:0:1}" == "M" ]]; then
message="Edit"
else
echo "No changes."
exit
fi
message="$message entry for $day.$month.$year"
# Ask the user if the changes should be pushed
if whiptail --yesno " Done?" 8 20;then
# Do a spellcheck
aspell --personal="$path/.aspell.en.pws" --repl="$path/.aspell.en.prepl" --dont-backup check $fullpath
# Do the git stuff
$git add $fullpath
$git add $path/.aspell.*
$git commit -m"$message"
echo "Uploading..."
$git push
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment