scripts/notes.sh

148 lines
3 KiB
Bash
Executable file

#!/usr/bin/env bash
# Load config
. ${HOME}/ayo.conf
. ${scripts_dir}/functions.sh
getopts "t" typora; #check if -t flag is given
# put new notes to Inbox directory
TARGET="${notes_dir}/Inbox"
function createNote() {
{
if [ "$1" = "" ]; then
read -p "Create new note: " title
else
title="${@:1}"
fi
file_date="$(date +'%m.%d.%Y')"
if [ "$title" = "" ]; then
title="$file_date"
file_name="${file_date}.md"
else
file_name="${file_date} - ${title}.md"
fi;
full_path="${TARGET}/${file_name}"
# IF Not Exists: create file with:
# - title
# - date & time
if ! test -f "$full_path"; then
install -Dv /dev/null "$full_path" >/dev/null
heading="# $title"
echo "$heading" > "$full_path"
date_heading=$(date +'%b %d, %Y, %a %r')
echo "$date_heading" >> "$full_path"
fi
# Edit the new / existing file
editFile "$full_path"
} || {
echo ">>> New note failed"
exit 1
}
}
function main() {
case "$2" in
# DIFF: Show git diff of staged changes
diff|d)
cd "$notes_dir"
git add .
git diff --staged .
;;
# DIR: open directory
dir)
vim -c "lcd $notes_dir"
;;
# SYNC: Sync notes directory with remote
sync|s)
notesSync
;;
# LIST: List all notes in directory
list|l)
echo "ACTIVE NOTES ($notes_dir): "
notesSync
if ! [ "$2" = "" ]; then
files=( "$notes_dir/$2"/*.md )
else
files=( "$notes_dir"/*.md )
fi
index=0
for file in "${files[@]##*/}"; do
((index++))
echo "$index) $file"
done
;;
# OPEN: Open a note from list or by index
open|o)
notesSync
files=( "$notes_dir"/*.md )
if ! [ "$2" = "" ]; then
index=$((2-1))
open_file=${files[$index]}
editFile "$open_file"
else
PS3="Open file #: "
echo "Please select a file to OPEN."
select file in "${files[@]##*/}"; do
{
echo "Opening $file"
editFile "$file"
break
} ||
{
echo "bad choice"
break
}
done
fi
;;
# REMOVE: Remove a note from list or by index
remove|rm)
notesSync
files=( "$notes_dir"/*.md )
if ! [ "$2" = "" ]; then
index=$((2-1))
remove_file=${files[$index]}
echo "Removing $remove_file"
rm "$remove_file"
notesSync
else
PS3="Remove file #: "
echo "Please select a file to REMOVE."
select file in "${files[@]##*/}"; do
{
echo "Removing $file"
rm "${notes_dir}/${file}"
notesSync
break
} ||
{
echo "bad choice"
break
}
done
fi
;;
# DEFAULT: Default action - create new note
*)
createNote $2
;;
esac
}
main "$@"