2022-04-08 02:32:05 -07:00
|
|
|
#!/bin/sh
|
2022-01-30 21:13:07 -08:00
|
|
|
# v1.0
|
2022-01-13 14:47:59 -08:00
|
|
|
# NEd: Note Editor (and your best friend!)
|
|
|
|
# Ned can edit and create notes in a folder you tell him to!
|
2022-01-21 17:39:22 -08:00
|
|
|
# Depends: fzf
|
2022-01-13 14:47:59 -08:00
|
|
|
|
2022-01-30 21:13:07 -08:00
|
|
|
# TODO: make it so the script assumes you are editing, and automatically creates a new document if the file to edit doesn't exist
|
|
|
|
|
2022-01-21 17:39:22 -08:00
|
|
|
# shellcheck disable=SC2068,SC2178,SC2128
|
2022-01-20 03:18:04 -08:00
|
|
|
|
2022-01-21 17:39:22 -08:00
|
|
|
# set default values if things are unset
|
2022-04-08 02:32:05 -07:00
|
|
|
BASE_DIR="${NOTES_DIR:-"$HOME/Documents/college/current"}"
|
2022-01-23 03:20:41 -08:00
|
|
|
EDITOR="nvim"
|
|
|
|
EDITOR_CMD="nvim -c :ZenMode"
|
2022-04-08 14:53:09 -07:00
|
|
|
DEFAULT_NEW_NOTE_EXT=.md
|
|
|
|
DEFAULT_NEW_NOTE_NAME="$(date +'%m-%d')$DEFAULT_NEW_NOTE_EXT"
|
2022-04-08 02:32:05 -07:00
|
|
|
|
|
|
|
# Some fzf commands
|
|
|
|
FZF="fzf -1 --layout=reverse --info=inline --height=10%"
|
|
|
|
FZF_SEARCH="$FZF -q "
|
2022-01-13 14:47:59 -08:00
|
|
|
|
2022-01-21 17:39:22 -08:00
|
|
|
usage() {
|
|
|
|
cat <<-EOF
|
|
|
|
Usage:
|
|
|
|
ned <verb>
|
2022-02-06 22:13:57 -08:00
|
|
|
new <folder> [note name] Create a new note in the matched folder
|
2022-03-05 22:45:21 -08:00
|
|
|
[edit|ed] [folder] <note name> Search for and edit a text document in a folder
|
2022-02-06 22:13:57 -08:00
|
|
|
ned Edit ned
|
|
|
|
help Print this message
|
2022-04-08 14:53:09 -07:00
|
|
|
cd [folder] Open a shell in a directory or the default directory
|
2022-01-13 14:47:59 -08:00
|
|
|
|
2022-01-21 17:39:22 -08:00
|
|
|
Notes:
|
2022-04-08 14:53:09 -07:00
|
|
|
Edit 'T' to get today's note
|
2022-02-06 22:13:57 -08:00
|
|
|
Folder and file names are searched with fuzzy search
|
2022-04-08 02:32:05 -07:00
|
|
|
ned will automatically activate the ZenMode neovim extension
|
|
|
|
Current repository path: '$BASE_DIR'
|
2022-01-21 17:39:22 -08:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2022-03-05 22:45:21 -08:00
|
|
|
open_file() {
|
2022-04-08 02:32:05 -07:00
|
|
|
cd "$(dirname "$1")" || return
|
2022-03-05 22:45:21 -08:00
|
|
|
$EDITOR_CMD "$1"
|
|
|
|
}
|
|
|
|
|
2022-04-08 02:32:05 -07:00
|
|
|
# Usage:
|
|
|
|
# edit "unused param" ["search_directory"] "search_term"
|
2022-01-21 17:39:22 -08:00
|
|
|
edit() {
|
|
|
|
check_setup
|
|
|
|
|
2022-04-08 02:32:05 -07:00
|
|
|
# This logic is to allow the $2 to be optional
|
2022-01-30 21:13:07 -08:00
|
|
|
if [ -n "$3" ]; then
|
|
|
|
SEARCH_TERM="$3"
|
2022-04-08 02:32:05 -07:00
|
|
|
NOTE_DIR="$(cd "$BASE_DIR" && fd -t f | sed 's-\./--g' | $FZF_SEARCH "$2")" || exit
|
2022-01-30 21:13:07 -08:00
|
|
|
elif [ -n "$2" ]; then
|
|
|
|
SEARCH_TERM="$2"
|
2022-01-21 17:39:22 -08:00
|
|
|
fi
|
|
|
|
|
2022-04-08 02:32:05 -07:00
|
|
|
# Searching for 'T' will get you today's note
|
2022-03-05 22:45:21 -08:00
|
|
|
if [ "$SEARCH_TERM" = 'T' ]; then
|
2022-04-08 02:32:05 -07:00
|
|
|
SEARCH_TERM="$DEFAULT_NEW_NOTE_NAME"
|
2022-03-05 22:45:21 -08:00
|
|
|
fi
|
|
|
|
|
2022-04-08 02:32:05 -07:00
|
|
|
NOTE="$NOTE_DIR/$(cd "$NOTE_DIR" && $FZF_SEARCH "$SEARCH_TERM")" || exit
|
2022-01-29 22:53:29 -08:00
|
|
|
|
2022-04-08 02:32:05 -07:00
|
|
|
open_file "$NOTE"
|
2022-01-21 17:39:22 -08:00
|
|
|
}
|
2022-01-13 14:47:59 -08:00
|
|
|
|
2022-04-08 02:32:05 -07:00
|
|
|
# Usage: new "unused param" "save directory" ["note name"]
|
2022-01-20 03:18:04 -08:00
|
|
|
new() {
|
2022-01-21 17:39:22 -08:00
|
|
|
check_setup
|
2022-01-27 03:48:06 -08:00
|
|
|
|
2022-04-08 02:32:05 -07:00
|
|
|
# Note name
|
2022-01-30 21:13:07 -08:00
|
|
|
if [ -n "$3" ]; then
|
2022-04-08 14:53:09 -07:00
|
|
|
NEW_NOTE_NAME="$3"
|
2022-01-30 21:13:07 -08:00
|
|
|
else
|
2022-04-08 02:32:05 -07:00
|
|
|
NEW_NOTE_NAME="$DEFAULT_NEW_NOTE_NAME"
|
2022-01-20 03:18:04 -08:00
|
|
|
fi
|
|
|
|
|
2022-04-08 02:32:05 -07:00
|
|
|
# Note dir
|
2022-04-13 01:36:06 -07:00
|
|
|
NEW_NOTE_DIR="$BASE_DIR/$(cd "$BASE_DIR" && fd -d 1 | sed 's-\./--g' | $FZF_SEARCH "$2")/notes" || exit
|
|
|
|
|
2022-04-14 14:09:25 -07:00
|
|
|
# make the directory if it doesn't exist
|
|
|
|
[ -d "${NEW_NOTE_DIR}" ] || mkdir "${NEW_NOTE_DIR}"
|
2022-01-21 17:39:22 -08:00
|
|
|
|
2022-04-08 02:32:05 -07:00
|
|
|
if [ -f "$NEW_NOTE_DIR/$NEW_NOTE_NAME" ]; then
|
|
|
|
open_file "$NEW_NOTE_DIR/$NEW_NOTE_NAME"
|
2022-01-27 03:48:06 -08:00
|
|
|
else
|
2022-04-08 02:32:05 -07:00
|
|
|
cd "$NEW_NOTE_DIR" || exit
|
2022-01-27 03:48:06 -08:00
|
|
|
# apply a template for notes
|
2022-04-08 02:32:05 -07:00
|
|
|
cat <<-EOF >> "$NEW_NOTE_DIR/$NEW_NOTE_NAME"
|
2022-04-13 01:36:06 -07:00
|
|
|
# ${NEW_NOTE_NAME%%"$DEFAULT_NEW_NOTE_EXT"}
|
2022-01-27 03:48:06 -08:00
|
|
|
|
|
|
|
<!-- vim: wrap nonu nornu
|
|
|
|
-->
|
|
|
|
EOF
|
2022-04-08 02:32:05 -07:00
|
|
|
STARTING_MD5="$(md5sum "$NEW_NOTE_DIR/$NEW_NOTE_NAME")"
|
|
|
|
open_file "$NEW_NOTE_DIR/$NEW_NOTE_NAME"
|
|
|
|
# Delete the file if no changes were made
|
|
|
|
if [ "$(md5sum "$NEW_NOTE_DIR/$NEW_NOTE_NAME")" = "$STARTING_MD5" ]; then
|
|
|
|
rm "$NEW_NOTE_DIR/$NEW_NOTE_NAME"
|
2022-01-30 17:14:55 -08:00
|
|
|
fi
|
2022-01-27 03:48:06 -08:00
|
|
|
fi
|
2022-01-21 17:39:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
subshell() {
|
2022-03-02 05:21:45 -08:00
|
|
|
echo
|
2022-01-21 17:39:22 -08:00
|
|
|
echo 'entering subshell'
|
2022-01-27 03:48:06 -08:00
|
|
|
$SHELL
|
|
|
|
}
|
|
|
|
|
|
|
|
shell() {
|
2022-04-08 14:53:09 -07:00
|
|
|
if [ -n "${2+foo}" ]; then
|
|
|
|
DIR="$BASE_DIR/$(cd "$BASE_DIR" && fd -d 1 -t d | sed 's-\./--g' | $FZF_SEARCH "$2")" || exit
|
|
|
|
fi
|
|
|
|
if cd "$DIR"; then
|
|
|
|
subshell
|
|
|
|
else
|
|
|
|
exit
|
2022-04-08 02:32:05 -07:00
|
|
|
fi
|
2022-01-21 17:39:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
check_setup() {
|
|
|
|
# perform preliminary checks to make sure the script can run properly
|
2022-04-08 02:32:05 -07:00
|
|
|
if ! [ -d "$BASE_DIR" ]; then
|
|
|
|
echo "Error: Notes directory ($BASE_DIR) does not exist or is not a directory"
|
2022-01-21 17:39:22 -08:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-01-20 03:18:04 -08:00
|
|
|
}
|
|
|
|
|
2022-01-21 17:39:22 -08:00
|
|
|
# Parse arguments (first character only)
|
2022-01-30 17:19:00 -08:00
|
|
|
case "${1}" in
|
2022-01-21 17:39:22 -08:00
|
|
|
# help
|
2022-02-06 22:13:57 -08:00
|
|
|
'--help'|'-h'|'help'|'h') usage;;
|
2022-01-13 14:47:59 -08:00
|
|
|
# create and edit new note
|
2022-01-30 17:19:00 -08:00
|
|
|
'new') new $@;;
|
2022-01-21 17:39:22 -08:00
|
|
|
# edit an existing document or file
|
2022-01-30 21:13:07 -08:00
|
|
|
'edit'|'ed') edit $@;;
|
2022-01-27 03:48:06 -08:00
|
|
|
# run a shell (cd to) in the directory
|
2022-04-08 02:32:05 -07:00
|
|
|
'cd') shell $@;;
|
2022-01-30 21:13:07 -08:00
|
|
|
# edit ned (the file you are looking at right now)
|
|
|
|
'ned') $EDITOR "$(realpath "$0")";;
|
2022-01-13 14:47:59 -08:00
|
|
|
*) echo "no such option";;
|
|
|
|
esac
|
|
|
|
|
2022-04-08 02:32:05 -07:00
|
|
|
# vim: et sw=3 ts=3 ft=sh
|