From 8a3525aef69d56efd59e196eda49f1db54d89f20 Mon Sep 17 00:00:00 2001 From: M1dgard Date: Thu, 5 Apr 2018 19:00:35 +0200 Subject: [PATCH] Change info-hackspeed Polybar script and config --- polybar/config.ini | 2 +- polybar/info-hackspeed.sh | 66 ++++++++++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/polybar/config.ini b/polybar/config.ini index e663541..7c7c0f7 100644 --- a/polybar/config.ini +++ b/polybar/config.ini @@ -392,7 +392,7 @@ menu-2-1-exec = menu-open-0 [module/info-hackspeed] 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 [settings] diff --git a/polybar/info-hackspeed.sh b/polybar/info-hackspeed.sh index e721afb..07b564f 100755 --- a/polybar/info-hackspeed.sh +++ b/polybar/info-hackspeed.sh @@ -1,19 +1,51 @@ #!/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)' +if [ -z "$KEYBOARD_ID" ]; then + KEYBOARD_ID="AT Translated Set 2 keyboard" +fi # cpm: characters per minute # 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 - wpm) factor=3; ;; - cpm) factor=1; ;; + wpm) divide_by=$((divide_by * 5)); ;; + cpm) ;; *) echo "Unsupported metric \"$METRIC\""; exit 1; ;; esac @@ -22,15 +54,27 @@ 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" | \ +xinput test "$KEYBOARD_ID" | \ stdbuf -o0 awk '$1 == "key" && $2 == "press" && ('"$CONDITION"') {printf "."}' >> "$hackspeed_cache" & 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") - value=$(($lines * $factor / 5)) + # Truncate the cache file so that in the next iteration, we only count new + # keypresses 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 + +# vim: set noet :