dotfiles/.config/shell/bin/ned

86 lines
2 KiB
Text
Raw Normal View History

2022-01-13 14:47:59 -08:00
#!/bin/bash
# NEd: Note Editor (and your best friend!)
# Ned can edit and create notes in a folder you tell him to!
# Depends: fzf
2022-01-13 14:47:59 -08:00
# shellcheck disable=SC2068,SC2178,SC2128
2022-01-20 03:18:04 -08:00
# set default values if things are unset
NOTES_DIR="${NOTES_DIR:-"$HOME/Documents/college/current"}"
EDITOR="${EDITOR:-"nvim"}"
2022-01-13 14:47:59 -08:00
usage() {
cat <<-EOF
Usage:
ned <verb>
new <folder> [note name] create a new note in the matched class folder
edit [folder] search for and edit a text document in a folder
help print this message
2022-01-13 14:47:59 -08:00
Notes:
Right now, only the first letter of each option is considered, so \`ned n\` and \`ned neww\` do the same thing
Folder and file names are globbed
Current repository path: $NOTES_DIR
EOF
}
edit() {
check_setup
NOTE_PATH=("$NOTES_DIR/$2"*)
NOTE_PATH="${NOTE_PATH[0]}"
if ! [ -d "$NOTE_PATH" ]; then
echo "No such directory: '$NOTE_PATH'"
exit 1
fi
NOTE_NAME="$(cd "$NOTE_PATH" && fzf --height=10 --layout=reverse)" || exit
"$EDITOR" "$NOTES_DIR/$NOTE_NAME"
}
2022-01-13 14:47:59 -08:00
2022-01-20 03:18:04 -08:00
new() {
check_setup
if [ -z "$3" ]; then
NOTE_NAME="$(date +'%F').md"
else
NOTE_NAME="$3.md"
2022-01-20 03:18:04 -08:00
fi
NOTE_PATH=("$NOTES_DIR/$2"*/notes)
NOTE_PATH="${NOTE_PATH[0]}"
if ! [ -d "$NOTE_PATH" ]; then
echo "No such directory: '$NOTE_PATH'"
exit 1
fi
"$EDITOR" "$NOTE_PATH/$NOTE_NAME"
}
subshell() {
echo 'entering subshell'
zsh
}
check_setup() {
# perform preliminary checks to make sure the script can run properly
if ! [ -d "$NOTES_DIR" ]; then
echo "Error: Notes directory ($NOTES_DIR) does not exist or is not a directory"
exit 1
fi
2022-01-20 03:18:04 -08:00
}
# Parse arguments (first character only)
case "${1:0:1}" in
# help
'h') usage;;
2022-01-13 14:47:59 -08:00
# create and edit new note
'n') new $@;;
# edit an existing document or file
'e') edit $@;;
# me: edit ned (the file you are looking at)
'm') "$EDITOR" "$(realpath "$0")";;
2022-01-13 14:47:59 -08:00
*) echo "no such option";;
esac
# vim: et sw=3 ts=3