#!/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="${EDITOR:-"nvim"}" usage() { cat <<-EOF Usage: ned new [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 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" } new() { check_setup if [ -z "$3" ]; then NOTE_NAME="$(date +'%F').md" else NOTE_NAME="$3.md" 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 } # 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 $@;; # me: edit ned (the file you are looking at) 'm') "$EDITOR" "$(realpath "$0")";; *) echo "no such option";; esac # vim: et sw=3 ts=3