aboutsummaryrefslogtreecommitdiff
path: root/Common/rofi/bin/usedcpu
diff options
context:
space:
mode:
authorjakobst1n <jakob.stendahl@outlook.com>2021-04-28 14:36:31 +0200
committerjakobst1n <jakob.stendahl@outlook.com>2021-04-28 14:36:31 +0200
commit82e7ae32867c9f46576d99fdc78bc0185410fe47 (patch)
treefd8764e91741f90967801e0e254aafeeb847d031 /Common/rofi/bin/usedcpu
parent045c955f835fdb11983117ca6e27aa4543de4109 (diff)
downloaddotfiles-82e7ae32867c9f46576d99fdc78bc0185410fe47.tar.gz
dotfiles-82e7ae32867c9f46576d99fdc78bc0185410fe47.zip
Do some mods and add i3 things
Diffstat (limited to 'Common/rofi/bin/usedcpu')
-rwxr-xr-xCommon/rofi/bin/usedcpu45
1 files changed, 45 insertions, 0 deletions
diff --git a/Common/rofi/bin/usedcpu b/Common/rofi/bin/usedcpu
new file mode 100755
index 0000000..512933c
--- /dev/null
+++ b/Common/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}"