refactor(notes): clean up; wrap login in main function

This commit is contained in:
Ayo Ayco 2025-08-23 17:46:35 +02:00
parent 3f2f90b1c9
commit 5969c69bab

257
notes.sh
View file

@ -1,15 +1,9 @@
#! /usr/bin/bash #! /usr/bin/bash
## notes management
# Load config # Load config
. ${HOME}/ayo.conf . ${HOME}/ayo.conf
. ${scripts_dir}/functions.sh . ${scripts_dir}/functions.sh
# TODO: write log for echoes with >>>
command=$1
getopts "t" typora; #check if -t flag is given getopts "t" typora; #check if -t flag is given
function editFile() { function editFile() {
@ -55,139 +49,144 @@ function createNote() {
} }
} }
function main() {
## DIFF ## DIFF
if [ "$1" = "diff" ] || [ "$1" = "d" ]; then if [ "$1" = "diff" ] || [ "$1" = "d" ]; then
cd $notes_dir cd $notes_dir
git add . git add .
git diff --staged . git diff --staged .
## SYNC notes in directory ## SYNC notes in directory
elif [ "$1" = "sync" ] || [ "$1" = "s" ]; then elif [ "$1" = "sync" ] || [ "$1" = "s" ]; then
notesSync notesSync
## LIST notes in directory ## LIST notes in directory
elif [ "$1" = "list" ] || [ "$1" = "l" ]; then elif [ "$1" = "list" ] || [ "$1" = "l" ]; then
echo "ACTIVE NOTES: " echo "ACTIVE NOTES: "
notesSync notesSync
if ! [ "$2" = "" ]; then if ! [ "$2" = "" ]; then
files=( $notes_dir/$2/*.md ) files=( $notes_dir/$2/*.md )
else else
files=( $notes_dir/*.md )
fi
index=0
for file in "${files[@]##*/}"; do
((index++))
echo "$index) $file"
done
## OPEN a note from a list
elif [ "$1" = "open" ] || [ "$1" = "o" ]; then
notesSync
files=( $notes_dir/*.md ) files=( $notes_dir/*.md )
fi
index=0
for file in "${files[@]##*/}"; do
((index++))
echo "$index) $file"
done
## OPEN a note from a list if ! [ "$2" = "" ]; then
elif [ "$1" = "open" ] || [ "$1" = "o" ]; then index=($2-1)
notesSync open_file=${files[$index]}
files=( $notes_dir/*.md ) editFile "$open_file"
else
PS3="Open file #: "
echo "Please select a file to OPEN."
select file in "${files[@]##*/}"; do
{
echo "Opening $file"
editFile "$file"
break
} ||
{
echo "bad choice"
break
}
done
fi
if ! [ "$2" = "" ]; then ## REMOVE a note from a list
index=($2-1) elif [ "$1" = "remove" ] || [ "$1" = "rm" ]; then
open_file=${files[$index]}
editFile "$open_file"
else
PS3="Open file #: "
echo "Please select a file to OPEN."
select file in "${files[@]##*/}"; do
{
echo "Opening $file"
editFile "$file"
break
} ||
{
echo "bad choice"
break
}
done
fi
## REMOVE a note from a list
elif [ "$1" = "remove" ] || [ "$1" = "rm" ]; then
notesSync
files=( $notes_dir/*.md )
if ! [ "$2" = "" ]; then
index=($2-1)
remove_file=${files[$index]}
echo "Removing $remove_file"
rm "$remove_file"
notesSync notesSync
else files=( $notes_dir/*.md )
PS3="Remove file #: "
echo "Please select a file to REMOVE."
select file in "${files[@]##*/}"; do
{
echo "Removing $file"
rm "${notes_dir}/${file}"
notesSync
break
} ||
{
echo "bad choice"
break
}
done
fi
## ARCHIVE a note from a list if ! [ "$2" = "" ]; then
elif [ "$1" = "archive" ] || [ "$1" = "a" ]; then index=($2-1)
notesSync remove_file=${files[$index]}
files=( $notes_dir/*.md ) echo "Removing $remove_file"
rm "$remove_file"
notesSync
else
PS3="Remove file #: "
echo "Please select a file to REMOVE."
select file in "${files[@]##*/}"; do
{
echo "Removing $file"
rm "${notes_dir}/${file}"
notesSync
break
} ||
{
echo "bad choice"
break
}
done
fi
if ! [ "$2" = "" ]; then ## ARCHIVE a note from a list
index=($2-1) elif [ "$1" = "archive" ] || [ "$1" = "a" ]; then
archive_file=${files[$index]}
echo "Archiving $archive_file"
mv "$archive_file" "${archive_dir}/"
notesSync notesSync
files=( $notes_dir/*.md )
if ! [ "$2" = "" ]; then
index=($2-1)
archive_file=${files[$index]}
echo "Archiving $archive_file"
mv "$archive_file" "${archive_dir}/"
notesSync
else
PS3="Archive file #: "
echo "Move a note to ARCHIVE ($(ls ${archive_dir} | wc -l))."
select file in "${files[@]##*/}"; do
{
echo "Archiving $file"
mv "${notes_dir}/${file}" "${archive_dir}/"
notesSync
break
} ||
{
echo "bad choice"
break
}
done
fi
## COPY content a note from a list
elif [ "$1" = "copy" ] || [ "$1" = "c" ]; then
files=( $notes_dir/*.md )
if ! [ "$2" = "" ]; then
index=($2-1)
copy_file=${files[$index]}
echo "Copied content of $copy_file"
xclip -sel c < "$copy_file"
else
PS3="Copy file content #: "
echo "Select a note to COPY Content."
select file in "${files[@]##*/}"; do
{
echo "Copied content of $file"
xclip -sel c < "${notes_dir}/${file}"
break
} ||
{
echo "bad choice"
break
}
done
fi
## CREATE a note (default)
else else
PS3="Archive file #: " createNote
echo "Move a note to ARCHIVE ($(ls ${archive_dir} | wc -l))."
select file in "${files[@]##*/}"; do
{
echo "Archiving $file"
mv "${notes_dir}/${file}" "${archive_dir}/"
notesSync
break
} ||
{
echo "bad choice"
break
}
done
fi fi
## COPY content a note from a list }
elif [ "$1" = "copy" ] || [ "$1" = "c" ]; then
files=( $notes_dir/*.md )
if ! [ "$2" = "" ]; then main
index=($2-1)
copy_file=${files[$index]}
echo "Copied content of $copy_file"
xclip -sel c < "$copy_file"
else
PS3="Copy file content #: "
echo "Select a note to COPY Content."
select file in "${files[@]##*/}"; do
{
echo "Copied content of $file"
xclip -sel c < "${notes_dir}/${file}"
break
} ||
{
echo "bad choice"
break
}
done
fi
## CREATE a note (default)
else
createNote
fi