From 40e04e053cae8b49b5b82c047660369f962d1975 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sun, 3 Feb 2019 19:20:14 +0100 Subject: 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 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'functions/utilities.zsh') 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 +} -- cgit v1.2.3 From a00cb84da48b1f53a67c0ed43c90e6b4f720262f Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 4 Feb 2019 15:40:31 +0100 Subject: Fix regular expression and test --- functions/utilities.zsh | 2 +- test/segments/ip.spec | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'functions/utilities.zsh') 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 -- cgit v1.2.3 From 9f9bc62dc9c934fadd799c6942cc4099228b07be Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 6 Feb 2019 08:53:46 +0100 Subject: Avoid loopback interfaces and parse IPv4 on OSX --- functions/utilities.zsh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'functions/utilities.zsh') 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 -- cgit v1.2.3 From 1ab6a32c4b4871e4161cd75e485c203d0c2111e7 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Thu, 21 Feb 2019 18:32:17 +0100 Subject: Interface States can be unsorted --- functions/utilities.zsh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'functions/utilities.zsh') 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 -- cgit v1.2.3 From fef639eb488fb99007526cfa2d1d09c901fca3cc Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Fri, 22 Feb 2019 18:16:15 +0100 Subject: Fix regex --- functions/utilities.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'functions/utilities.zsh') 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 -- cgit v1.2.3 From 1049bca47373e1d8700dc81d2c61a30d83cbb899 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Fri, 22 Feb 2019 18:16:33 +0100 Subject: Strip all newlines from ifconfig output --- functions/utilities.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'functions/utilities.zsh') 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 -- cgit v1.2.3