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 all commits
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
37 changes: 28 additions & 9 deletions src/giph
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ MICROPHONE=0
MICROPHONE_SOURCE=default
FORMAT_OVERWRITE=""

LAST_NOTIFICATION_ID=0

function print_version() {
echo "$VERSION"
exit 0
Expand Down Expand Up @@ -79,7 +81,7 @@ function log() {
}

function log_error() {
notify "$1" "critical"
notify "$1"
log "\033[0;31mERROR:\033[0m $1" -1 "${2:-true}" true
}

Expand All @@ -88,20 +90,35 @@ function log_warning() {
}

function log_success() {
notify "$1" "normal"
notify "$1"
log "\033[0;32mSUCCESS:\033[0m $1" 0 "${2:-true}"
}

function log_info() {
log "\033[0;36mINFO:\033[0m $1" 0 "${2:-true}"
}

# send a notification - (message:string, durationms:int)
function notify() {
[ "$NOTIFY" = 1 ] && {
notify=(notify-send -t 3000)
notify+=(-u "$2")
notify+=("giph" "$1")
"${notify[@]}"
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": <1>}' "${3:-5000}" | sed -E 's/^.* ([0-9]+).*$/\1/')
}
}

function close_last_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 @@ -373,7 +390,7 @@ function record() {
;;
esac

[ -n "$DELAY" ] && [ "$DELAY" -gt 0 ] && countdown_cli "$DELAY" "recording starts in"
[ -n "$DELAY" ] && [ "$DELAY" -gt 0 ] && countdown "$DELAY" "recording starts in"

ffmpeg_command="${ffmpeg[*]}"
log "ffmpeg command: '$ffmpeg_command'" 2 true
Expand All @@ -384,7 +401,7 @@ function record() {
log "started recording video with ffmpeg" 1 true

if [ -n "$TIMER" ] && [ "$TIMER" -gt 0 ]; then
countdown_cli "$TIMER" "recording stops in"
countdown "$TIMER" "recording stops in"
else
stop_recording_handler_cli
fi
Expand All @@ -395,14 +412,16 @@ function record() {
log "completed ffmpeg video recording" 1 true
}

function countdown_cli() {
function countdown() {
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" 2000
if [ "$seconds" -gt 0 ]; then
sleep 1
else
log "\r\033[K" 0 false false true
close_last_notification
fi
: "$((seconds--))"
done
Expand Down