Add average choice to load extension

This commit adds a functionality to choose if one wants to see 1, 5 or
15 minutes average.

Resolves issue #604
This commit is contained in:
Tadek Teleżyński 2017-08-17 21:54:26 +02:00
parent 9f4faf7f21
commit 582edf200c
2 changed files with 29 additions and 7 deletions

View file

@ -96,7 +96,7 @@ The segments that are currently available are:
* [`ip`](#ip) - Shows the current IP address. * [`ip`](#ip) - Shows the current IP address.
* [`vpn`](#vpn) - Shows the current VPN IP address. * [`vpn`](#vpn) - Shows the current VPN IP address.
* [`public_ip`](#public_ip) - Shows your public IP address. * [`public_ip`](#public_ip) - Shows your public IP address.
* `load` - Your machine's load averages. * [`load`](#load) - Your machine's load averages.
* `os_icon` - Display a nice little icon, depending on your operating system. * `os_icon` - Display a nice little icon, depending on your operating system.
* `ram` - Show free RAM. * `ram` - Show free RAM.
* `root_indicator` - An indicator if the user has superuser status. * `root_indicator` - An indicator if the user has superuser status.
@ -482,6 +482,17 @@ segment will not be displayed.
|`POWERLEVEL9K_PUBLIC_IP_METHODS`|(dig curl wget)| These methods in that order are used to refresh your IP.| |`POWERLEVEL9K_PUBLIC_IP_METHODS`|(dig curl wget)| These methods in that order are used to refresh your IP.|
|`POWERLEVEL9K_PUBLIC_IP_NONE`|None|The string displayed when an IP was not obtained| |`POWERLEVEL9K_PUBLIC_IP_NONE`|None|The string displayed when an IP was not obtained|
##### load
Displays one of your load averages with appropriate state coloring. The thresholds are:
- `0.7 * NUM_CORES <`: critical
- `0.5 * NUM_CORES <`: warning
- `less`: normal
| Variable | Default Value | Description |
|----------|---------------|-------------|
|`POWERLEVEL9K_LOAD_WHICH`|5|Which average to show. Possible values: 1, 5 or 15|
##### newline ##### newline
Puts a newline in your prompt so you can continue using segments on the next Puts a newline in your prompt so you can continue using segments on the next

View file

@ -937,6 +937,8 @@ prompt_load() {
local current_state="unknown" local current_state="unknown"
local cores local cores
set_default POWERLEVEL9K_LOAD_WHICH 5
typeset -AH load_states typeset -AH load_states
load_states=( load_states=(
'critical' 'red' 'critical' 'red'
@ -945,31 +947,40 @@ prompt_load() {
) )
if [[ "$OS" == "OSX" ]] || [[ "$OS" == "BSD" ]]; then if [[ "$OS" == "OSX" ]] || [[ "$OS" == "BSD" ]]; then
load_avg_1min=$(sysctl vm.loadavg | grep -o -E '[0-9]+(\.|,)[0-9]+' | head -n 1)
if [[ "$POWERLEVEL9K_LOAD_WHICH" -eq 1 ]]; then
load_avg=$(sysctl vm.loadavg | grep -o -E '[0-9]+(\.|,)[0-9]+' | head -n 3 | sed -n 1p)
elif [[ "$POWERLEVEL9K_LOAD_WHICH" -eq 5 ]]; then
load_avg=$(sysctl vm.loadavg | grep -o -E '[0-9]+(\.|,)[0-9]+' | head -n 3 | sed -n 2p)
else
load_avg=$(sysctl vm.loadavg | grep -o -E '[0-9]+(\.|,)[0-9]+' | head -n 3 | sed -n 3p)
fi
if [[ "$OS" == "OSX" ]]; then if [[ "$OS" == "OSX" ]]; then
cores=$(sysctl -n hw.logicalcpu) cores=$(sysctl -n hw.logicalcpu)
else else
cores=$(sysctl -n hw.ncpu) cores=$(sysctl -n hw.ncpu)
fi fi
else else
load_avg_1min=$(grep -o "[0-9.]*" /proc/loadavg | head -n 1) load_avg=$(grep -o "[0-9.]*" /proc/loadavg | head -n 1)
cores=$(nproc) cores=$(nproc)
fi fi
# Replace comma # Replace comma
load_avg_1min=${load_avg_1min//,/.} load_avg=${load_avg//,/.}
if [[ "$load_avg_1min" -gt $(bc -l <<< "${cores} * 0.7") ]]; then if [[ "$load_avg" -gt $(bc -l <<< "${cores} * 0.7") ]]; then
current_state="critical" current_state="critical"
elif [[ "$load_avg_1min" -gt $(bc -l <<< "${cores} * 0.5") ]]; then elif [[ "$load_avg" -gt $(bc -l <<< "${cores} * 0.5") ]]; then
current_state="warning" current_state="warning"
else else
current_state="normal" current_state="normal"
fi fi
"$1_prompt_segment" "${0}_${current_state}" "$2" "${load_states[$current_state]}" "$DEFAULT_COLOR" "$load_avg_1min" 'LOAD_ICON' "$1_prompt_segment" "${0}_${current_state}" "$2" "${load_states[$current_state]}" "$DEFAULT_COLOR" "$load_avg" 'LOAD_ICON'
} }
# Node version # Node version
prompt_node_version() { prompt_node_version() {
local node_version=$(node -v 2>/dev/null) local node_version=$(node -v 2>/dev/null)