From d61794254cffc44887005cdaa1559ce3a0f34ab3 Mon Sep 17 00:00:00 2001 From: PowerUser64 Date: Fri, 21 Jan 2022 17:39:22 -0800 Subject: [PATCH] zsh: ned: big improvements. check the usage() function for details --- .config/shell/bin/ned | 94 +++++++++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 22 deletions(-) diff --git a/.config/shell/bin/ned b/.config/shell/bin/ned index ed2e947..3f75f22 100755 --- a/.config/shell/bin/ned +++ b/.config/shell/bin/ned @@ -1,35 +1,85 @@ #!/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 +# shellcheck disable=SC2068,SC2178,SC2128 -# set default value for $NOTES_DIR -${NOTES_DIR:="$HOME/Documents/college/2y/2q/"} +# set default values if things are unset +NOTES_DIR="${NOTES_DIR:-"$HOME/Documents/college/current"}" +EDITOR="${EDITOR:-"nvim"}" -[ -d "$NOTES_DIR"/misc ] && mkdir "$NOTES_DIR/misc" +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 -# Create misc note if no input -if [ -z "$1" ]; then - "$EDITOR" "$NOTES_DIR/unsorted/$(date)" - zsh -fi - -new() { - if [ -z "$1" ]; then - "$EDITOR" "$NOTES_DIR/unsorted/$(date)" - zsh - fi - - "$EDITOR" "$NOTES_DIR/$2"*"/notes/$3" + 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 } -# Parse arguments -case "$1" in +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 - 'new'|'n') new $@;; - # edit ned - 'e') "$EDITOR" "$(realpath "$0")";; + '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