Refactor CPU load

Use a single function name for collecting loadavg. Reduces pollution
(issue #28).
Refactor _lp_load_color: less code but same features.
This commit is contained in:
Olivier Mengué 2012-08-15 00:11:34 +02:00
parent 7f5346f953
commit 2a833475bc

View File

@ -153,27 +153,26 @@ esac
# get current load # get current load
case "$LP_OS" in
_lp_load_Linux() Linux)
{ _lp_cpu_load () {
local load local load eol
load=$(awk '{print $1}' /proc/loadavg) read load eol < /proc/loadavg
echo -n "$load" echo "$load"
} }
;;
_lp_load_FreeBSD() FreeBSD)
{ _lp_cpu_load () {
local load local bol load eol
load=$(LANG=C sysctl -n vm.loadavg | awk '{print $2}') read bol load eol < $<( LANG=C sysctl -n vm.loadavg )
echo -n "$load" echo "$load"
} }
;;
_lp_load_SunOS() SunOS)
{ _lp_cpu_load () {
local load LANG=C uptime | awk '{print substr($10,0,length($10))}'
load=$(LANG=C uptime | awk '{print substr($10,0,length($10))}') }
echo -n "$load" esac
}
################# #################
@ -787,14 +786,9 @@ _lp_load_color()
# Then we have to choose the values at which the colours switch, with # Then we have to choose the values at which the colours switch, with
# anything past yellow being pretty important. # anything past yellow being pretty important.
local loadval
local load local load
loadval=$(_lp_load_$LP_OS) load="$(_lp_cpu_load | sed 's/\.//g;s/^0*//g' )"
load=$(echo $loadval | sed 's/\.//g;s/^0*//g' ) let "load=${load:-0}/$_lp_CPUNUM"
if [[ -z "$load" ]] ; then
load=0
fi
let "load=$load/$_lp_CPUNUM"
if [[ $load -ge $LP_LOAD_THRESHOLD ]] if [[ $load -ge $LP_LOAD_THRESHOLD ]]
then then