2019-07-23 13:47:25 +00:00
|
|
|
# _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-03-18 16:24:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
2019-07-23 13:47:25 +00:00
|
|
|
_p9k_ret=$x
|
2019-05-27 10:45:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:47:25 +00:00
|
|
|
typeset -gr __p9k_byte_suffix=('B' 'K' 'M' 'G' 'T' 'P' 'E' 'Z' 'Y')
|
2015-10-19 15:37:18 +00:00
|
|
|
|
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
|
2019-07-23 13:47:25 +00:00
|
|
|
for suf in $__p9k_byte_suffix; do
|
2019-05-22 17:14:57 +00:00
|
|
|
(( n < 100 )) && break
|
|
|
|
(( n /= 1024 ))
|
2015-10-19 15:37:18 +00:00
|
|
|
done
|
2019-07-23 13:47:25 +00:00
|
|
|
_p9k_ret=$n$suf
|
2015-10-19 15:37:18 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 19:40:05 +00:00
|
|
|
# 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)] ))
|
2016-01-22 18:13:07 +00:00
|
|
|
}
|
2016-03-17 22:00:20 +00:00
|
|
|
|
2019-05-22 22:00:12 +00:00
|
|
|
function _p9k_parse_ip() {
|
|
|
|
local desiredInterface=${1:-'^[^ ]+'}
|
2019-02-03 18:20:14 +00:00
|
|
|
|
2019-05-22 22:00:12 +00:00
|
|
|
if [[ $OS == OSX ]]; then
|
|
|
|
[[ -x /sbin/ifconfig ]] || return
|
|
|
|
local rawInterfaces && rawInterfaces="$(/sbin/ifconfig -l 2>/dev/null)" || return
|
|
|
|
local -a interfaces=(${(A)=rawInterfaces})
|
2019-02-03 18:20:14 +00:00
|
|
|
local pattern="${desiredInterface}[^ ]?"
|
|
|
|
local -a relevantInterfaces
|
|
|
|
for rawInterface in $interfaces; do
|
2019-05-22 22:00:12 +00:00
|
|
|
[[ "$rawInterface" =~ $pattern ]] && relevantInterfaces+=$MATCH
|
2019-02-03 18:20:14 +00:00
|
|
|
done
|
|
|
|
local newline=$'\n'
|
2019-05-22 22:00:12 +00:00
|
|
|
local interfaceName interface
|
2019-02-03 18:20:14 +00:00
|
|
|
for interfaceName in $relevantInterfaces; do
|
2019-05-22 22:00:12 +00:00
|
|
|
interface="$(/sbin/ifconfig $interfaceName 2>/dev/null)" || continue
|
|
|
|
[[ "${interface}" =~ "lo[0-9]*" ]] && continue
|
2019-02-22 17:16:33 +00:00
|
|
|
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
|
2019-07-23 13:47:25 +00:00
|
|
|
_p9k_ret=$ipFound
|
2019-05-22 22:00:12 +00:00
|
|
|
return
|
2019-02-21 17:32:17 +00:00
|
|
|
fi
|
2019-02-03 18:20:14 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
2019-05-22 22:00:12 +00:00
|
|
|
[[ -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
|
2019-02-03 18:20:14 +00:00
|
|
|
for interface in "${(@)interfaces}"; do
|
|
|
|
if [[ "$interface" =~ $pattern ]]; then
|
2019-07-23 13:47:25 +00:00
|
|
|
_p9k_ret=$match[1]
|
2019-05-22 22:00:12 +00:00
|
|
|
return
|
2019-02-03 18:20:14 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|