blob: f0263ce646040d95a69043cdb925a8bb960f804b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# _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() {
[[ -n "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[(r)${1}]}" ||
-n "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[(r)${1}_joined]}" ||
-n "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(r)${1}]}" ||
-n "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(r)${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
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
}
|