aboutsummaryrefslogtreecommitdiff
path: root/.config/rofi/bin/usedcpu
diff options
context:
space:
mode:
authorAlec Goncharow <algo@cloudflare.com>2022-02-20 11:50:35 -0500
committerAlec Goncharow <algo@cloudflare.com>2022-02-20 11:50:35 -0500
commitf5abca7278f3ac0c7358e6f30db8a15da45a1e3b (patch)
tree96889e1a2cc2367b8461414ae7c94d9050b84890 /.config/rofi/bin/usedcpu
parent3e0c8208ccbead65cd434fe730223a7ab854c377 (diff)
parent72cf398a6292fa56d57e82caa4d21570e5573294 (diff)
Merge branch 'desktop' of github.com:AlecGoncharow/dotfiles into desktop
Diffstat (limited to '.config/rofi/bin/usedcpu')
-rwxr-xr-x.config/rofi/bin/usedcpu45
1 files changed, 45 insertions, 0 deletions
diff --git a/.config/rofi/bin/usedcpu b/.config/rofi/bin/usedcpu
new file mode 100755
index 0000000..512933c
--- /dev/null
+++ b/.config/rofi/bin/usedcpu
@@ -0,0 +1,45 @@
+#!/usr/bin/env bash
+
+PREV_TOTAL=0
+PREV_IDLE=0
+
+cpuFile="/tmp/.cpu"
+
+if [[ -f "${cpuFile}" ]]; then
+ fileCont=$(cat "${cpuFile}")
+ PREV_TOTAL=$(echo "${fileCont}" | head -n 1)
+ PREV_IDLE=$(echo "${fileCont}" | tail -n 1)
+fi
+
+CPU=(`cat /proc/stat | grep '^cpu '`) # Get the total CPU statistics.
+unset CPU[0] # Discard the "cpu" prefix.
+IDLE=${CPU[4]} # Get the idle CPU time.
+
+# Calculate the total CPU time.
+TOTAL=0
+
+for VALUE in "${CPU[@]:0:4}"; do
+ let "TOTAL=$TOTAL+$VALUE"
+done
+
+if [[ "${PREV_TOTAL}" != "" ]] && [[ "${PREV_IDLE}" != "" ]]; then
+ # Calculate the CPU usage since we last checked.
+ let "DIFF_IDLE=$IDLE-$PREV_IDLE"
+ let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
+ let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"
+ if [[ $1 = "-i" ]]; then
+ echo " ${DIFF_USAGE}%"
+ else
+ echo "${DIFF_USAGE}%"
+ fi
+else
+ if [[ $1 = "-i" ]]; then
+ echo " ?"
+ else
+ echo "?"
+ fi
+fi
+
+# Remember the total and idle CPU times for the next check.
+echo "${TOTAL}" > "${cpuFile}"
+echo "${IDLE}" >> "${cpuFile}"