1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-13 16:40:08 +00:00

Simplify code that checks if a VPN is running

This commit is contained in:
Dominik Ritter 2019-01-23 01:02:42 +01:00
parent 480ade162b
commit 54a985cb11
2 changed files with 10 additions and 13 deletions

View file

@ -600,17 +600,12 @@ prompt_public_ip() {
# Check VPN is on if VPN interface is set # Check VPN is on if VPN interface is set
if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then
if [[ "$OS" == "OSX" ]]; then if [[ "$OS" == "OSX" ]]; then
for vpn_iface in $(${ROOT_PREFIX}/sbin/ifconfig | grep -e "^$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE" | cut -d":" -f1) local interface="$(${ROOT_PREFIX}/sbin/ifconfig $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE)"
do # Check if interface is UP.
icon='VPN_ICON' [[ "$interface" =~ "<UP," ]] && icon='VPN_ICON'
break
done
else else
for vpn_iface in $(${ROOT_PREFIX}/sbin/ip link ls up | grep -o -E ":\s+[a-z0-9]+:" | grep -v "lo" | grep -o -E "[a-z0-9]+" | grep -o -E "^$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE.*") local interface=$(${ROOT_PREFIX}/sbin/ip -brief -4 a show "${POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE}")
do [[ -n "$interface" ]] && icon='VPN_ICON'
icon='VPN_ICON'
break
done
fi fi
fi fi
$1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" "$icon" $1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" "$icon"

View file

@ -36,13 +36,15 @@ function tearDown() {
} }
function fakeIfconfig() { function fakeIfconfig() {
local INTERFACE="${1}"
[[ -z "${INTERFACE}" ]] && INTERFACE="tun0"
# Fake ifconfig # Fake ifconfig
cat > $FOLDER/sbin/ifconfig <<EOF cat > $FOLDER/sbin/ifconfig <<EOF
#!/usr/bin/env zsh #!/usr/bin/env zsh
if [[ "\$#" -gt 0 ]]; then if [[ "\$#" -gt 0 ]]; then
cat <<INNER cat <<INNER
tun1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 ${INTERFACE}: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 1.2.3.4 txqueuelen 1000 (Ethernet) inet 1.2.3.4 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B) RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0 RX errors 0 dropped 0 overruns 0 frame 0
@ -62,7 +64,7 @@ docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
TX packets 0 bytes 0 (0.0 B) TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tun1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 ${INTERFACE}: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 1.2.3.4 txqueuelen 1000 (Ethernet) inet 1.2.3.4 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B) RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0 RX errors 0 dropped 0 overruns 0 frame 0
@ -337,7 +339,7 @@ function testPublicIpSegmentWithVPNTurnedOnOsx() {
} }
# Fake ifconfig # Fake ifconfig
fakeIfconfig fakeIfconfig "tun1"
assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")" assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")"