mirror of
https://github.com/romkatv/powerlevel10k.git
synced 2024-11-18 02:30:07 +00:00
137 lines
3.7 KiB
Bash
Executable file
137 lines
3.7 KiB
Bash
Executable file
# _p9k_declare <type> <uppercase-name> [default]...
|
|
function _p9k_declare() {
|
|
local -i set=$+parameters[$2]
|
|
(( ARGC > 2 || set )) || return 0
|
|
case $1 in
|
|
-b)
|
|
if (( set )); then
|
|
[[ ${(P)2} == true ]] && typeset -gi _$2=1 || typeset -gi _$2=0
|
|
else
|
|
typeset -gi _$2=$3
|
|
fi
|
|
;;
|
|
-a)
|
|
local -a v=(${(P)2})
|
|
if (( set )); then
|
|
eval "typeset -ga _${(q)2}=(${(@qq)v})";
|
|
else
|
|
if [[ $3 != '--' ]]; then
|
|
echo "internal error in _p9k_declare " "${(qqq)@}" >&2
|
|
fi
|
|
eval "typeset -ga _${(q)2}=(${(@qq)*[4,-1]})"
|
|
fi
|
|
;;
|
|
-i)
|
|
(( set )) && typeset -gi _$2=$2 || typeset -gi _$2=$3
|
|
;;
|
|
-F)
|
|
(( set )) && typeset -gF _$2=$2 || typeset -gF _$2=$3
|
|
;;
|
|
-s)
|
|
(( set )) && typeset -g _$2=${(P)2} || typeset -g _$2=$3
|
|
;;
|
|
-e)
|
|
if (( set )); then
|
|
local v=${(P)2}
|
|
typeset -g _$2=${(g::)v}
|
|
else
|
|
typeset -g _$2=${(g::)3}
|
|
fi
|
|
;;
|
|
*)
|
|
echo "internal error in _p9k_declare " "${(qqq)@}" >&2
|
|
esac
|
|
}
|
|
|
|
# If we execute `print -P $1`, how many characters will be printed on the last line?
|
|
# Assumes that `%{%}` and `%G` don't lie.
|
|
#
|
|
# _p9k_prompt_length '' => 0
|
|
# _p9k_prompt_length 'abc' => 3
|
|
# _p9k_prompt_length $'abc\nxy' => 2
|
|
# _p9k_prompt_length $'\t' => 8
|
|
# _p9k_prompt_length '%F{red}abc' => 3
|
|
# _p9k_prompt_length $'%{a\b%Gb%}' => 1
|
|
function _p9k_prompt_length() {
|
|
emulate -L zsh
|
|
local COLUMNS=1024
|
|
local -i x y=$#1 m
|
|
if (( y )); then
|
|
while (( ${${(%):-$1%$y(l.1.0)}[-1]} )); do
|
|
x=y
|
|
(( y *= 2 ));
|
|
done
|
|
local xy
|
|
while (( y > x + 1 )); do
|
|
m=$(( x + (y - x) / 2 ))
|
|
typeset ${${(%):-$1%$m(l.x.y)}[-1]}=$m
|
|
done
|
|
fi
|
|
_p9k_ret=$x
|
|
}
|
|
|
|
typeset -gr __p9k_byte_suffix=('B' 'K' 'M' 'G' 'T' 'P' 'E' 'Z' 'Y')
|
|
|
|
# 42 => 42B
|
|
# 1536 => 1.5K
|
|
function _p9k_human_readable_bytes() {
|
|
typeset -F 2 n=$1
|
|
local suf
|
|
for suf in $__p9k_byte_suffix; do
|
|
(( n < 100 )) && break
|
|
(( n /= 1024 ))
|
|
done
|
|
_p9k_ret=$n$suf
|
|
}
|
|
|
|
# Determine if the passed segment is used in the prompt
|
|
#
|
|
# Pass the name of the segment to this function to test for its presence in
|
|
# either the LEFT or RIGHT prompt arrays.
|
|
# * $1: The segment to be tested.
|
|
segment_in_use() {
|
|
(( $_POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[(I)$1(|_joined)] ||
|
|
$_POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(I)$1(|_joined)] ))
|
|
}
|
|
|
|
function _p9k_parse_ip() {
|
|
local desiredInterface=${1:-'^[^ ]+'}
|
|
|
|
if [[ $_p9k_os == OSX ]]; then
|
|
[[ -x /sbin/ifconfig ]] || return
|
|
local rawInterfaces && rawInterfaces="$(/sbin/ifconfig -l 2>/dev/null)" || return
|
|
local -a interfaces=(${(A)=rawInterfaces})
|
|
local pattern="${desiredInterface}[^ ]?"
|
|
local -a relevantInterfaces
|
|
for rawInterface in $interfaces; do
|
|
[[ "$rawInterface" =~ $pattern ]] && relevantInterfaces+=$MATCH
|
|
done
|
|
local newline=$'\n'
|
|
local interfaceName interface
|
|
for interfaceName in $relevantInterfaces; do
|
|
interface="$(/sbin/ifconfig $interfaceName 2>/dev/null)" || continue
|
|
[[ "${interface}" =~ "lo[0-9]*" ]] && continue
|
|
if [[ "${interface//${newline}/}" =~ "<([^>]*)>(.*)inet[ ]+([^ ]*)" ]]; then
|
|
local ipFound="${match[3]}"
|
|
local -a interfaceStates=(${(s:,:)match[1]})
|
|
if (( ${interfaceStates[(I)UP]} )); then
|
|
_p9k_ret=$ipFound
|
|
return
|
|
fi
|
|
fi
|
|
done
|
|
else
|
|
[[ -x /sbin/ip ]] || return
|
|
local -a interfaces=( "${(f)$(/sbin/ip -brief -4 a show 2>/dev/null)}" )
|
|
local pattern="^${desiredInterface}[[:space:]]+UP[[:space:]]+([^/ ]+)"
|
|
local interface
|
|
for interface in "${(@)interfaces}"; do
|
|
if [[ "$interface" =~ $pattern ]]; then
|
|
_p9k_ret=$match[1]
|
|
return
|
|
fi
|
|
done
|
|
fi
|
|
|
|
return 1
|
|
}
|