Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support countdown via notification #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/giph
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ MICROPHONE=0
MICROPHONE_SOURCE=default
FORMAT_OVERWRITE=""

# last notification id
radiantly marked this conversation as resolved.
Show resolved Hide resolved
LAST_NOTIFICATION_ID=0

function print_version() {
echo "$VERSION"
exit 0
Expand Down Expand Up @@ -96,12 +99,33 @@ function log_info() {
log "\033[0;36mINFO:\033[0m $1" 0 "${2:-true}"
}

# send a notification - (message:string, urgency:string, durationms:int)
radiantly marked this conversation as resolved.
Show resolved Hide resolved
function notify() {
[ "$NOTIFY" = 1 ] && {
notify=(notify-send -t 3000)
notify+=(-u "$2")
notify+=("giph" "$1")
"${notify[@]}"
case "$2" in
low) URGENCY=0 ;;
normal) URGENCY=1 ;;
critical) URGENCY=2 ;;
esac
LAST_NOTIFICATION_ID=$(gdbus call \
--session \
--dest=org.freedesktop.Notifications \
--object-path=/org/freedesktop/Notifications \
--method=org.freedesktop.Notifications.Notify \
"giph" "$LAST_NOTIFICATION_ID" "" "giph" "$1" \
'[]' '{"urgency": <'$URGENCY'>}' "${3:-5000}" | sed -E 's/^.* ([0-9]+).*$/\1/')
}
}

# closes the last notification
radiantly marked this conversation as resolved.
Show resolved Hide resolved
function close_notification() {
[ "$NOTIFY" = 1 ] && {
gdbus call \
--session \
--dest org.freedesktop.Notifications \
--object-path /org/freedesktop/Notifications \
--method org.freedesktop.Notifications.CloseNotification "$LAST_NOTIFICATION_ID"
LAST_NOTIFICATION_ID=0
}
}

Expand Down Expand Up @@ -399,10 +423,12 @@ function countdown_cli() {
seconds="$1"
while [ "$seconds" -ge 0 ]; do
log "\r\033[K\033[0;36m$2:\033[0m $seconds" 0 false false true
notify "$2 $seconds" "normal" 2000
radiantly marked this conversation as resolved.
Show resolved Hide resolved
if [ "$seconds" -gt 0 ]; then
sleep 1
else
log "\r\033[K" 0 false false true
close_notification
fi
: "$((seconds--))"
done
Expand Down