From a8fa0332d28713ad43b9258ad9b7cf0e3de8e1fb Mon Sep 17 00:00:00 2001 From: Shini31 Date: Sun, 30 Dec 2018 11:26:04 +0100 Subject: [PATCH 01/15] Use ip command for VPN segment - fix #1125 --- powerlevel9k.zsh-theme | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 79237385..65147c63 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -597,11 +597,19 @@ prompt_public_ip() { icon='PUBLIC_IP_ICON' # Check VPN is on if VPN interface is set if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then - for vpn_iface in $(/sbin/ifconfig | grep -e ^"$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE" | cut -d":" -f1) - do - icon='VPN_ICON' - break - done + if [[ "$OS" == "OSX" ]]; then + for vpn_iface in $(/sbin/ifconfig | grep -e ^"$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE" | cut -d":" -f1) + do + icon='VPN_ICON' + break + done + else + for vpn_iface in $(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.*") + do + icon='VPN_ICON' + break + done + fi fi $1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" "$icon" fi @@ -1134,11 +1142,19 @@ prompt_ip() { set_default POWERLEVEL9K_VPN_IP_INTERFACE "tun" # prompt if vpn active prompt_vpn_ip() { - for vpn_iface in $(/sbin/ifconfig | grep -e "^${POWERLEVEL9K_VPN_IP_INTERFACE}" | cut -d":" -f1) - do - ip=$(/sbin/ifconfig "$vpn_iface" | grep -o "inet\s.*" | cut -d' ' -f2) - "$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'VPN_ICON' - done + if [[ "$OS" == "OSX" ]]; then + for vpn_iface in $(/sbin/ifconfig | grep -e "^${POWERLEVEL9K_VPN_IP_INTERFACE}" | cut -d":" -f1) + do + ip=$(/sbin/ifconfig "$vpn_iface" | grep -o "inet\s.*" | cut -d' ' -f2) + "$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'VPN_ICON' + done + else + for vpn_iface in $(ip link ls up | grep -o -E ":\s+[a-z0-9]+:" | grep -v "lo" | grep -o -E "[a-z0-9]+" | grep -o -E "^${POWERLEVEL9K_VPN_IP_INTERFACE}.*") + do + ip=$(/sbin/ip -4 a show "$vpn_iface" | grep -o "inet\s*[0-9.]*" | grep -o -E "[0-9.]+") + "$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'VPN_ICON' + done + fi } ################################################################ From 9b73c8bd9c40be4101e6cd7edb09754e5d85c27a Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 21 Jan 2019 19:46:53 +0100 Subject: [PATCH 02/15] Add tests --- powerlevel9k.zsh-theme | 15 +-- test/segments/public_ip.spec | 83 ++++++++++++++++ test/segments/vpn_ip.spec | 181 +++++++++++++++++++++++++++++++++++ 3 files changed, 273 insertions(+), 6 deletions(-) create mode 100755 test/segments/vpn_ip.spec diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 65147c63..2f90581a 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -533,7 +533,9 @@ prompt_battery() { # * $1 Alignment: string - left|right # * $2 Index: integer # * $3 Joined: bool - If the segment should be joined +# * $4 Root Prefix: string - Root prefix for testing purposes prompt_public_ip() { + local ROOT_PREFIX="${4}" # set default values for segment set_default POWERLEVEL9K_PUBLIC_IP_TIMEOUT "300" set_default POWERLEVEL9K_PUBLIC_IP_NONE "" @@ -561,7 +563,7 @@ prompt_public_ip() { # grab a fresh IP if needed local fresh_ip - if [[ $refresh_ip =~ true && -w $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then + if [[ $refresh_ip == true && -w $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then for method in "${POWERLEVEL9K_PUBLIC_IP_METHODS[@]}"; do case $method in 'dig') @@ -598,7 +600,7 @@ prompt_public_ip() { # Check VPN is on if VPN interface is set if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then if [[ "$OS" == "OSX" ]]; then - for vpn_iface in $(/sbin/ifconfig | grep -e ^"$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE" | cut -d":" -f1) + for vpn_iface in $(${ROOT_PREFIX}/sbin/ifconfig | grep -e ^"$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE" | cut -d":" -f1) do icon='VPN_ICON' break @@ -1142,16 +1144,17 @@ prompt_ip() { set_default POWERLEVEL9K_VPN_IP_INTERFACE "tun" # prompt if vpn active prompt_vpn_ip() { + local ROOT_PREFIX="${4}" if [[ "$OS" == "OSX" ]]; then - for vpn_iface in $(/sbin/ifconfig | grep -e "^${POWERLEVEL9K_VPN_IP_INTERFACE}" | cut -d":" -f1) + for vpn_iface in $(${ROOT_PREFIX}/sbin/ifconfig | grep -e "^${POWERLEVEL9K_VPN_IP_INTERFACE}" | cut -d":" -f1) do - ip=$(/sbin/ifconfig "$vpn_iface" | grep -o "inet\s.*" | cut -d' ' -f2) + ip=$(${ROOT_PREFIX}/sbin/ifconfig "$vpn_iface" | grep -o "inet\s.*" | cut -d' ' -f2) "$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'VPN_ICON' done else - for vpn_iface in $(ip link ls up | grep -o -E ":\s+[a-z0-9]+:" | grep -v "lo" | grep -o -E "[a-z0-9]+" | grep -o -E "^${POWERLEVEL9K_VPN_IP_INTERFACE}.*") + 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_VPN_IP_INTERFACE}.*") do - ip=$(/sbin/ip -4 a show "$vpn_iface" | grep -o "inet\s*[0-9.]*" | grep -o -E "[0-9.]+") + ip=$(${ROOT_PREFIX}/sbin/ip -4 a show "$vpn_iface" | grep -o "inet\s*[0-9.]*" | grep -o -E "[0-9.]+") "$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'VPN_ICON' done fi diff --git a/test/segments/public_ip.spec b/test/segments/public_ip.spec index 334a6423..abfecbcb 100755 --- a/test/segments/public_ip.spec +++ b/test/segments/public_ip.spec @@ -12,6 +12,8 @@ function setUp() { P9K_HOME=$(pwd) FOLDER=/tmp/powerlevel9k-test mkdir -p $FOLDER + mkdir $FOLDER/bin + mkdir $FOLDER/sbin cd $FOLDER # Change cache file, so that the users environment don't @@ -219,4 +221,85 @@ function testPublicIpSegmentWhenGoingOnline() { unfunction dig } +function testPublicIpSegmentWithVPNTurnedOnLinux() { + local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip) + local OS='linux' + + echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE + local POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE="tun1" + + ip() { + cat < mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 + link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 +2: enp0s31f6: mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000 + link/ether 8c:16:45:7d:0c:9a brd ff:ff:ff:ff:ff:ff +3: tun1: mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000 + link/ether b4:6b:fc:9d:c6:bc brd ff:ff:ff:ff:ff:ff +5: docker0: mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default + link/ether 02:42:8f:5c:ed:39 brd ff:ff:ff:ff:ff:ff +EOF + } + + assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")" + + unfunction ip + rm -f $POWERLEVEL9K_PUBLIC_IP_FILE +} + +function testPublicIpSegmentWithVPNTurnedOnOsx() { + typeset -F now + now=$(date +%s) + + local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip) + local OS='OSX' + + echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE + local POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE="tun1" + + # Fake stat call + function stat() { + echo $now + } + + # Fake ifconfig + cat > $FOLDER/sbin/ifconfig < mtu 1500 + inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 + ether 02:42:8f:5c:ed:51 txqueuelen 0 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + +tun1: flags=4099 mtu 1500 + ether 8c:16:45:7d:0c:9a txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 + +lo: flags=73 mtu 65536 + inet 127.0.0.1 netmask 255.0.0.0 + inet6 ::1 prefixlen 128 scopeid 0x10 + loop txqueuelen 1000 (Local Loopback) + RX packets 5136 bytes 328651 (320.9 KiB) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 5136 bytes 328651 (320.9 KiB) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 +INNER +EOF + chmod +x $FOLDER/sbin/ifconfig + + assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")" + + rm -f $POWERLEVEL9K_PUBLIC_IP_FILE + unfunction stat +} + source shunit2/shunit2 \ No newline at end of file diff --git a/test/segments/vpn_ip.spec b/test/segments/vpn_ip.spec new file mode 100755 index 00000000..b213202e --- /dev/null +++ b/test/segments/vpn_ip.spec @@ -0,0 +1,181 @@ +#!/usr/bin/env zsh +#vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8 + +# Required for shunit2 to run correctly +setopt shwordsplit +SHUNIT_PARENT=$0 + +function setUp() { + export TERM="xterm-256color" + + # Test specific + P9K_HOME=$(pwd) + FOLDER=/tmp/powerlevel9k-test + mkdir -p $FOLDER + mkdir $FOLDER/bin + mkdir $FOLDER/sbin +} + +function tearDown() { + # Go back to powerlevel9k folder + cd "${P9K_HOME}" + # Remove eventually created test-specific folder + rm -fr "${FOLDER}" + + unset FOLDER + unset P9K_HOME +} + +function fakeIfconfig() { + # Fake ifconfig + cat > $FOLDER/sbin/ifconfig < mtu 1500 + inet 1.2.3.4 txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 +INNER + exit 0 +fi + + cat < mtu 1500 + inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 + ether 02:42:8f:5c:ed:51 txqueuelen 0 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + +tun1: flags=4099 mtu 1500 + inet 1.2.3.4 txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 + +lo: flags=73 mtu 65536 + inet 127.0.0.1 netmask 255.0.0.0 + inet6 ::1 prefixlen 128 scopeid 0x10 + loop txqueuelen 1000 (Local Loopback) + RX packets 5136 bytes 328651 (320.9 KiB) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 5136 bytes 328651 (320.9 KiB) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 +INNER +EOF + chmod +x $FOLDER/sbin/ifconfig +} + +function fakeIp() { + local INTERFACE1="${1}" + [[ -z "${INTERFACE1}" ]] && INTERFACE1="tun0" + local INTERFACE2="${2}" + [[ -z "${INTERFACE2}" ]] && INTERFACE2="disabled-if2" + cat > $FOLDER/sbin/ip < mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default + link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 +2: ${INTERFACE1}: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 + link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff +3: ${INTERFACE2}: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 + link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff +4: wlan0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 + link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff +INNER + fi + + if [[ "\$*" =~ 'show ${INTERFACE1}' ]]; then + cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 + inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 + valid_lft forever preferred_lft forever +INNER + fi + + if [[ "\$*" =~ 'show ${INTERFACE2}' ]]; then + cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 + inet 1.2.3.4 brd 10.0.2.255 scope global eth0 + valid_lft forever preferred_lft forever +INNER + fi +EOF + + chmod +x $FOLDER/sbin/ip +} + +function testVpnIpSegmentPrintsNothingOnOsxIfNotConnected() { + alias ifconfig='echo "not connected"' + + # Load Powerlevel9k + source powerlevel9k.zsh-theme + local OS="OSX" # Fake OSX + + assertEquals "" "$(prompt_vpn_ip left 1 false "$FOLDER")" + + unalias ifconfig +} + +function testVpnIpSegmentPrintsNothingOnLinuxIfNotConnected() { + alias ip='echo "not connected"' + + # Load Powerlevel9k + source powerlevel9k.zsh-theme + local OS="Linux" # Fake Linux + + assertEquals "" "$(prompt_vpn_ip left 1 false "$FOLDER")" + + unalias ip +} + +function testVpnIpSegmentWorksOnOsxWithInterfaceSpecified() { + local POWERLEVEL9K_VPN_IP_INTERFACE='tun1' + + fakeIfconfig + + # Load Powerlevel9k + source powerlevel9k.zsh-theme + local OS='OSX' # Fake OSX + + assertEquals "%K{006} %F{000}(vpn) %f%F{000}1.2.3.4 " "$(prompt_vpn_ip left 1 false "$FOLDER")" +} + +function testVpnIpSegmentWorksOnLinuxWithInterfaceSpecified() { + local POWERLEVEL9K_VPN_IP_INTERFACE='tun1' + + fakeIp "tun1" + + # Load Powerlevel9k + source powerlevel9k.zsh-theme + local OS='Linux' # Fake Linux + + assertEquals "%K{006} %F{000}(vpn) %f%F{000}10.0.2.15 " "$(prompt_vpn_ip left 1 false "$FOLDER")" +} + +# vpn_ip is not capable of handling multiple vpn interfaces ATM. +# function testVpnIpSegmentWorksOnLinuxWithMultipleInterfacesSpecified() { +# local POWERLEVEL9K_VPN_IP_INTERFACE=(tun0 tun1) + +# fakeIp "tun0" "tun1" + +# # Load Powerlevel9k +# source powerlevel9k.zsh-theme +# local OS='Linux' # Fake Linux + +# setopt xtrace +# assertEquals "%K{006} %F{000}(vpn) %f%F{000}10.0.2.15 " "$(prompt_vpn_ip left 1 false "$FOLDER")" +# unsetopt xtrace +# } + +source shunit2/shunit2 \ No newline at end of file From da50151d2487b3fb4a54a6ae101971605dde66ee Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 21 Jan 2019 19:48:28 +0100 Subject: [PATCH 03/15] Fix regular expression --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 2f90581a..e7b1b882 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -600,7 +600,7 @@ prompt_public_ip() { # Check VPN is on if VPN interface is set if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then if [[ "$OS" == "OSX" ]]; then - for vpn_iface in $(${ROOT_PREFIX}/sbin/ifconfig | grep -e ^"$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE" | cut -d":" -f1) + for vpn_iface in $(${ROOT_PREFIX}/sbin/ifconfig | grep -e "^$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE" | cut -d":" -f1) do icon='VPN_ICON' break From 32d3216bde4c22721ba7e4cb7ff39f12b5f3b2a4 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 22 Jan 2019 19:18:09 +0100 Subject: [PATCH 04/15] Fix tests --- test/segments/public_ip.spec | 2 +- test/segments/vpn_ip.spec | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/segments/public_ip.spec b/test/segments/public_ip.spec index abfecbcb..a3f41e61 100755 --- a/test/segments/public_ip.spec +++ b/test/segments/public_ip.spec @@ -266,7 +266,7 @@ function testPublicIpSegmentWithVPNTurnedOnOsx() { # Fake ifconfig cat > $FOLDER/sbin/ifconfig < mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 diff --git a/test/segments/vpn_ip.spec b/test/segments/vpn_ip.spec index b213202e..3333fd07 100755 --- a/test/segments/vpn_ip.spec +++ b/test/segments/vpn_ip.spec @@ -29,7 +29,7 @@ function tearDown() { function fakeIfconfig() { # Fake ifconfig cat > $FOLDER/sbin/ifconfig < $FOLDER/sbin/ip < Date: Wed, 23 Jan 2019 01:01:49 +0100 Subject: [PATCH 05/15] Improve tests --- powerlevel9k.zsh-theme | 2 +- test/segments/public_ip.spec | 142 +++++++++++++++++++++++------------ 2 files changed, 93 insertions(+), 51 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index e7b1b882..6902ed10 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -606,7 +606,7 @@ prompt_public_ip() { break done else - for vpn_iface in $(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.*") + 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.*") do icon='VPN_ICON' break diff --git a/test/segments/public_ip.spec b/test/segments/public_ip.spec index a3f41e61..eeb50596 100755 --- a/test/segments/public_ip.spec +++ b/test/segments/public_ip.spec @@ -35,6 +35,95 @@ function tearDown() { unset POWERLEVEL9K_PUBLIC_IP_FILE } +function fakeIfconfig() { + # Fake ifconfig + cat > $FOLDER/sbin/ifconfig < mtu 1500 + inet 1.2.3.4 txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 +INNER + exit 0 +fi + + cat < mtu 1500 + inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 + ether 02:42:8f:5c:ed:51 txqueuelen 0 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + +tun1: flags=4099 mtu 1500 + inet 1.2.3.4 txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 + +lo: flags=73 mtu 65536 + inet 127.0.0.1 netmask 255.0.0.0 + inet6 ::1 prefixlen 128 scopeid 0x10 + loop txqueuelen 1000 (Local Loopback) + RX packets 5136 bytes 328651 (320.9 KiB) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 5136 bytes 328651 (320.9 KiB) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 +INNER +EOF + chmod +x $FOLDER/sbin/ifconfig +} + +function fakeIp() { + local INTERFACE1="${1}" + [[ -z "${INTERFACE1}" ]] && INTERFACE1="tun0" + local INTERFACE2="${2}" + [[ -z "${INTERFACE2}" ]] && INTERFACE2="disabled-if2" + cat > $FOLDER/sbin/ip < mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default + link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 +2: ${INTERFACE1}: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 + link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff +3: ${INTERFACE2}: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 + link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff +4: wlan0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 + link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff +INNER + fi + + if [[ "\$*" =~ 'show ${INTERFACE1}' ]]; then + cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 + inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 + valid_lft forever preferred_lft forever +INNER + fi + + if [[ "\$*" =~ 'show ${INTERFACE2}' ]]; then + cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 + inet 1.2.3.4 brd 10.0.2.255 scope global eth0 + valid_lft forever preferred_lft forever +INNER + fi +EOF + + chmod +x $FOLDER/sbin/ip +} + function testPublicIpSegmentPrintsNothingByDefaultIfHostIsNotAvailable() { local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip custom_world) @@ -222,38 +311,21 @@ function testPublicIpSegmentWhenGoingOnline() { } function testPublicIpSegmentWithVPNTurnedOnLinux() { - local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip) local OS='linux' echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE local POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE="tun1" - ip() { - cat < mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 - link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 -2: enp0s31f6: mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000 - link/ether 8c:16:45:7d:0c:9a brd ff:ff:ff:ff:ff:ff -3: tun1: mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000 - link/ether b4:6b:fc:9d:c6:bc brd ff:ff:ff:ff:ff:ff -5: docker0: mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default - link/ether 02:42:8f:5c:ed:39 brd ff:ff:ff:ff:ff:ff -EOF - } + # Fake ip command + fakeIp "tun1" assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")" - - unfunction ip - rm -f $POWERLEVEL9K_PUBLIC_IP_FILE } function testPublicIpSegmentWithVPNTurnedOnOsx() { typeset -F now now=$(date +%s) - local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip) local OS='OSX' echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE @@ -265,40 +337,10 @@ function testPublicIpSegmentWithVPNTurnedOnOsx() { } # Fake ifconfig - cat > $FOLDER/sbin/ifconfig < mtu 1500 - inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 - ether 02:42:8f:5c:ed:51 txqueuelen 0 (Ethernet) - RX packets 0 bytes 0 (0.0 B) - RX errors 0 dropped 0 overruns 0 frame 0 - TX packets 0 bytes 0 (0.0 B) - TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 - -tun1: flags=4099 mtu 1500 - ether 8c:16:45:7d:0c:9a txqueuelen 1000 (Ethernet) - RX packets 0 bytes 0 (0.0 B) - RX errors 0 dropped 0 overruns 0 frame 0 - TX packets 0 bytes 0 (0.0 B) - TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 - device interrupt 16 memory 0xe8200000-e8220000 - -lo: flags=73 mtu 65536 - inet 127.0.0.1 netmask 255.0.0.0 - inet6 ::1 prefixlen 128 scopeid 0x10 - loop txqueuelen 1000 (Local Loopback) - RX packets 5136 bytes 328651 (320.9 KiB) - RX errors 0 dropped 0 overruns 0 frame 0 - TX packets 5136 bytes 328651 (320.9 KiB) - TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 -INNER -EOF - chmod +x $FOLDER/sbin/ifconfig + fakeIfconfig assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")" - rm -f $POWERLEVEL9K_PUBLIC_IP_FILE unfunction stat } From 54a985cb11861f3857daa7001558f015d971dc3d Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 23 Jan 2019 01:02:42 +0100 Subject: [PATCH 06/15] Simplify code that checks if a VPN is running --- powerlevel9k.zsh-theme | 15 +++++---------- test/segments/public_ip.spec | 8 +++++--- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 6902ed10..7437fe32 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -600,17 +600,12 @@ prompt_public_ip() { # Check VPN is on if VPN interface is set if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then if [[ "$OS" == "OSX" ]]; then - for vpn_iface in $(${ROOT_PREFIX}/sbin/ifconfig | grep -e "^$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE" | cut -d":" -f1) - do - icon='VPN_ICON' - break - done + local interface="$(${ROOT_PREFIX}/sbin/ifconfig $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE)" + # Check if interface is UP. + [[ "$interface" =~ " $FOLDER/sbin/ifconfig < mtu 1500 +${INTERFACE}: flags=4099 mtu 1500 inet 1.2.3.4 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 @@ -62,7 +64,7 @@ docker0: flags=4099 mtu 1500 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 -tun1: flags=4099 mtu 1500 +${INTERFACE}: flags=4099 mtu 1500 inet 1.2.3.4 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 @@ -337,7 +339,7 @@ function testPublicIpSegmentWithVPNTurnedOnOsx() { } # 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")" From bde4337992e01738e72ef589c321299193580838 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 23 Jan 2019 18:56:07 +0100 Subject: [PATCH 07/15] Match VPN Interface fuzzy --- powerlevel9k.zsh-theme | 22 +++++++++++++++++++--- test/segments/public_ip.spec | 31 ++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 7437fe32..081562b5 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -600,9 +600,25 @@ prompt_public_ip() { # Check VPN is on if VPN interface is set if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then if [[ "$OS" == "OSX" ]]; then - local interface="$(${ROOT_PREFIX}/sbin/ifconfig $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE)" - # Check if interface is UP. - [[ "$interface" =~ " $FOLDER/sbin/ifconfig < mtu 1500 inet 1.2.3.4 txqueuelen 1000 (Ethernet) @@ -55,6 +60,7 @@ INNER exit 0 fi +if [[ "\$#" == "0" ]]; then cat < mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 @@ -81,6 +87,7 @@ lo: flags=73 mtu 65536 TX packets 5136 bytes 328651 (320.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 INNER +fi EOF chmod +x $FOLDER/sbin/ifconfig } @@ -346,4 +353,26 @@ function testPublicIpSegmentWithVPNTurnedOnOsx() { unfunction stat } +function testPublicIpSegmentWithVPNTurnedOnAndFuzzyMatchingOnOsx() { + typeset -F now + now=$(date +%s) + + local OS='OSX' + + echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE + local POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE="tun" + + # Fake stat call + function stat() { + echo $now + } + + # Fake ifconfig + fakeIfconfig "tun3" + + assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")" + + unfunction stat +} + source shunit2/shunit2 \ No newline at end of file From 747b94b1b6b8450375a2b1619d8a5c7336be1852 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 23 Jan 2019 19:10:46 +0100 Subject: [PATCH 08/15] Match VPN interface fuzzy on Linux --- powerlevel9k.zsh-theme | 11 +++++++++-- test/segments/public_ip.spec | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 081562b5..3db36050 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -620,8 +620,15 @@ prompt_public_ip() { fi done else - local interface=$(${ROOT_PREFIX}/sbin/ip -brief -4 a show "${POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE}") - [[ -n "$interface" ]] && icon='VPN_ICON' + local -a interfaces + interfaces=( "${(f)$(${ROOT_PREFIX}/sbin/ip -brief -4 a show)}" ) + local pattern="^${POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE}[ ]+UP[ ]+" + for interface in "${(@)interfaces}"; do + if [[ "$interface" =~ $pattern ]]; then + icon='VPN_ICON' + break + fi + done fi fi $1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" "$icon" diff --git a/test/segments/public_ip.spec b/test/segments/public_ip.spec index 39b0243c..5ae62321 100755 --- a/test/segments/public_ip.spec +++ b/test/segments/public_ip.spec @@ -113,6 +113,15 @@ function fakeIp() { INNER fi + if [[ "\$*" =~ '-brief.*show' ]]; then + cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 From 40e04e053cae8b49b5b82c047660369f962d1975 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sun, 3 Feb 2019 19:20:14 +0100 Subject: [PATCH 09/15] Parse IPs properly This is done if we want to show a public IP, internal IP, or a VPN. In the VPN case, what we actually want is to display an indicator that a VPN is active, instead of the VPN IP itself. We parse the IP here anyway, because we want to save some specific code there. --- functions/utilities.zsh | 48 ++++++ powerlevel9k.zsh-theme | 76 +-------- test/segments/ip.spec | 351 ++++++++++++++++++++------------------ test/segments/vpn_ip.spec | 33 +++- 4 files changed, 262 insertions(+), 246 deletions(-) diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 127007fb..a15bf650 100755 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -371,3 +371,51 @@ function upsearch () { popd > /dev/null fi } + +# Parse IP address from ifconfig on OSX and from IP on Linux +# Parameters: +# $1 - string The desired Interface +# $2 - string A root prefix for testing purposes +function p9k::parseIp() { + local desiredInterface="${1}" + + if [[ -z "${desiredInterface}" ]]; then + desiredInterface="^[^ ]+" + fi + + local ROOT_PREFIX="${2}" + if [[ "$OS" == "OSX" ]]; then + # Get a plain list of all interfaces + local rawInterfaces="$(${ROOT_PREFIX}/sbin/ifconfig -l 2>/dev/null)" + # Parse into array (split by whitespace) + local -a interfaces + interfaces=(${=rawInterfaces}) + # Parse only relevant interface names + local pattern="${desiredInterface}[^ ]?" + local -a relevantInterfaces + for rawInterface in $interfaces; do + [[ "$rawInterface" =~ $pattern ]] && relevantInterfaces+=( $MATCH ) + done + local newline=$'\n' + for interfaceName in $relevantInterfaces; do + local interface="$(${ROOT_PREFIX}/sbin/ifconfig $interfaceName 2>/dev/null)" + # Check if interface is UP. + if [[ "${interface/${newline}/}" =~ "]*>(.*?)inet[ ]*([^ ]*)" ]]; then + echo "${match[3]}" + return 0 + fi + done + else + local -a interfaces + interfaces=( "${(f)$(${ROOT_PREFIX}/sbin/ip -brief -4 a show 2>/dev/null)}" ) + local pattern="^${desiredInterface}[ ]+UP[ ]+([^/ ]+)" + for interface in "${(@)interfaces}"; do + if [[ "$interface" =~ $pattern ]]; then + echo "${match[1]}" + return 0 + fi + done + fi + + return 1 +} diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 3db36050..d114fcc8 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -599,36 +599,9 @@ prompt_public_ip() { icon='PUBLIC_IP_ICON' # Check VPN is on if VPN interface is set if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then - if [[ "$OS" == "OSX" ]]; then - # Get a plain list of all interfaces - local rawInterfaces="$(${ROOT_PREFIX}/sbin/ifconfig -l)" - # Parse into array (split by whitespace) - local -a interfaces - interfaces=(${=rawInterfaces}) - # Parse only relevant interface names - local pattern="${POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE}[^ ]?" - local -a relevantInterfaces - for rawInterface in $interfaces; do - [[ "$rawInterface" =~ $pattern ]] && relevantInterfaces+=( $MATCH ) - done - for interfaceName in $relevantInterfaces; do - local interface="$(${ROOT_PREFIX}/sbin/ifconfig $interfaceName)" - # Check if interface is UP. - if [[ "$interface" =~ " $FOLDER/sbin/ifconfig < mtu 1500 + inet ${INTERFACE1_IP} txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 +INNER + exit 0 +fi + +if [[ "\$*" =~ '${INTERFACE2}' ]]; then + cat < mtu 1500 + inet ${INTERFACE2_IP} txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 +INNER + exit 0 +fi + + +# If neither INTERFACE1 nor INTERFACE2 is queried, fake a offline (DOWN) interface. +# We assume if there is at least one argument, we queried for a specific interface. +if [[ "\$#" -gt 0 ]]; then + cat < mtu 1500 + inet 5.5.5.5 txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 +INNER + exit 0 +fi + +if [[ "\$#" -eq 0 ]]; then + cat < mtu 1500 + inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 + ether 02:42:8f:5c:ed:51 txqueuelen 0 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + +tun1: flags=4099 mtu 1500 + inet 10.20.30.40 txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 + +${INTERFACE1}: flags=4099 mtu 1500 + inet ${INTERFACE1_IP} txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 + +${INTERFACE2}: flags=4099 mtu 1500 + inet ${INTERFACE2_IP} txqueuelen 1000 (Ethernet) + RX packets 0 bytes 0 (0.0 B) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 0 bytes 0 (0.0 B) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 + device interrupt 16 memory 0xe8200000-e8220000 + +lo: flags=73 mtu 65536 + inet 127.0.0.1 netmask 255.0.0.0 + inet6 ::1 prefixlen 128 scopeid 0x10 + loop txqueuelen 1000 (Local Loopback) + RX packets 5136 bytes 328651 (320.9 KiB) + RX errors 0 dropped 0 overruns 0 frame 0 + TX packets 5136 bytes 328651 (320.9 KiB) + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 +INNER +exit 0 +fi +EOF + chmod +x $FOLDER/sbin/ifconfig } -function testIpSegmentWorksOnOsxWithNoInterfaceSpecified() { - local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip) - alias networksetup="echo 'An asterisk (*) denotes that a network service is disabled. -(1) Ethernet -(Hardware Port: Ethernet, Device: en0) - -(2) FireWire -(Hardware Port: FireWire, Device: fw0) +function fakeIp() { + local INTERFACE1="${1}" + [[ -z "${INTERFACE1}" ]] && INTERFACE1="eth0" + local INTERFACE2="${2}" + [[ -z "${INTERFACE2}" ]] && INTERFACE2="disabled-if2" + cat > $FOLDER/sbin/ip < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 + inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 + valid_lft forever preferred_lft forever +INNER + fi + + if [[ "\$*" =~ 'show ${INTERFACE2}' ]]; then + cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 + inet 1.2.3.4 brd 10.0.2.255 scope global eth0 + valid_lft forever preferred_lft forever +INNER + fi +EOF + + chmod +x $FOLDER/sbin/ip +} -(4) Bluetooth PAN -(Hardware Port: Bluetooth PAN, Device: en3) +function testIpSegmentPrintsNothingOnOsxIfNotConnected() { + cat > $FOLDER/sbin/ifconfig < $FOLDER/sbin/ip < mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default - link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 -2: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 - link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff"; - fi - - if [[ "$*" == '-4 a show eth0' ]]; then - echo '2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 - inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 - valid_lft forever preferred_lft forever'; - fi - } - - # Load Powerlevel9k - source powerlevel9k.zsh-theme - local OS='Linux' # Fake Linux - - assertEquals "%K{006} %F{000}IP %f%F{000}10.0.2.15 %k%F{006}%f " "$(build_left_prompt)" - - unfunction ip -} - -function testIpSegmentWorksOnLinuxWithMultipleInterfacesSpecified() { - setopt aliases - local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip) - # That command is harder to test, as it is used at first - # to get all relevant network interfaces and then for - # getting the configuration of that segment.. - ip(){ - if [[ "$*" == 'link ls up' ]]; then - echo "1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default - link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 -2: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 - link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff -3: eth1: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 - link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff -4: wlan0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 - link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff"; - fi - - if [[ "$*" == '-4 a show eth1' ]]; then - echo '3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 - inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 - valid_lft forever preferred_lft forever'; - fi - } - # Load Powerlevel9k source powerlevel9k.zsh-theme local OS='Linux' # Fake Linux - assertEquals "%K{006} %F{000}IP %f%F{000}10.0.2.15 %k%F{006}%f " "$(build_left_prompt)" + fakeIp "eth0" - unfunction ip + assertEquals "%K{006} %F{000}IP %f%F{000}1.2.3.4 " "$(prompt_ip left 1 false "$FOLDER")" } function testIpSegmentWorksOnLinuxWithInterfaceSpecified() { - local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip) - local POWERLEVEL9K_IP_INTERFACE='xxx' - ip(){ - echo '2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 -inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 - valid_lft forever preferred_lft forever'; - } + fakeIp "eth3" # Load Powerlevel9k source powerlevel9k.zsh-theme local OS='Linux' # Fake Linux - assertEquals "%K{006} %F{000}IP %f%F{000}10.0.2.15 %k%F{006}%f " "$(build_left_prompt)" - - unfunction ip + assertEquals "%K{006} %F{000}IP %f%F{000}1.2.3.4 " "$(prompt_ip left 1 false "$FOLDER")" } source shunit2/shunit2 \ No newline at end of file diff --git a/test/segments/vpn_ip.spec b/test/segments/vpn_ip.spec index 3333fd07..13a3e60a 100755 --- a/test/segments/vpn_ip.spec +++ b/test/segments/vpn_ip.spec @@ -95,6 +95,15 @@ function fakeIp() { INNER fi + if [[ "\$*" =~ '-brief.*show' ]]; then + cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 @@ -116,27 +125,33 @@ EOF } function testVpnIpSegmentPrintsNothingOnOsxIfNotConnected() { - alias ifconfig='echo "not connected"' - # Load Powerlevel9k source powerlevel9k.zsh-theme local OS="OSX" # Fake OSX - assertEquals "" "$(prompt_vpn_ip left 1 false "$FOLDER")" + cat > $FOLDER/sbin/ifconfig < $FOLDER/sbin/ip < Date: Mon, 4 Feb 2019 15:40:31 +0100 Subject: [PATCH 10/15] Fix regular expression and test --- functions/utilities.zsh | 2 +- test/segments/ip.spec | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 9a72eab8..5532eef6 100755 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -402,7 +402,7 @@ function p9k::parseIp() { for interfaceName in $relevantInterfaces; do local interface="$(${ROOT_PREFIX}/sbin/ifconfig $interfaceName 2>/dev/null)" # Check if interface is UP. - if [[ "${interface/${newline}/}" =~ "]*>(.*?)inet[ ]*([^ ]*)" ]]; then + if [[ "${interface/${newline}/}" =~ "]*>(.*)inet[ ]*([^ ]*)" ]]; then echo "${match[3]}" return 0 fi diff --git a/test/segments/ip.spec b/test/segments/ip.spec index 9fb0bddb..2dce55f7 100755 --- a/test/segments/ip.spec +++ b/test/segments/ip.spec @@ -212,6 +212,8 @@ function testIpSegmentWorksOnOsxWithNoInterfaceSpecified() { function testIpSegmentWorksOnOsxWithInterfaceSpecified() { fakeIfconfig "eth1" + local POWERLEVEL9K_IP_INTERFACE="eth1" + # Load Powerlevel9k source powerlevel9k.zsh-theme local OS='OSX' # Fake OSX @@ -232,6 +234,8 @@ function testIpSegmentWorksOnLinuxWithNoInterfaceSpecified() { function testIpSegmentWorksOnLinuxWithInterfaceSpecified() { fakeIp "eth3" + local POWERLEVEL9K_IP_INTERFACE="eth3" + # Load Powerlevel9k source powerlevel9k.zsh-theme local OS='Linux' # Fake Linux From 9f9bc62dc9c934fadd799c6942cc4099228b07be Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 6 Feb 2019 08:53:46 +0100 Subject: [PATCH 11/15] Avoid loopback interfaces and parse IPv4 on OSX --- functions/utilities.zsh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 5532eef6..13a7b82d 100755 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -401,8 +401,11 @@ function p9k::parseIp() { local newline=$'\n' for interfaceName in $relevantInterfaces; do local interface="$(${ROOT_PREFIX}/sbin/ifconfig $interfaceName 2>/dev/null)" + if [[ "${interface}" =~ "lo[0-9]*" ]]; then + continue + fi # Check if interface is UP. - if [[ "${interface/${newline}/}" =~ "]*>(.*)inet[ ]*([^ ]*)" ]]; then + if [[ "${interface/${newline}/}" =~ "]*>(.*)inet[ ]+([^ ]*)" ]]; then echo "${match[3]}" return 0 fi From 1ab6a32c4b4871e4161cd75e485c203d0c2111e7 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Thu, 21 Feb 2019 18:32:17 +0100 Subject: [PATCH 12/15] Interface States can be unsorted --- functions/utilities.zsh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 13a7b82d..069fb1a8 100755 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -405,9 +405,13 @@ function p9k::parseIp() { continue fi # Check if interface is UP. - if [[ "${interface/${newline}/}" =~ "]*>(.*)inet[ ]+([^ ]*)" ]]; then - echo "${match[3]}" - return 0 + if [[ "${interface/${newline}/}" =~ "<([^>]*)>(.*?)inet[ ]+([^ ]*)" ]]; then + local ipFound="${match[3]}" + local -a interfaceStates=(${(s:,:)match[1]}) + if [[ "${interfaceStates[(r)UP]}" == "UP" ]]; then + echo "${ipFound}" + return 0 + fi fi done else From fef639eb488fb99007526cfa2d1d09c901fca3cc Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Fri, 22 Feb 2019 18:16:15 +0100 Subject: [PATCH 13/15] Fix regex --- functions/utilities.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 069fb1a8..2e6d98dd 100755 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -405,7 +405,7 @@ function p9k::parseIp() { continue fi # Check if interface is UP. - if [[ "${interface/${newline}/}" =~ "<([^>]*)>(.*?)inet[ ]+([^ ]*)" ]]; then + if [[ "${interface/${newline}/}" =~ "<([^>]*)>(.*)inet[ ]+([^ ]*)" ]]; then local ipFound="${match[3]}" local -a interfaceStates=(${(s:,:)match[1]}) if [[ "${interfaceStates[(r)UP]}" == "UP" ]]; then From 1049bca47373e1d8700dc81d2c61a30d83cbb899 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Fri, 22 Feb 2019 18:16:33 +0100 Subject: [PATCH 14/15] Strip all newlines from ifconfig output --- functions/utilities.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 2e6d98dd..8c18bb44 100755 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -405,7 +405,7 @@ function p9k::parseIp() { continue fi # Check if interface is UP. - if [[ "${interface/${newline}/}" =~ "<([^>]*)>(.*)inet[ ]+([^ ]*)" ]]; then + if [[ "${interface//${newline}/}" =~ "<([^>]*)>(.*)inet[ ]+([^ ]*)" ]]; then local ipFound="${match[3]}" local -a interfaceStates=(${(s:,:)match[1]}) if [[ "${interfaceStates[(r)UP]}" == "UP" ]]; then From 0dbfa4e1e4bfd65311403884168b7cddb17cdbdc Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 25 Feb 2019 18:22:59 +0100 Subject: [PATCH 15/15] Add small changes suggested by @Syphdias --- test/segments/ip.spec | 3 +-- test/segments/public_ip.spec | 3 +-- test/segments/vpn_ip.spec | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/test/segments/ip.spec b/test/segments/ip.spec index 2dce55f7..86819e19 100755 --- a/test/segments/ip.spec +++ b/test/segments/ip.spec @@ -12,7 +12,6 @@ function setUp() { P9K_HOME=$(pwd) FOLDER=/tmp/powerlevel9k-test mkdir -p $FOLDER - mkdir $FOLDER/bin mkdir $FOLDER/sbin } @@ -152,7 +151,7 @@ INNER if [[ "\$*" =~ 'show ${INTERFACE1}' ]]; then cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 +2: ${INTERFACE1}: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 valid_lft forever preferred_lft forever INNER diff --git a/test/segments/public_ip.spec b/test/segments/public_ip.spec index 2c7a00e9..19c3adbc 100755 --- a/test/segments/public_ip.spec +++ b/test/segments/public_ip.spec @@ -12,7 +12,6 @@ function setUp() { P9K_HOME=$(pwd) FOLDER=/tmp/powerlevel9k-test mkdir -p $FOLDER - mkdir $FOLDER/bin mkdir $FOLDER/sbin cd $FOLDER @@ -124,7 +123,7 @@ INNER if [[ "\$*" =~ 'show ${INTERFACE1}' ]]; then cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 +2: ${INTERFACE1}: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 valid_lft forever preferred_lft forever INNER diff --git a/test/segments/vpn_ip.spec b/test/segments/vpn_ip.spec index 870126e5..fe5b5fbc 100755 --- a/test/segments/vpn_ip.spec +++ b/test/segments/vpn_ip.spec @@ -12,7 +12,6 @@ function setUp() { P9K_HOME=$(pwd) FOLDER=/tmp/powerlevel9k-test mkdir -p $FOLDER - mkdir $FOLDER/bin mkdir $FOLDER/sbin } @@ -106,7 +105,7 @@ INNER if [[ "\$*" =~ 'show ${INTERFACE1}' ]]; then cat < mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 +2: ${INTERFACE1}: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 valid_lft forever preferred_lft forever INNER