1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-12-20 22:31:57 +00:00
powerlevel10k/functions/utilities.zsh

138 lines
3.7 KiB
Bash
Raw Normal View History

# _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
}
2019-05-27 10:45:01 +00:00
# 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
2019-06-09 08:53:10 +00:00
local COLUMNS=1024
2019-05-27 10:45:01 +00:00
local -i x y=$#1 m
if (( y )); then
2019-05-27 17:11:09 +00:00
while (( ${${(%):-$1%$y(l.1.0)}[-1]} )); do
x=y
(( y *= 2 ));
done
2019-05-27 10:45:01 +00:00
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
2019-05-27 10:45:01 +00:00
}
typeset -gr __p9k_byte_suffix=('B' 'K' 'M' 'G' 'T' 'P' 'E' 'Z' 'Y')
2019-05-22 17:14:57 +00:00
# 42 => 42B
# 1536 => 1.5K
function _p9k_human_readable_bytes() {
typeset -F 2 n=$1
local suf
for suf in $__p9k_byte_suffix; do
2019-05-22 17:14:57 +00:00
(( 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() {
2019-07-23 14:20:59 +00:00
(( $_POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[(I)$1(|_joined)] ||
$_POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(I)$1(|_joined)] ))
}
function _p9k_parse_ip() {
local desiredInterface=${1:-'^[^ ]+'}
if [[ $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
2019-02-21 17:32:17 +00:00
local ipFound="${match[3]}"
local -a interfaceStates=(${(s:,:)match[1]})
2019-05-22 22:02:51 +00:00
if (( ${interfaceStates[(I)UP]} )); then
_p9k_ret=$ipFound
return
2019-02-21 17:32:17 +00:00
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
}