From 1dd6c0852a7fc02875c509e08a09db1df4ddde9b Mon Sep 17 00:00:00 2001 From: Rowan Date: Tue, 14 Feb 2023 17:17:05 +0100 Subject: [PATCH 1/2] Add a rolling average to the hackspeed script This PR adds a rolling average to the hackspeed-info script which improves the statistic displayed --- .../info-hackspeed/info-hackspeed.sh | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/polybar-scripts/info-hackspeed/info-hackspeed.sh b/polybar-scripts/info-hackspeed/info-hackspeed.sh index 999c31e6..da16f0ee 100644 --- a/polybar-scripts/info-hackspeed/info-hackspeed.sh +++ b/polybar-scripts/info-hackspeed/info-hackspeed.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # shellcheck disable=SC2016,SC2059 KEYBOARD_ID="AT Translated Set 2 keyboard" @@ -6,9 +6,12 @@ KEYBOARD_ID="AT Translated Set 2 keyboard" # cpm: characters per minute # wpm: words per minute (1 word = 5 characters) METRIC=cpm -FORMAT="# %d $METRIC" +FORMAT="%d $METRIC" -INTERVAL=20 +INTERVAL=1 + +# The amount of INTERVAL which must be 0 to reset the rolling average +RESET_INTERVAL=10 # 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"` @@ -43,6 +46,8 @@ printf '' > "$hackspeed_cache" xinput test "$KEYBOARD_ID" | \ stdbuf -o0 awk '$1 == "key" && $2 == "press" && ('"$CONDITION"') {printf "."}' >> "$hackspeed_cache" & +array=() + 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 @@ -58,7 +63,36 @@ while true; do # then divide value=$((lines * multiply_by / divide_by)) - printf "$FORMAT\\n" "$value" + if [ "${#arr[@]}" -gt "60" ]; then + array=("${array[@]:1}") + fi + + reset="1"; + + for (( i=1; i<=$RESET_INTERVAL; i++ )) + do + if [ "${#array[@]}" -ge "$i" ]; then + if [ "${array[${#array[@]} - $i]}" -ne "0" ]; then + reset="0" + break + fi + fi + done + + if [ "$reset" -eq "1" ]; then + array=() + fi + + array+=($value) + + avg=0 + for n in "${array[@]}" + do + avg=$((avg+n)) + done + avg=$((avg/${#array[@]})) + + printf "$FORMAT\\n" "$avg" sleep $INTERVAL done From 5dff95cc832f63d31657d8a6fa42ba6263561cfc Mon Sep 17 00:00:00 2001 From: Rowan Date: Tue, 14 Feb 2023 18:35:40 +0100 Subject: [PATCH 2/2] Fix array name typo & default format back --- polybar-scripts/info-hackspeed/info-hackspeed.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/polybar-scripts/info-hackspeed/info-hackspeed.sh b/polybar-scripts/info-hackspeed/info-hackspeed.sh index da16f0ee..9f25f73e 100644 --- a/polybar-scripts/info-hackspeed/info-hackspeed.sh +++ b/polybar-scripts/info-hackspeed/info-hackspeed.sh @@ -6,7 +6,7 @@ KEYBOARD_ID="AT Translated Set 2 keyboard" # cpm: characters per minute # wpm: words per minute (1 word = 5 characters) METRIC=cpm -FORMAT="%d $METRIC" +FORMAT="# %d $METRIC" INTERVAL=1 @@ -63,7 +63,7 @@ while true; do # then divide value=$((lines * multiply_by / divide_by)) - if [ "${#arr[@]}" -gt "60" ]; then + if [ "${#array[@]}" -gt "60" ]; then array=("${array[@]:1}") fi