Change info-hackspeed Polybar script and config

This commit is contained in:
M1dgard 2018-04-05 19:00:35 +02:00
parent 2ac8db48bc
commit 8a3525aef6
Signed by untrusted user who does not match committer: midgard
GPG key ID: 511C112F1331BBB4
2 changed files with 56 additions and 12 deletions

View file

@ -392,7 +392,7 @@ menu-2-1-exec = menu-open-0
[module/info-hackspeed] [module/info-hackspeed]
type = custom/script type = custom/script
exec = ~/.config/polybar/info-hackspeed.sh exec = LAYOUT=azerty ICON= METRIC=wpm ~/git/realprojects/polybar-scripts/polybar-scripts/info-hackspeed/info-hackspeed.sh
tail = true tail = true
[settings] [settings]

View file

@ -1,19 +1,51 @@
#!/bin/sh #!/bin/sh
# Only count keys that satisfy this condition. $3 is the key index if [ -z "$KEYBOARD_ID" ]; then
# Use `xinput test "AT Translated Set 2 keyboard"` to find out key indices KEYBOARD_ID="AT Translated Set 2 keyboard"
CONDITION='($3 >= 24 && $3 <= 33) || ($3 >= 37 && $3 <= 54) || ($3 >= 52 && $3 <= 57)' fi
# cpm: characters per minute # cpm: characters per minute
# wpm: words per minute (1 word = 5 characters) # wpm: words per minute (1 word = 5 characters)
METRIC=wpm if [ -z "$METRIC" ]; then
METRIC=cpm
fi
FORMAT=" %d $METRIC\n" if [ -z "$ICON" ]; then
ICON="#"
fi
if [ -z "$FORMAT" ]; then
FORMAT="$ICON %d $METRIC"
fi
if [ -z "$INTERVAL" ]; then
INTERVAL=20
fi
# If you have a keyboard layout that is not listed here yet, create a condition yourself. $3 is the key index.
# Use `xinput test "AT Translated Set 2 keyboard"` to see key codes in real time.
# Be sure to open a pull request for your layout's condition!
if [ -z "$LAYOUT" ]; then
LAYOUT=qwerty
fi
if [ -z "$CONDITION" ]; then
case "$LAYOUT" in
qwerty) CONDITION='($3 >= 10 && $3 <= 19) || ($3 >= 24 && $3 <= 33) || ($3 >= 37 && $3 <= 53) || ($3 >= 52 && $3 <= 58)'; ;;
azerty) CONDITION='($3 >= 10 && $3 <= 19) || ($3 >= 24 && $3 <= 33) || ($3 >= 37 && $3 <= 54) || ($3 >= 52 && $3 <= 57)'; ;;
dontcare) CONDITION='$3 == $3'; ;; # Just register all key presses, not only letters and numbers
*) echo "Unsupported layout \"$LAYOUT\""; exit 1; ;;
esac
fi
# We have to account for the fact we're not listening a whole minute
multiply_by=60
divide_by=$INTERVAL
case "$METRIC" in case "$METRIC" in
wpm) factor=3; ;; wpm) divide_by=$((divide_by * 5)); ;;
cpm) factor=1; ;; cpm) ;;
*) echo "Unsupported metric \"$METRIC\""; exit 1; ;; *) echo "Unsupported metric \"$METRIC\""; exit 1; ;;
esac esac
@ -22,15 +54,27 @@ trap 'rm "$hackspeed_cache"' EXIT
# Write a dot to our cache for each key press # Write a dot to our cache for each key press
printf '' > "$hackspeed_cache" printf '' > "$hackspeed_cache"
xinput test "AT Translated Set 2 keyboard" | \ xinput test "$KEYBOARD_ID" | \
stdbuf -o0 awk '$1 == "key" && $2 == "press" && ('"$CONDITION"') {printf "."}' >> "$hackspeed_cache" & stdbuf -o0 awk '$1 == "key" && $2 == "press" && ('"$CONDITION"') {printf "."}' >> "$hackspeed_cache" &
while true; do while true; do
# Ask the kernel how big the file is with the command `stat`. The number we
# get is the file size in bytes, which equals the amount of dots the file
# contains, and hence how much keys were pressed since the file was last
# cleared.
lines=$(stat --format %s "$hackspeed_cache") lines=$(stat --format %s "$hackspeed_cache")
value=$(($lines * $factor / 5))
# Truncate the cache file so that in the next iteration, we only count new
# keypresses
printf '' > "$hackspeed_cache" printf '' > "$hackspeed_cache"
printf "$FORMAT" "$value"
sleep 20 # The shell only does integer operations, so make sure to first multiply and
# then divide
value=$(($lines * $multiply_by / $divide_by))
printf "$FORMAT\n" "$value"
sleep $INTERVAL
done done
# vim: set noet :