refactor(notes): use switch-case for long if-else block

This commit is contained in:
Ayo Ayco 2025-08-23 18:40:31 +02:00
parent 5969c69bab
commit f62aaf6cb1

109
notes.sh
View file

@ -50,38 +50,42 @@ function createNote() {
} }
function main() { function main() {
case "$1" in
## DIFF # DIFF: Show git diff of staged changes
if [ "$1" = "diff" ] || [ "$1" = "d" ]; then diff|d)
cd $notes_dir cd "$notes_dir"
git add . git add .
git diff --staged . git diff --staged .
## SYNC notes in directory ;;
elif [ "$1" = "sync" ] || [ "$1" = "s" ]; then
notesSync
## LIST notes in directory # SYNC: Sync notes directory with remote
elif [ "$1" = "list" ] || [ "$1" = "l" ]; then sync|s)
notesSync
;;
# LIST: List all notes in directory
list|l)
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 ) files=( "$notes_dir"/*.md )
fi fi
index=0 index=0
for file in "${files[@]##*/}"; do for file in "${files[@]##*/}"; do
((index++)) ((index++))
echo "$index) $file" echo "$index) $file"
done done
;;
## OPEN a note from a list # OPEN: Open a note from list or by index
elif [ "$1" = "open" ] || [ "$1" = "o" ]; then open|o)
notesSync notesSync
files=( $notes_dir/*.md ) files=( "$notes_dir"/*.md )
if ! [ "$2" = "" ]; then if ! [ "$2" = "" ]; then
index=($2-1) index=$((2-1))
open_file=${files[$index]} open_file=${files[$index]}
editFile "$open_file" editFile "$open_file"
else else
@ -99,14 +103,15 @@ function main() {
} }
done done
fi fi
;;
## REMOVE a note from a list # REMOVE: Remove a note from list or by index
elif [ "$1" = "remove" ] || [ "$1" = "rm" ]; then remove|rm)
notesSync notesSync
files=( $notes_dir/*.md ) files=( "$notes_dir"/*.md )
if ! [ "$2" = "" ]; then if ! [ "$2" = "" ]; then
index=($2-1) index=$((2-1))
remove_file=${files[$index]} remove_file=${files[$index]}
echo "Removing $remove_file" echo "Removing $remove_file"
rm "$remove_file" rm "$remove_file"
@ -127,66 +132,16 @@ function main() {
} }
done done
fi fi
;;
## ARCHIVE a note from a list # ARCHIVE: Move a note to archive directory
elif [ "$1" = "archive" ] || [ "$1" = "a" ]; then archive|a)
notesSync ;;
files=( $notes_dir/*.md ) # DEFAULT: Default action - create new note
*)
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
createNote createNote
fi ;;
esac
} }
main main