ned: everything seems to be working fine, I'm calling this ned v1.0
This commit is contained in:
parent
6ecb219644
commit
ec271021a8
1 changed files with 24 additions and 16 deletions
|
@ -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 <folder> [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() {
|
|||
<!-- vim: wrap nonu nornu
|
||||
-->
|
||||
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
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue