ned: fzf is used everywhere now

This commit is contained in:
PowerUser64 2022-04-08 02:32:05 -07:00
parent 1eae827160
commit 4241c6ef05

View file

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/sh
# v1.0 # 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!
@ -9,10 +9,14 @@
# shellcheck disable=SC2068,SC2178,SC2128 # shellcheck disable=SC2068,SC2178,SC2128
# set default values if things are unset # set default values if things are unset
NOTES_DIR="${NOTES_DIR:-"$HOME/Documents/college/current"}" BASE_DIR="${NOTES_DIR:-"$HOME/Documents/college/current"}"
EDITOR="nvim" EDITOR="nvim"
EDITOR_CMD="nvim -c :ZenMode" EDITOR_CMD="nvim -c :ZenMode"
DEFAULT_NOTE_NAME="$(date +'%m-%d').md" DEFAULT_NEW_NOTE_NAME="$(date +'%m-%d').md"
# Some fzf commands
FZF="fzf -1 --layout=reverse --info=inline --height=10%"
FZF_SEARCH="$FZF -q "
usage() { usage() {
cat <<-EOF cat <<-EOF
@ -24,91 +28,71 @@ usage() {
help Print this message help Print this message
Notes: Notes:
'T' is an edit pointer to the default file name (today's date) Edit 'T' to get today's note
Folder and file names are searched with fuzzy search Folder and file names are searched with fuzzy search
Ned will automatically activate the ZenMode neovim extension ned will automatically activate the ZenMode neovim extension
Current repository path: '$NOTES_DIR' Current repository path: '$BASE_DIR'
EOF EOF
} }
open_file() { open_file() {
cd $(dirname "$1") cd "$(dirname "$1")" || return
$EDITOR_CMD "$1" $EDITOR_CMD "$1"
} }
# Usage:
# edit "unused param" ["search_directory"] "search_term"
edit() { edit() {
check_setup check_setup
# This logic is to allow the $2 to be optional
if [ -n "$3" ]; then if [ -n "$3" ]; then
SEARCH_TERM="$3" SEARCH_TERM="$3"
if [ -d "$NOTES_DIR/$2"* ]; then NOTE_DIR="$(cd "$BASE_DIR" && fd -t f | sed 's-\./--g' | $FZF_SEARCH "$2")" || exit
NOTES_DIR="$(echo "$NOTES_DIR"/"$2"*)"
fi
elif [ -n "$2" ]; then elif [ -n "$2" ]; then
SEARCH_TERM="$2" SEARCH_TERM="$2"
fi fi
# Searching for 'T' will get you today's note
if [ "$SEARCH_TERM" = 'T' ]; then if [ "$SEARCH_TERM" = 'T' ]; then
SEARCH_TERM="$DEFAULT_NOTE_NAME" SEARCH_TERM="$DEFAULT_NEW_NOTE_NAME"
fi fi
NOTE_PATH="$NOTES_DIR/$(cd "$NOTES_DIR" && fzf -1 -q "$SEARCH_TERM" --layout=reverse --info=inline --height=10%)" || exit NOTE="$NOTE_DIR/$(cd "$NOTE_DIR" && $FZF_SEARCH "$SEARCH_TERM")" || exit
cd "$(dirname "$NOTE_PATH")" open_file "$NOTE"
open_file "$NOTE_PATH"
} }
# Usage: new "unused param" "save directory" ["note name"]
new() { new() {
check_setup check_setup
# Find out note name # Note name
if [ -n "$3" ]; then if [ -n "$3" ]; then
NOTE_NAME="$3.md" NEW_NOTE_NAME="$3.md"
else else
NOTE_NAME="$DEFAULT_NOTE_NAME" NEW_NOTE_NAME="$DEFAULT_NEW_NOTE_NAME"
fi fi
# TODO: use fzf instead of fd + fzf for picking the directory # Note dir
NOTE_PATH="$(fd -ad 1 "^$2" "$NOTES_DIR")" NEW_NOTE_DIR="$BASE_DIR/$(cd "$BASE_DIR" && fd -d 1 | sed 's-\./--g' | $FZF_SEARCH "$2")" || exit
# check if there were no results
if [ -z "$NOTE_PATH" ]; then
echo "No directories found for '$2'."
echo "All possible directories:"
(cd "$NOTES_DIR" && fd -d 1 -t d)
exit
# if there was more than one result if [ -f "$NEW_NOTE_DIR/$NEW_NOTE_NAME" ]; then
elif [ "$(echo "$NOTE_PATH" | wc -l)" -gt 1 ]; then open_file "$NEW_NOTE_DIR/$NEW_NOTE_NAME"
NOTE_PATH="$(echo "$NOTE_PATH" | fzy)" || exit
fi
# TODO: allow setting location for files to go with $3, and $4 for file name
# 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 else
PROMPT="Place note in… " cd "$NEW_NOTE_DIR" || exit
NOTE_PATH="$(cd "$NOTE_PATH" && fd -ad 1 -t d | fzy -p "$PROMPT")" || exit
fi
if [ -f "$NOTE_PATH/$NOTE_NAME" ]; then
echo "File already exists. Opening..."
sleep 0.5
open_file "$NOTE_PATH/$NOTE_NAME"
else
cd "$NOTE_PATH" || exit
# apply a template for notes # apply a template for notes
cat <<-EOF >> "$NOTE_PATH/$NOTE_NAME" cat <<-EOF >> "$NEW_NOTE_DIR/$NEW_NOTE_NAME"
# $NOTE_NAME # $NEW_NOTE_NAME
<!-- vim: wrap nonu nornu <!-- vim: wrap nonu nornu
--> -->
EOF EOF
STARTING_MD5="$(md5sum "$NOTE_PATH/$NOTE_NAME")" STARTING_MD5="$(md5sum "$NEW_NOTE_DIR/$NEW_NOTE_NAME")"
open_file "$NOTE_PATH/$NOTE_NAME" open_file "$NEW_NOTE_DIR/$NEW_NOTE_NAME"
if [ "$(md5sum "$NOTE_PATH/$NOTE_NAME")" = "$STARTING_MD5" ]; then # Delete the file if no changes were made
rm "$NOTE_PATH/$NOTE_NAME" if [ "$(md5sum "$NEW_NOTE_DIR/$NEW_NOTE_NAME")" = "$STARTING_MD5" ]; then
rm "$NEW_NOTE_DIR/$NEW_NOTE_NAME"
fi fi
fi fi
} }
@ -120,13 +104,16 @@ subshell() {
} }
shell() { shell() {
cd "$NOTES_DIR" && subshell if [ -n "${1+foo}" ]; then
DIR="$BASE_DIR/$(cd "$BASE_DIR" && fd -d 1 -t d | sed 's-\./--g' | $FZF_SEARCH "$1")" || exit
fi
cd "$DIR" || exit
} }
check_setup() { check_setup() {
# perform preliminary checks to make sure the script can run properly # perform preliminary checks to make sure the script can run properly
if ! [ -d "$NOTES_DIR" ]; then if ! [ -d "$BASE_DIR" ]; then
echo "Error: Notes directory ($NOTES_DIR) does not exist or is not a directory" echo "Error: Notes directory ($BASE_DIR) does not exist or is not a directory"
exit 1 exit 1
fi fi
} }
@ -140,10 +127,10 @@ case "${1}" in
# edit an existing document or file # edit an existing document or file
'edit'|'ed') edit $@;; 'edit'|'ed') edit $@;;
# run a shell (cd to) in the directory # run a shell (cd to) in the directory
'x'|'cd'|'shell') shell;; 'cd') shell $@;;
# edit ned (the file you are looking at right now) # edit ned (the file you are looking at right now)
'ned') $EDITOR "$(realpath "$0")";; 'ned') $EDITOR "$(realpath "$0")";;
*) echo "no such option";; *) echo "no such option";;
esac esac
# vim: et sw=3 ts=3 ft=bash # vim: et sw=3 ts=3 ft=sh