diff --git a/polybar/config.ini b/polybar/config.ini index 25106b0..d726f81 100644 --- a/polybar/config.ini +++ b/polybar/config.ini @@ -390,6 +390,11 @@ menu-2-0-exec = sudo poweroff menu-2-1 = cancel menu-2-1-exec = menu-open-0 +[module/info-hackspeed] +type = custom/script +exec = ~/.config/polybar/info-hackspeed.sh +tail = true + [settings] screenchange-reload = true ;compositing-background = xor diff --git a/polybar/info-hackspeed.sh b/polybar/info-hackspeed.sh new file mode 100755 index 0000000..e721afb --- /dev/null +++ b/polybar/info-hackspeed.sh @@ -0,0 +1,36 @@ +#!/bin/sh + +# Only count keys that satisfy this condition. $3 is the key index +# Use `xinput test "AT Translated Set 2 keyboard"` to find out key indices +CONDITION='($3 >= 24 && $3 <= 33) || ($3 >= 37 && $3 <= 54) || ($3 >= 52 && $3 <= 57)' + +# cpm: characters per minute +# wpm: words per minute (1 word = 5 characters) +METRIC=wpm + +FORMAT=" %d $METRIC\n" + + +case "$METRIC" in + wpm) factor=3; ;; + cpm) factor=1; ;; + *) echo "Unsupported metric \"$METRIC\""; exit 1; ;; +esac + +hackspeed_cache="$(mktemp -p '' hackspeed_cache.XXXXX)" +trap 'rm "$hackspeed_cache"' EXIT + +# Write a dot to our cache for each key press +printf '' > "$hackspeed_cache" +xinput test "AT Translated Set 2 keyboard" | \ + stdbuf -o0 awk '$1 == "key" && $2 == "press" && ('"$CONDITION"') {printf "."}' >> "$hackspeed_cache" & + +while true; do + lines=$(stat --format %s "$hackspeed_cache") + value=$(($lines * $factor / 5)) + + printf '' > "$hackspeed_cache" + printf "$FORMAT" "$value" + + sleep 20 +done