Merge pull request #98 from iagotito/normalize-cpu-usage-width
Create function to normalize cpu usage width
This commit is contained in:
commit
f508a8907f
1 changed files with 14 additions and 2 deletions
|
@ -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*)
|
||||
|
|
Loading…
Reference in a new issue