From ec271021a88e31756350af02f59ee4a282bac520 Mon Sep 17 00:00:00 2001 From: PowerUser64 Date: Sun, 30 Jan 2022 21:13:07 -0800 Subject: [PATCH] ned: everything seems to be working fine, I'm calling this ned v1.0 --- .config/shell/bin/ned | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/.config/shell/bin/ned b/.config/shell/bin/ned index 422240c..650bde2 100755 --- a/.config/shell/bin/ned +++ b/.config/shell/bin/ned @@ -1,8 +1,11 @@ #!/bin/bash +# v1.0 # NEd: Note Editor (and your best friend!) # Ned can edit and create notes in a folder you tell him to! # 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 # set default values if things are unset @@ -17,6 +20,7 @@ usage() { 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 + ned edit ned (the thing you just ran) Notes: 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() { check_setup - # set search term - if [ -z "$2" ]; then - echo "Error: no search term provided" >&2 - exit 1 + if [ -n "$3" ]; then + SEARCH_TERM="$3" + if [ -d "$NOTES_DIR/$2"* ]; then + NOTES_DIR="$(echo "$NOTES_DIR"/"$2"*)" + fi + elif [ -n "$2" ]; then + SEARCH_TERM="$2" fi - SEARCH_TERM="$2" - NOTE_PATH="$(cd "$NOTES_DIR" && fzf -1 -q "$SEARCH_TERM" --layout=reverse --info=inline --height=10%)" + NOTE_PATH="$NOTES_DIR/$(cd "$NOTES_DIR" && fzf -1 -q "$SEARCH_TERM" --layout=reverse --info=inline --height=10%)" $EDITOR_CMD "$NOTE_PATH" } @@ -45,10 +51,12 @@ new() { check_setup # Find out note name - if [ -z "$3" ]; then - NOTE_NAME="$(date +'%F').md" - else + if [ -n "$3" ]; then NOTE_NAME="$3.md" + elif [ -n "$2" ]; then + NOTE_NAME="$2.md" + else + NOTE_NAME="$(date +'%F').md" fi NOTE_PATH="$(fd -ad 1 "^$2" "$NOTES_DIR")" @@ -68,7 +76,6 @@ new() { # 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… " @@ -77,6 +84,7 @@ new() { if [ -f "$NOTE_PATH/$NOTE_NAME" ]; then echo "File already exists. Opening..." + sleep 0.5 $EDITOR_CMD "$NOTE_PATH/$NOTE_NAME" else cd "$NOTE_PATH" || exit @@ -86,11 +94,11 @@ new() { EOF + STARTING_MD5="$(md5sum "$NOTE_PATH/$NOTE_NAME")" $EDITOR_CMD "$NOTE_PATH/$NOTE_NAME" - if [ "$(md5sum "$NOTE_PATH/$NOTE_NAME")" = 'c0269c768dbafd5d25dcf8144b3dd133' ]; then - echo 'file unmodified' + if [ "$(md5sum "$NOTE_PATH/$NOTE_NAME")" = "$STARTING_MD5" ]; then + rm "$NOTE_PATH/$NOTE_NAME" fi - fi } @@ -118,11 +126,11 @@ case "${1}" in # create and edit new note 'new') new $@;; # edit an existing document or file - 'edit') edit $@;; + 'edit'|'ed') edit $@;; # run a shell (cd to) in the directory 'cd'|'shell') shell;; - # me: edit ned (the file you are looking at) - 'me') $EDITOR "$(realpath "$0")";; + # edit ned (the file you are looking at right now) + 'ned') $EDITOR "$(realpath "$0")";; *) echo "no such option";; esac