speed up ram and swap prompts
This commit is contained in:
parent
3e3da88dbc
commit
b92d510882
|
@ -45,34 +45,18 @@ function _p9k_g_expand() {
|
||||||
typeset -g $1=${(g::)${(P)1}}
|
typeset -g $1=${(g::)${(P)1}}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Converts large memory values into a human-readable unit (e.g., bytes --> GB)
|
typeset -g _P9K_BYTE_SUFFIX=('B' 'K' 'M' 'G' 'T' 'P' 'E' 'Z' 'Y')
|
||||||
# Takes two arguments:
|
|
||||||
# * $size - The number which should be prettified
|
|
||||||
# * $base - The base of the number (default Bytes)
|
|
||||||
printSizeHumanReadable() {
|
|
||||||
typeset -F 2 size
|
|
||||||
size="$1"+0.00001
|
|
||||||
local extension
|
|
||||||
extension=('B' 'K' 'M' 'G' 'T' 'P' 'E' 'Z' 'Y')
|
|
||||||
local index=1
|
|
||||||
|
|
||||||
# if the base is not Bytes
|
# 42 => 42B
|
||||||
if [[ -n $2 ]]; then
|
# 1536 => 1.5K
|
||||||
local idx
|
function _p9k_human_readable_bytes() {
|
||||||
for idx in "${extension[@]}"; do
|
typeset -F 2 n=$1
|
||||||
if [[ "$2" == "$idx" ]]; then
|
local suf
|
||||||
break
|
for suf in $_P9K_BYTE_SUFFIX; do
|
||||||
fi
|
(( n < 100 )) && break
|
||||||
index=$(( index + 1 ))
|
(( n /= 1024 ))
|
||||||
done
|
done
|
||||||
fi
|
_P9K_RETVAL=$n$suf
|
||||||
|
|
||||||
while (( (size / 1024) > 0.1 )); do
|
|
||||||
size=$(( size / 1024 ))
|
|
||||||
index=$(( index + 1 ))
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "$size${extension[$index]}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Determine if the passed segment is used in the prompt
|
# Determine if the passed segment is used in the prompt
|
||||||
|
|
|
@ -1279,26 +1279,30 @@ prompt_php_version() {
|
||||||
################################################################
|
################################################################
|
||||||
# Segment to display free RAM and used Swap
|
# Segment to display free RAM and used Swap
|
||||||
prompt_ram() {
|
prompt_ram() {
|
||||||
local ROOT_PREFIX="${4}"
|
local -F free_bytes
|
||||||
local base=''
|
|
||||||
local ramfree=0
|
|
||||||
if [[ "$OS" == "OSX" ]]; then
|
|
||||||
# Available = Free + Inactive
|
|
||||||
# See https://support.apple.com/en-us/HT201538
|
|
||||||
ramfree=$(vm_stat | grep "Pages free" | grep -o -E '[0-9]+')
|
|
||||||
ramfree=$((ramfree + $(vm_stat | grep "Pages inactive" | grep -o -E '[0-9]+')))
|
|
||||||
# Convert pages into Bytes
|
|
||||||
ramfree=$(( ramfree * 4096 ))
|
|
||||||
else
|
|
||||||
if [[ "$OS" == "BSD" ]]; then
|
|
||||||
ramfree=$(grep 'avail memory' ${ROOT_PREFIX}/var/run/dmesg.boot | awk '{print $4}')
|
|
||||||
else
|
|
||||||
ramfree=$(grep -o -E "MemAvailable:\s+[0-9]+" ${ROOT_PREFIX}/proc/meminfo | grep -o -E "[0-9]+")
|
|
||||||
base='K'
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
"$1_prompt_segment" "$0" "$2" "yellow" "$DEFAULT_COLOR" 'RAM_ICON' 0 '' "$(printSizeHumanReadable "$ramfree" $base)"
|
case $OS in
|
||||||
|
OSX)
|
||||||
|
(( $+commands[vm_stat] )) || return
|
||||||
|
local stat && stat=$(command vm_stat 2>/dev/null) || return
|
||||||
|
[[ $stat =~ 'Pages free:[[:space:]]+([0-9]+)' ]] || return
|
||||||
|
(( free_bytes+=match[1] ))
|
||||||
|
[[ $stat =~ 'Pages inactive:[[:space:]]+([0-9]+)' ]] || return
|
||||||
|
(( free_bytes+=match[1] ))
|
||||||
|
(( free_bytes *= 4096 ))
|
||||||
|
;;
|
||||||
|
BSD)
|
||||||
|
local stat && stat=$(command grep -F 'avail memory' /var/run/dmesg.boot 2>/dev/null) || return
|
||||||
|
free_bytes=${${(A)=stat}[4]}
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
local stat && stat=$(command grep -F MemAvailable /proc/meminfo 2>/dev/null) || return
|
||||||
|
free_bytes=$(( ${${(A)=stat}[2]} * 1024 ))
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
_p9k_human_readable_bytes $free_bytes
|
||||||
|
$1_prompt_segment $0 $2 yellow "$DEFAULT_COLOR" RAM_ICON 0 '' $_P9K_RETVAL
|
||||||
}
|
}
|
||||||
|
|
||||||
function _p9k_read_rbenv_version_file() {
|
function _p9k_read_rbenv_version_file() {
|
||||||
|
@ -1505,28 +1509,30 @@ prompt_status() {
|
||||||
################################################################
|
################################################################
|
||||||
# Segment to display Swap information
|
# Segment to display Swap information
|
||||||
prompt_swap() {
|
prompt_swap() {
|
||||||
local ROOT_PREFIX="${4}"
|
local -F used_bytes
|
||||||
local swap_used=0
|
|
||||||
local base=''
|
|
||||||
|
|
||||||
if [[ "$OS" == "OSX" ]]; then
|
if [[ "$OS" == "OSX" ]]; then
|
||||||
local raw_swap_used
|
(( $+commands[sysctl] )) || return
|
||||||
raw_swap_used=$(sysctl vm.swapusage | grep -o "used\s*=\s*[0-9,.A-Z]*" | grep -o "[0-9,.A-Z]*$")
|
[[ "$(sysctl vm.swapusage)" =~ "used = ([0-9,.]+)([A-Z]+)" ]] || return
|
||||||
|
used_bytes=${match[1]//,/.}
|
||||||
typeset -F 2 swap_used
|
case ${match[2]} in
|
||||||
swap_used=${$(echo $raw_swap_used | grep -o "[0-9,.]*")//,/.}
|
K) (( used_bytes *= 1024 ));;
|
||||||
# Replace comma
|
M) (( used_bytes *= 1048576 ));;
|
||||||
swap_used=${swap_used//,/.}
|
G) (( used_bytes *= 1073741824 ));;
|
||||||
|
T) (( used_bytes *= 1099511627776 ));;
|
||||||
base=$(echo "$raw_swap_used" | grep -o "[A-Z]*$")
|
*) return;;
|
||||||
|
esac
|
||||||
else
|
else
|
||||||
swap_total=$(grep -o -E "SwapTotal:\s+[0-9]+" ${ROOT_PREFIX}/proc/meminfo | grep -o -E "[0-9]+")
|
local meminfo && meminfo=$(command grep -F 'Swap' /proc/meminfo) || return
|
||||||
swap_free=$(grep -o -E "SwapFree:\s+[0-9]+" ${ROOT_PREFIX}/proc/meminfo | grep -o -E "[0-9]+")
|
[[ $meminfo =~ 'SwapTotal:[[:space:]]+([0-9]+)' ]] || return
|
||||||
swap_used=$(( swap_total - swap_free ))
|
(( used_bytes+=match[1] ))
|
||||||
base='K'
|
[[ $meminfo =~ 'SwapFree:[[:space:]]+([0-9]+)' ]] || return
|
||||||
|
(( used_bytes-=match[1] ))
|
||||||
|
(( used_bytes *= 1024 ))
|
||||||
fi
|
fi
|
||||||
|
|
||||||
"$1_prompt_segment" "$0" "$2" "yellow" "$DEFAULT_COLOR" 'SWAP_ICON' 0 '' "$(printSizeHumanReadable "$swap_used" $base)"
|
_p9k_human_readable_bytes $used_bytes
|
||||||
|
$1_prompt_segment $0 $2 yellow "$DEFAULT_COLOR" SWAP_ICON 0 '' $_P9K_RETVAL
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################
|
################################################################
|
||||||
|
|
Loading…
Reference in New Issue