Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

[Enhancement] Added support for kdig as fallback in prompt_public_ip #1260

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion segments/public_ip/public_ip.p9k
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
p9k::set_default P9K_PUBLIC_IP_NONE ""
p9k::set_default P9K_PUBLIC_IP_FILE "/tmp/p9k_public_ip"
p9k::set_default P9K_PUBLIC_IP_HOST "http://ident.me"
p9k::defined P9K_PUBLIC_IP_METHODS || P9K_PUBLIC_IP_METHODS=(dig curl wget)
p9k::defined P9K_PUBLIC_IP_METHODS || P9K_PUBLIC_IP_METHODS=(dig kdig curl wget)
}

# On macOS, stat is in /usr/bin/stat. However, if gnu utils are installed, this won't work.
Expand Down Expand Up @@ -71,6 +71,10 @@ prompt_public_ip() {
fresh_ip="$(dig +time=1 +tries=1 +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)"
[[ "$fresh_ip" =~ ^\; ]] && unset fresh_ip
;;
'kdig')
fresh_ip="$(kdig +timeout=1 +retry=1 +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)"
[[ "$fresh_ip" =~ ^\; ]] && unset fresh_ip
;;
'curl')
fresh_ip="$(curl --max-time 10 -w '\n' "$P9K_PUBLIC_IP_HOST" 2> /dev/null)"
;;
Expand Down
21 changes: 20 additions & 1 deletion segments/public_ip/public_ip.spec
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,29 @@ function testPublicIpSegmentUsesCurlAsFallbackMethodIfWgetIsNotAvailable() {
unalias wget
}

function testPublicIpSegmentUsesDigAsFallbackMethodIfWgetAndCurlAreNotAvailable() {
function testPublicIpSegmentUsesKdigAsFallbackMethodIfWgetAndCurlAreNotAvailable() {
local -a P9K_LEFT_PROMPT_ELEMENTS
P9K_LEFT_PROMPT_ELEMENTS=(public_ip)
alias curl='nocurl'
alias wget='nowget'
kdig() {
echo "kdig 1.2.3.4"
}

# Load Powerlevel9k
assertEquals "%K{000} %F{015}kdig 1.2.3.4 %k%F{000}%f " "$(__p9k_build_left_prompt)"

unfunction kdig
unalias curl
unalias wget
}

function testPublicIpSegmentUsesDigAsFallbackMethodIfKdigAndWgetAndCurlAreNotAvailable() {
local -a P9K_LEFT_PROMPT_ELEMENTS
P9K_LEFT_PROMPT_ELEMENTS=(public_ip)
alias curl='nocurl'
alias wget='nowget'
alias kdig='nokdig'
dig() {
echo "dig 1.2.3.4"
}
Expand All @@ -112,6 +130,7 @@ function testPublicIpSegmentUsesDigAsFallbackMethodIfWgetAndCurlAreNotAvailable(
assertEquals "%K{000} %F{015}dig 1.2.3.4 %k%F{000}%f " "$(__p9k_build_left_prompt)"

unfunction dig
unalias kdig
unalias curl
unalias wget
}
Expand Down