ned: everything seems to be working fine, I'm calling this ned v1.0

This commit is contained in:
PowerUser64 2022-01-30 21:13:07 -08:00
parent 6ecb219644
commit ec271021a8

View file

@ -1,8 +1,11 @@
#!/bin/bash #!/bin/bash
# v1.0
# NEd: Note Editor (and your best friend!) # NEd: Note Editor (and your best friend!)
# Ned can edit and create notes in a folder you tell him to! # Ned can edit and create notes in a folder you tell him to!
# Depends: fzf # Depends: fzf
# TODO: make it so the script assumes you are editing, and automatically creates a new document if the file to edit doesn't exist
# shellcheck disable=SC2068,SC2178,SC2128 # shellcheck disable=SC2068,SC2178,SC2128
# set default values if things are unset # set default values if things are unset
@ -17,6 +20,7 @@ usage() {
new <folder> [note name] create a new note in the matched class folder 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 edit [folder] search for and edit a text document in a folder
help print this message help print this message
ned edit ned (the thing you just ran)
Notes: Notes:
Right now, only the first letter of each option is considered, so \`ned n\` and \`ned neww\` do the same thing Right now, only the first letter of each option is considered, so \`ned n\` and \`ned neww\` do the same thing
@ -29,14 +33,16 @@ usage() {
edit() { edit() {
check_setup check_setup
# set search term if [ -n "$3" ]; then
if [ -z "$2" ]; then SEARCH_TERM="$3"
echo "Error: no search term provided" >&2 if [ -d "$NOTES_DIR/$2"* ]; then
exit 1 NOTES_DIR="$(echo "$NOTES_DIR"/"$2"*)"
fi
elif [ -n "$2" ]; then
SEARCH_TERM="$2"
fi fi
SEARCH_TERM="$2" NOTE_PATH="$NOTES_DIR/$(cd "$NOTES_DIR" && fzf -1 -q "$SEARCH_TERM" --layout=reverse --info=inline --height=10%)"
NOTE_PATH="$(cd "$NOTES_DIR" && fzf -1 -q "$SEARCH_TERM" --layout=reverse --info=inline --height=10%)"
$EDITOR_CMD "$NOTE_PATH" $EDITOR_CMD "$NOTE_PATH"
} }
@ -45,10 +51,12 @@ new() {
check_setup check_setup
# Find out note name # Find out note name
if [ -z "$3" ]; then if [ -n "$3" ]; then
NOTE_NAME="$(date +'%F').md"
else
NOTE_NAME="$3.md" NOTE_NAME="$3.md"
elif [ -n "$2" ]; then
NOTE_NAME="$2.md"
else
NOTE_NAME="$(date +'%F').md"
fi fi
NOTE_PATH="$(fd -ad 1 "^$2" "$NOTES_DIR")" NOTE_PATH="$(fd -ad 1 "^$2" "$NOTES_DIR")"
@ -68,7 +76,6 @@ new() {
# check if there is a notes directory in the note path # check if there is a notes directory in the note path
if [ -d "$NOTE_PATH/notes" ]; then if [ -d "$NOTE_PATH/notes" ]; then
NOTE_PATH="$NOTE_PATH/notes" NOTE_PATH="$NOTE_PATH/notes"
# if there is no notes directory # if there is no notes directory
else else
PROMPT="Place note in… " PROMPT="Place note in… "
@ -77,6 +84,7 @@ new() {
if [ -f "$NOTE_PATH/$NOTE_NAME" ]; then if [ -f "$NOTE_PATH/$NOTE_NAME" ]; then
echo "File already exists. Opening..." echo "File already exists. Opening..."
sleep 0.5
$EDITOR_CMD "$NOTE_PATH/$NOTE_NAME" $EDITOR_CMD "$NOTE_PATH/$NOTE_NAME"
else else
cd "$NOTE_PATH" || exit cd "$NOTE_PATH" || exit
@ -86,11 +94,11 @@ new() {
<!-- vim: wrap nonu nornu <!-- vim: wrap nonu nornu
--> -->
EOF EOF
STARTING_MD5="$(md5sum "$NOTE_PATH/$NOTE_NAME")"
$EDITOR_CMD "$NOTE_PATH/$NOTE_NAME" $EDITOR_CMD "$NOTE_PATH/$NOTE_NAME"
if [ "$(md5sum "$NOTE_PATH/$NOTE_NAME")" = 'c0269c768dbafd5d25dcf8144b3dd133' ]; then if [ "$(md5sum "$NOTE_PATH/$NOTE_NAME")" = "$STARTING_MD5" ]; then
echo 'file unmodified' rm "$NOTE_PATH/$NOTE_NAME"
fi fi
fi fi
} }
@ -118,11 +126,11 @@ case "${1}" in
# create and edit new note # create and edit new note
'new') new $@;; 'new') new $@;;
# edit an existing document or file # edit an existing document or file
'edit') edit $@;; 'edit'|'ed') edit $@;;
# run a shell (cd to) in the directory # run a shell (cd to) in the directory
'cd'|'shell') shell;; 'cd'|'shell') shell;;
# me: edit ned (the file you are looking at) # edit ned (the file you are looking at right now)
'me') $EDITOR "$(realpath "$0")";; 'ned') $EDITOR "$(realpath "$0")";;
*) echo "no such option";; *) echo "no such option";;
esac esac