Browse Source

feature: refactor include and cancel operations

new_list
Lucas 12 months ago
parent
commit
e707ef6aec
  1. 120
      remind

120
remind

@ -1,64 +1,102 @@
#!/bin/bash #!/bin/bash
CMD="cancel" CMD="cancel"
SOUND_LOCATION="$HOME/.local/share/sounds" SOUND_LOCATION="$HOME/.local/share/sounds"
ICON_LOCATION="$HOME/.local/share/icons/clock.svg" ICON_LOCATION="$HOME/.local/share/icons/clock.svg"
LIST_LOCATION="$HOME/.remind"
_execute() { _execute() {
if [[ $TIMECHOOSEN =~ ^[1-9]?[0-9](s|m|h)$ ]]; then if [[ $TIMECHOOSEN =~ ^[1-9]?[0-9](s|m|h)$ ]]; then
sleep $TIMECHOOSEN INFO="$MSG; $TIMECHOOSEN; $BASHPID; $(date "+%Y-%m-%d %H:%M:%S %:::z");"
notify-send -i $ICON_LOCATION "Reminder" "$MSG" echo -e "$INFO" >> $LIST_LOCATION
ffplay -nodisp -autoexit -hide_banner -loglevel error $SOUND_LOCATION/beep.mp3 sleep $TIMECHOOSEN
else sed -i "/$INFO/d" $LIST_LOCATION
echo -e "\033[31m[error]\nThe time must be provided as the example: 10(s|m|h).\n\033[0m" notify-send -i $ICON_LOCATION "Reminder" "$MSG"
fi ffplay -nodisp -autoexit -hide_banner -loglevel error $SOUND_LOCATION/beep.mp3
else
echo -e "\033[31mThe time must be provided as the example: 10(s|m|h).\n\033[0m"
fi
} }
_parse() { _parse() {
if [[ $@ == "$CMD" ]]; then if [[ $@ == "$CMD" ]]; then
_pre_cancel _cancel
else else
_remind $@ _remind $@
fi fi
} }
_remind() { _remind() {
MSG="$@" MSG="$@"
if [[ -z "$@" ]]; then
echo -e "\033[31m[error]\nProvide a remind text first.\n\033[0m"
else
read -p 'Time to delay: ' TIMECHOOSEN
if [[ -z "$TIMECHOOSEN" ]]; then
echo -e "\033[31m[error]\nProvide a time.\n\033[0m"
else
_execute $@ $TIMECHOOSEN &
fi
fi
}
_pre_cancel() { if [[ -z "$@" ]]; then
NUMOFLINES=$(pgrep -a remind | wc -l) echo -e "\033[31mProvide a remind text first.\n\033[0m"
if [[ $NUMOFLINES -le 2 ]]; then else
echo "No reminders to cancel" read -p 'Time to delay: ' TIMECHOOSEN
else if [[ -z "$TIMECHOOSEN" ]]; then
_cancel echo -e "\033[31m[error]\nProvide a time.\n\033[0m"
fi else
_execute $@ $TIMECHOOSEN &
fi
fi
} }
_cancel() { _cancel() {
pgrep -a remind | awk -F "remind" -v nl="$NUMOFLINES" '{ if (NR < (nl - 1)) { print "[" NR "]" $NF}}' IFS=$'\n' read -d '' -r -a lines < $LIST_LOCATION
read -p "Please choose a number to cancel [1-$(($NUMOFLINES - 2))]: " CHOOSE REMIND_QTY="${#lines[@]}"
if [[ $CHOOSE =~ ^[1-9]?[0-9] ]]; then
if [[ $CHOOSE -le $(($NUMOFLINES - 2)) ]]; then if [[ $REMIND_QTY < 1 ]]; then
kill $(pgrep -a remind | awk -F " " -v op="$CHOOSE" '{ if (NR == op) { print $1}}') echo "There is no reminders"
else exit 0
echo -e "\033[31m[error]\nInvalid option.\n\033[0m" fi
fi
else for idx in "${!lines[@]}"; do
echo -e "\033[31m[error]\nInvalid option.\n\033[0m" IFS=';' read -r -a array <<< "${lines[$idx]}"
fi time_type=$(echo "${array[1]}" | grep -o "[a-z]")
time_text=$(echo "${array[1]}" | grep -Eo "[0-9]{1,2}")
ti=$(echo "${array[3]}")
if [[ $time_type == "h" ]]; then
end=$(date -d "$ti + $time_text hours" +"%s")
elif [[ $time_type == "m" ]]; then
end=$(date -d "$ti + $time_text minutes" +"%s")
else
end=$(date -d "$ti + $time_text seconds" +"%s")
fi
now=$(date "+%s")
time_left=$(date -u -d "0 $end seconds - $now seconds" +"%H:%M:%S")
echo -e "[$idx] ${array[0]} $time_left"
done
if [[ $REMIND_QTY > 1 ]]; then
TEXT_REMOVE="[0-$(($REMIND_QTY - 1))]"
else
TEXT_REMOVE="[0]"
fi
echo ""
read -p "Press [a] to cancel all reminders or choose a number to cancel $TEXT_REMOVE: " CHOOSE
if [[ $CHOOSE =~ ^[0-9]{1,3}$|a ]]; then
if [[ $CHOOSE == "a" ]]; then
for idx in "${!lines[@]}"; do
IFS=';' read -r -a PROC_NUM <<< "${lines[$idx]}"
kill ${PROC_NUM[2]} && sed -i "/${lines[$idx]}/d" $LIST_LOCATION
done
echo -e "\033[32mAll reminders are canceled."
elif [[ $CHOOSE -le $((REMIND_QTY - 1)) ]]; then
IFS=';' read -r -a PROC_NUM <<< "${lines[$CHOOSE]}"
kill ${PROC_NUM[2]} && sed -i "/${lines[$CHOOSE]}/d" $LIST_LOCATION && echo -e "\033[32mReminder canceled"
else
echo -e "\033[31mInvalid option.\n\033[0m"
fi
else
echo -e "\033[31mInvalid option.\n\033[0m"
fi
} }
_parse $@ _parse $@

Loading…
Cancel
Save