From 5ac2cd94e095750885df2c32b167e00c6bd20673 Mon Sep 17 00:00:00 2001 From: joris Date: Fri, 20 Jul 2012 12:50:05 +0200 Subject: [PATCH] Start protablility infrastructure. An OS variable is store at sourcing. Non portable functions are called with __function_$OS. This way you don't have to call "uname" at each prompt. Currently ported functions : - __load | Linux, FreeBSD | detect current system load - __cpunum | Linux, FreeBSD | number of cpu --- liquidprompt.bash | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/liquidprompt.bash b/liquidprompt.bash index cad36bd..e4609db 100644 --- a/liquidprompt.bash +++ b/liquidprompt.bash @@ -73,8 +73,43 @@ LIGHT_CYAN="\[$(tput bold ; tput setaf 6)\]" NO_COL="\[$(tput sgr0)\]" +case $(uname) in + "Linux" ) OS="Linux" ;; + "FreeBSD") OS="FreeBSD" ;; +esac -__CPUNUM=$(grep ^processor /proc/cpuinfo | wc -l) + +############### +# OS specific # +############### + +# get cpu number +__cpunum_Linux () +{ + grep ^processor /proc/cpuinfo | wc -l +} + +__cpunum_FreeBSD () +{ + sysctl -n hw.ncpu +} + +__CPUNUM=$(__cpunum_$OS) + + +# get current load + +__load_Linux() +{ + load=$(awk '{print $1}' /proc/loadavg) + echo -n "$load" +} + +__load_FreeBSD() +{ + load=$(LANG=C sysctl -n vm.loadavg | awk '{print $2}') + echo -n "$load" +} ############### @@ -363,13 +398,6 @@ __battery_color() # System load # ############### -# Get the load average -__load() -{ - load=$(awk '{print $1}' /proc/loadavg) - echo -n "$load" -} - # Compute a gradient of background/forground colors depending on the battery status __load_color() { @@ -380,7 +408,7 @@ __load_color() # Then we have to choose the values at which the colours switch, with # anything past yellow being pretty important. - loadval=$(__load) + loadval=$(__load_$OS) load=$(echo $loadval | sed -E 's/(^0| .*|\.)//g;s/^0*//g' ) if [ -z "$load" ]; then load=0