Browse Source

feature: refactor include and cancel operations

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

74
remind

@ -1,23 +1,26 @@
#!/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
INFO="$MSG; $TIMECHOOSEN; $BASHPID; $(date "+%Y-%m-%d %H:%M:%S %:::z");"
echo -e "$INFO" >> $LIST_LOCATION
sleep $TIMECHOOSEN sleep $TIMECHOOSEN
sed -i "/$INFO/d" $LIST_LOCATION
notify-send -i $ICON_LOCATION "Reminder" "$MSG" notify-send -i $ICON_LOCATION "Reminder" "$MSG"
ffplay -nodisp -autoexit -hide_banner -loglevel error $SOUND_LOCATION/beep.mp3 ffplay -nodisp -autoexit -hide_banner -loglevel error $SOUND_LOCATION/beep.mp3
else else
echo -e "\033[31m[error]\nThe time must be provided as the example: 10(s|m|h).\n\033[0m" echo -e "\033[31mThe time must be provided as the example: 10(s|m|h).\n\033[0m"
fi fi
} }
_parse() { _parse() {
if [[ $@ == "$CMD" ]]; then if [[ $@ == "$CMD" ]]; then
_pre_cancel _cancel
else else
_remind $@ _remind $@
fi fi
@ -27,7 +30,7 @@ _remind() {
MSG="$@" MSG="$@"
if [[ -z "$@" ]]; then if [[ -z "$@" ]]; then
echo -e "\033[31m[error]\nProvide a remind text first.\n\033[0m" echo -e "\033[31mProvide a remind text first.\n\033[0m"
else else
read -p 'Time to delay: ' TIMECHOOSEN read -p 'Time to delay: ' TIMECHOOSEN
if [[ -z "$TIMECHOOSEN" ]]; then if [[ -z "$TIMECHOOSEN" ]]; then
@ -38,26 +41,61 @@ _remind() {
fi fi
} }
_pre_cancel() { _cancel() {
NUMOFLINES=$(pgrep -a remind | wc -l) IFS=$'\n' read -d '' -r -a lines < $LIST_LOCATION
if [[ $NUMOFLINES -le 2 ]]; then REMIND_QTY="${#lines[@]}"
echo "No reminders to cancel"
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 else
_cancel end=$(date -d "$ti + $time_text seconds" +"%s")
fi fi
}
_cancel() { now=$(date "+%s")
pgrep -a remind | awk -F "remind" -v nl="$NUMOFLINES" '{ if (NR < (nl - 1)) { print "[" NR "]" $NF}}' time_left=$(date -u -d "0 $end seconds - $now seconds" +"%H:%M:%S")
read -p "Please choose a number to cancel [1-$(($NUMOFLINES - 2))]: " CHOOSE
if [[ $CHOOSE =~ ^[1-9]?[0-9] ]]; then echo -e "[$idx] ${array[0]} $time_left"
if [[ $CHOOSE -le $(($NUMOFLINES - 2)) ]]; then done
kill $(pgrep -a remind | awk -F " " -v op="$CHOOSE" '{ if (NR == op) { print $1}}')
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 else
echo -e "\033[31m[error]\nInvalid option.\n\033[0m" echo -e "\033[31mInvalid option.\n\033[0m"
fi fi
else else
echo -e "\033[31m[error]\nInvalid option.\n\033[0m" echo -e "\033[31mInvalid option.\n\033[0m"
fi fi
} }

Loading…
Cancel
Save