Merge pull request #98 from iagotito/normalize-cpu-usage-width

Create function to normalize cpu usage width
This commit is contained in:
Ethan Edwards 2021-04-03 10:20:36 -04:00 committed by GitHub
commit f508a8907f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,12 +14,24 @@ get_tmux_option() {
fi
}
# normalize the percentage string to always have a length of 5
normalize_percent_len() {
# the max length that the percent can reach, which happens for a two digit number with a decimal house: "99.9%"
max_len=5
percent_len=${#1}
let diff_len=$max_len-$percent_len
# if the diff_len is even, left will have 1 more space than right
let left_spaces=($diff_len+1)/2
let right_spaces=($diff_len)/2
printf "%${left_spaces}s%s%${right_spaces}s\n" "" $1 ""
}
get_percent()
{
case $(uname -s) in
Linux)
percent=$(LC_NUMERIC=en_US.UTF-8 top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
echo $percent
normalize_percent_len $percent
;;
Darwin)
@ -27,7 +39,7 @@ get_percent()
cpucores=$(sysctl -n hw.logicalcpu)
cpuusage=$(( cpuvalue / cpucores ))
percent="$cpuusage%"
echo $percent
normalize_percent_len $percent
;;
CYGWIN*|MINGW32*|MSYS*|MINGW*)