125 lines
3.1 KiB
Bash
Executable file
125 lines
3.1 KiB
Bash
Executable file
#!/bin/bash
|
|
# NEd: Note Editor (and your best friend!)
|
|
# Ned can edit and create notes in a folder you tell him to!
|
|
# Depends: fzf
|
|
|
|
# shellcheck disable=SC2068,SC2178,SC2128
|
|
|
|
# set default values if things are unset
|
|
NOTES_DIR="${NOTES_DIR:-"$HOME/Documents/college/current"}"
|
|
EDITOR="nvim"
|
|
EDITOR_CMD="nvim -c :ZenMode"
|
|
|
|
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
|
|
|
|
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
|
|
Will automatically activate the ZenMode nvim extension
|
|
Current repository path: $NOTES_DIR
|
|
EOF
|
|
}
|
|
|
|
edit() {
|
|
check_setup
|
|
|
|
# set search term
|
|
if [ -z "$2" ]; then
|
|
echo "Error: no search term provided" >&2
|
|
exit 1
|
|
fi
|
|
|
|
SEARCH_TERM="$2"
|
|
NOTE_PATH="$(cd "$NOTES_DIR" && fzf -1 -q "$SEARCH_TERM" --layout=reverse --info=inline --height=10%)"
|
|
|
|
$EDITOR_CMD "$NOTE_PATH"
|
|
}
|
|
|
|
new() {
|
|
check_setup
|
|
|
|
# Find out note name
|
|
if [ -z "$3" ]; then
|
|
NOTE_NAME="$(date +'%F').md"
|
|
else
|
|
NOTE_NAME="$3.md"
|
|
fi
|
|
|
|
NOTE_PATH="$(fd -ad 1 "^$2" "$NOTES_DIR")"
|
|
# check if there were no results
|
|
if [ -z "$NOTE_PATH" ]; then
|
|
echo "No directories found for '$2'."
|
|
echo "All possible directories:"
|
|
(cd "$NOTES_DIR" && fd -d 1 -t d "$NOTES_DIR")
|
|
exit
|
|
|
|
# if there was more than one result
|
|
elif [ "$(echo "$NOTE_PATH" | wc -l)" -gt 1 ]; then
|
|
NOTE_PATH="$(echo "$NOTE_PATH" | fzy)" || exit
|
|
fi
|
|
|
|
# TODO: allow setting location for files to go with $3, and $4 for file name
|
|
# check if there is a notes directory in the note path
|
|
if [ -d "$NOTE_PATH/notes" ]; then
|
|
NOTE_PATH="$NOTE_PATH/notes"
|
|
|
|
# if there is no notes directory
|
|
else
|
|
PROMPT="Place note in… "
|
|
NOTE_PATH="$(cd "$NOTE_PATH" && fd -ad 1 -t d | fzy -p "$PROMPT")" || exit
|
|
fi
|
|
|
|
if [ -f "$NOTE_PATH/$NOTE_NAME" ]; then
|
|
echo "File already exists. Opening..."
|
|
$EDITOR_CMD "$NOTE_PATH/$NOTE_NAME"
|
|
else
|
|
cd "$NOTE_PATH" || exit
|
|
# apply a template for notes
|
|
cat <<-EOF >> "$NOTE_PATH/$NOTE_NAME"
|
|
|
|
<!-- vim: wrap nonu nornu
|
|
-->
|
|
EOF
|
|
$EDITOR_CMD "$NOTE_PATH/$NOTE_NAME"
|
|
fi
|
|
}
|
|
|
|
subshell() {
|
|
echo 'entering subshell'
|
|
$SHELL
|
|
}
|
|
|
|
shell() {
|
|
cd "$NOTES_DIR" && subshell
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
# Parse arguments (first character only)
|
|
case "${1:0:1}" in
|
|
# help
|
|
'h') usage;;
|
|
# create and edit new note
|
|
'n') new $@;;
|
|
# edit an existing document or file
|
|
'e') edit $@;;
|
|
# run a shell (cd to) in the directory
|
|
'c'|'x'|'s') shell;;
|
|
# me: edit ned (the file you are looking at)
|
|
'm') $EDITOR "$(realpath "$0")";;
|
|
*) echo "no such option";;
|
|
esac
|
|
|
|
# vim: et sw=3 ts=3 ft=bash
|