From 7039857b9542027816b188050ac1f110531f9931 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 29 Nov 2022 00:57:16 -0500 Subject: [PATCH] Make the indicator update nearly immediately from sway keybindings being pressed. --- .../waybar_recording_indicator_freebsd.bash | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/ansible/roles/waybar/files/waybar_scripts/waybar_recording_indicator_freebsd.bash b/ansible/roles/waybar/files/waybar_scripts/waybar_recording_indicator_freebsd.bash index f9ec329..8056ccb 100644 --- a/ansible/roles/waybar/files/waybar_scripts/waybar_recording_indicator_freebsd.bash +++ b/ansible/roles/waybar/files/waybar_scripts/waybar_recording_indicator_freebsd.bash @@ -6,11 +6,31 @@ IFS=$'\n\t' DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" SLEEP_INTERVAL=${SLEEP_INTERVAL:-10} +DELAY_ACTION=${DELAY_ACTION:-2} function main { local previous_reported="-1" + local now=$(date +%s) + local check_again_at=$((now + SLEEP_INTERVAL)) + local next_sleep_interval=$SLEEP_INTERVAL + + stream_key_binding_activations | while true; do + if read -t "$next_sleep_interval" event; then + now=$(date +%s) + latest_allowed_check=$((now + DELAY_ACTION)) + if [ "$latest_allowed_check" -lt "$check_again_at" ]; then + # Scheduling a debounced action + check_again_at=$latest_allowed_check + next_sleep_interval=$DELAY_ACTION + else + # New debounced event would trigger after a check is already scheduled + next_sleep_interval=$((check_again_at - now)) + fi + if [ "$now" -lt "$check_again_at" ]; then + continue + fi + fi - stream_key_binding_activations | while read -t "$SLEEP_INTERVAL" event; do if pgrep -q wf-recorder; then local is_running=1 else @@ -20,14 +40,15 @@ function main { previous_reported="$is_running" report "$is_running" fi - done - while true; do - sleep $SLEEP_INTERVAL + now=$(date +%s) + check_again_at=$((now + SLEEP_INTERVAL)) + next_sleep_interval=$SLEEP_INTERVAL done } function stream_key_binding_activations { + echo "{}" swaymsg --raw --monitor --type subscribe '[ "binding" ]' }