refactor(notes): use switch-case for long if-else block
This commit is contained in:
parent
5969c69bab
commit
f62aaf6cb1
1 changed files with 83 additions and 128 deletions
211
notes.sh
211
notes.sh
|
@ -50,143 +50,98 @@ function createNote() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
|
case "$1" in
|
||||||
|
# DIFF: Show git diff of staged changes
|
||||||
|
diff|d)
|
||||||
|
cd "$notes_dir"
|
||||||
|
git add .
|
||||||
|
git diff --staged .
|
||||||
|
;;
|
||||||
|
|
||||||
## DIFF
|
# SYNC: Sync notes directory with remote
|
||||||
if [ "$1" = "diff" ] || [ "$1" = "d" ]; then
|
sync|s)
|
||||||
cd $notes_dir
|
|
||||||
git add .
|
|
||||||
git diff --staged .
|
|
||||||
## SYNC notes in directory
|
|
||||||
elif [ "$1" = "sync" ] || [ "$1" = "s" ]; then
|
|
||||||
notesSync
|
|
||||||
|
|
||||||
## LIST notes in directory
|
|
||||||
elif [ "$1" = "list" ] || [ "$1" = "l" ]; then
|
|
||||||
echo "ACTIVE NOTES: "
|
|
||||||
notesSync
|
|
||||||
if ! [ "$2" = "" ]; then
|
|
||||||
files=( $notes_dir/$2/*.md )
|
|
||||||
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 )
|
|
||||||
|
|
||||||
if ! [ "$2" = "" ]; then
|
|
||||||
index=($2-1)
|
|
||||||
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
|
;;
|
||||||
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
|
# LIST: List all notes in directory
|
||||||
elif [ "$1" = "archive" ] || [ "$1" = "a" ]; then
|
list|l)
|
||||||
notesSync
|
echo "ACTIVE NOTES: "
|
||||||
files=( $notes_dir/*.md )
|
|
||||||
|
|
||||||
if ! [ "$2" = "" ]; then
|
|
||||||
index=($2-1)
|
|
||||||
archive_file=${files[$index]}
|
|
||||||
echo "Archiving $archive_file"
|
|
||||||
mv "$archive_file" "${archive_dir}/"
|
|
||||||
notesSync
|
notesSync
|
||||||
else
|
if ! [ "$2" = "" ]; then
|
||||||
PS3="Archive file #: "
|
files=( "$notes_dir/$2"/*.md )
|
||||||
echo "Move a note to ARCHIVE ($(ls ${archive_dir} | wc -l))."
|
else
|
||||||
select file in "${files[@]##*/}"; do
|
files=( "$notes_dir"/*.md )
|
||||||
{
|
fi
|
||||||
echo "Archiving $file"
|
index=0
|
||||||
mv "${notes_dir}/${file}" "${archive_dir}/"
|
for file in "${files[@]##*/}"; do
|
||||||
notesSync
|
((index++))
|
||||||
break
|
echo "$index) $file"
|
||||||
} ||
|
done
|
||||||
{
|
;;
|
||||||
echo "bad choice"
|
|
||||||
break
|
|
||||||
}
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
## COPY content a note from a list
|
# OPEN: Open a note from list or by index
|
||||||
elif [ "$1" = "copy" ] || [ "$1" = "c" ]; then
|
open|o)
|
||||||
files=( $notes_dir/*.md )
|
notesSync
|
||||||
|
files=( "$notes_dir"/*.md )
|
||||||
|
|
||||||
if ! [ "$2" = "" ]; then
|
if ! [ "$2" = "" ]; then
|
||||||
index=($2-1)
|
index=$((2-1))
|
||||||
copy_file=${files[$index]}
|
open_file=${files[$index]}
|
||||||
echo "Copied content of $copy_file"
|
editFile "$open_file"
|
||||||
xclip -sel c < "$copy_file"
|
else
|
||||||
else
|
PS3="Open file #: "
|
||||||
PS3="Copy file content #: "
|
echo "Please select a file to OPEN."
|
||||||
echo "Select a note to COPY Content."
|
select file in "${files[@]##*/}"; do
|
||||||
select file in "${files[@]##*/}"; do
|
{
|
||||||
{
|
echo "Opening $file"
|
||||||
echo "Copied content of $file"
|
editFile "$file"
|
||||||
xclip -sel c < "${notes_dir}/${file}"
|
break
|
||||||
break
|
} ||
|
||||||
} ||
|
{
|
||||||
{
|
echo "bad choice"
|
||||||
echo "bad choice"
|
break
|
||||||
break
|
}
|
||||||
}
|
done
|
||||||
done
|
fi
|
||||||
fi
|
;;
|
||||||
|
|
||||||
|
# REMOVE: Remove a note from list or by index
|
||||||
|
remove|rm)
|
||||||
|
notesSync
|
||||||
|
files=( "$notes_dir"/*.md )
|
||||||
|
|
||||||
## CREATE a note (default)
|
if ! [ "$2" = "" ]; then
|
||||||
else
|
index=$((2-1))
|
||||||
createNote
|
remove_file=${files[$index]}
|
||||||
fi
|
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
|
||||||
|
;;
|
||||||
|
|
||||||
|
# ARCHIVE: Move a note to archive directory
|
||||||
|
archive|a)
|
||||||
|
;;
|
||||||
|
# DEFAULT: Default action - create new note
|
||||||
|
*)
|
||||||
|
createNote
|
||||||
|
;;
|
||||||
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
main
|
main
|
Loading…
Reference in a new issue