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
CMD="cancel"
SOUND_LOCATION="$HOME/.local/share/sounds"
ICON_LOCATION="$HOME/.local/share/icons/clock.svg"
LIST_LOCATION="$HOME/.remind"
_execute() {
if [[ $TIMECHOOSEN =~ ^[1-9]?[0-9](s|m|h)$ ]]; then
sleep $TIMECHOOSEN
notify-send -i $ICON_LOCATION "Reminder" "$MSG"
ffplay -nodisp -autoexit -hide_banner -loglevel error $SOUND_LOCATION/beep.mp3
else
echo -e "\033[31m[error]\nThe time must be provided as the example: 10(s|m|h).\n\033[0m"
fi
if [[ $TIMECHOOSEN =~ ^[1-9]?[0-9](s|m|h)$ ]]; then
INFO="$MSG; $TIMECHOOSEN; $BASHPID; $(date "+%Y-%m-%d %H:%M:%S %:::z");"
echo -e "$INFO" >> $LIST_LOCATION
sleep $TIMECHOOSEN
sed -i "/$INFO/d" $LIST_LOCATION
notify-send -i $ICON_LOCATION "Reminder" "$MSG"
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() {
if [[ $@ == "$CMD" ]]; then
_pre_cancel
_cancel
else
_remind $@
_remind $@
fi
}
_remind() {
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
}
MSG="$@"
_pre_cancel() {
NUMOFLINES=$(pgrep -a remind | wc -l)
if [[ $NUMOFLINES -le 2 ]]; then
echo "No reminders to cancel"
else
_cancel
fi
if [[ -z "$@" ]]; then
echo -e "\033[31mProvide 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
}
_cancel() {
pgrep -a remind | awk -F "remind" -v nl="$NUMOFLINES" '{ if (NR < (nl - 1)) { print "[" NR "]" $NF}}'
read -p "Please choose a number to cancel [1-$(($NUMOFLINES - 2))]: " CHOOSE
if [[ $CHOOSE =~ ^[1-9]?[0-9] ]]; then
if [[ $CHOOSE -le $(($NUMOFLINES - 2)) ]]; then
kill $(pgrep -a remind | awk -F " " -v op="$CHOOSE" '{ if (NR == op) { print $1}}')
else
echo -e "\033[31m[error]\nInvalid option.\n\033[0m"
fi
else
echo -e "\033[31m[error]\nInvalid option.\n\033[0m"
fi
IFS=$'\n' read -d '' -r -a lines < $LIST_LOCATION
REMIND_QTY="${#lines[@]}"
if [[ $REMIND_QTY < 1 ]]; then
echo "There is no reminders"
exit 0
fi
for idx in "${!lines[@]}"; do
IFS=';' read -r -a array <<< "${lines[$idx]}"
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 $@

Loading…
Cancel
Save