1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-14 09:00:07 +00:00

speed up ip, vpn_ip and public_ip prompts

This commit is contained in:
romkatv 2019-05-23 00:00:12 +02:00
parent e64be938b1
commit 88963159fd
2 changed files with 53 additions and 104 deletions

View file

@ -72,54 +72,41 @@ segment_in_use() {
-n "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(r)${key}_joined]}" ]] -n "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(r)${key}_joined]}" ]]
} }
# Parse IP address from ifconfig on OSX and from IP on Linux function _p9k_parse_ip() {
# Parameters: local desiredInterface=${1:-'^[^ ]+'}
# $1 - string The desired Interface
# $2 - string A root prefix for testing purposes
function p9k::parseIp() {
local desiredInterface="${1}"
if [[ -z "${desiredInterface}" ]]; then if [[ $OS == OSX ]]; then
desiredInterface="^[^ ]+" [[ -x /sbin/ifconfig ]] || return
fi local rawInterfaces && rawInterfaces="$(/sbin/ifconfig -l 2>/dev/null)" || return
local -a interfaces=(${(A)=rawInterfaces})
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 pattern="${desiredInterface}[^ ]?"
local -a relevantInterfaces local -a relevantInterfaces
for rawInterface in $interfaces; do for rawInterface in $interfaces; do
[[ "$rawInterface" =~ $pattern ]] && relevantInterfaces+=( $MATCH ) [[ "$rawInterface" =~ $pattern ]] && relevantInterfaces+=$MATCH
done done
local newline=$'\n' local newline=$'\n'
local interfaceName interface
for interfaceName in $relevantInterfaces; do for interfaceName in $relevantInterfaces; do
local interface="$(${ROOT_PREFIX}/sbin/ifconfig $interfaceName 2>/dev/null)" interface="$(/sbin/ifconfig $interfaceName 2>/dev/null)" || continue
if [[ "${interface}" =~ "lo[0-9]*" ]]; then [[ "${interface}" =~ "lo[0-9]*" ]] && continue
continue
fi
# Check if interface is UP.
if [[ "${interface//${newline}/}" =~ "<([^>]*)>(.*)inet[ ]+([^ ]*)" ]]; then if [[ "${interface//${newline}/}" =~ "<([^>]*)>(.*)inet[ ]+([^ ]*)" ]]; then
local ipFound="${match[3]}" local ipFound="${match[3]}"
local -a interfaceStates=(${(s:,:)match[1]}) local -a interfaceStates=(${(s:,:)match[1]})
if [[ "${interfaceStates[(r)UP]}" == "UP" ]]; then if (( "${interfaceStates[(I)UP]}" )); then
echo "${ipFound}" _P9K_RETVAL=$ipFound
return 0 return
fi fi
fi fi
done done
else else
local -a interfaces [[ -x /sbin/ip ]] || return
interfaces=( "${(f)$(${ROOT_PREFIX}/sbin/ip -brief -4 a show 2>/dev/null)}" ) local -a interfaces=( "${(f)$(/sbin/ip -brief -4 a show 2>/dev/null)}" )
local pattern="^${desiredInterface}[ ]+UP[ ]+([^/ ]+)" local pattern="^${desiredInterface}[[:space:]]+UP[[:space:]]+([^/ ]+)"
local interface
for interface in "${(@)interfaces}"; do for interface in "${(@)interfaces}"; do
if [[ "$interface" =~ $pattern ]]; then if [[ "$interface" =~ $pattern ]]; then
echo "${match[1]}" _P9K_RETVAL=$match[1]
return 0 return
fi fi
done done
fi fi

View file

@ -561,86 +561,56 @@ prompt_battery() {
$1_prompt_segment $0_$state $2 "$bg" "$_P9K_BATTERY_STATES[$state]" $icon 0 '' $msg $1_prompt_segment $0_$state $2 "$bg" "$_P9K_BATTERY_STATES[$state]" $icon 0 '' $msg
} }
typeset -gF _P9K_PUBLIC_IP_TIMESTAMP
typeset -g _P9K_PUBLIC_IP
################################################################ ################################################################
# Public IP segment # Public IP segment
# Parameters:
# * $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
set_default -i POWERLEVEL9K_PUBLIC_IP_TIMEOUT 300 set_default -i POWERLEVEL9K_PUBLIC_IP_TIMEOUT 300
set_default -a POWERLEVEL9K_PUBLIC_IP_METHODS dig curl wget set_default -a POWERLEVEL9K_PUBLIC_IP_METHODS dig curl wget
set_default POWERLEVEL9K_PUBLIC_IP_NONE "" set_default POWERLEVEL9K_PUBLIC_IP_NONE ""
set_default POWERLEVEL9K_PUBLIC_IP_FILE "/tmp/p9k_public_ip"
set_default POWERLEVEL9K_PUBLIC_IP_HOST "http://ident.me" set_default POWERLEVEL9K_PUBLIC_IP_HOST "http://ident.me"
set_default POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE "" set_default POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ""
prompt_public_ip() { prompt_public_ip() {
local ROOT_PREFIX="${4}" if (( ! $#_P9K_PUBLIC_IP || EPOCHREALTIME >= _P9K_PUBLIC_IP_TIMESTAMP + POWERLEVEL9K_PUBLIC_IP_TIMEOUT )); then
_P9K_PUBLIC_IP=''
# Do we need a fresh IP? local method
local refresh_ip=false for method in $POWERLEVEL9K_PUBLIC_IP_METHODS; do
if [[ -f $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then
typeset -i timediff
# if saved IP is more than
if [[ "$OS" == "OSX" ]]; then
timediff=$(($(date +%s) - $(stat -f "%m" $POWERLEVEL9K_PUBLIC_IP_FILE)))
else
timediff=$(($(date +%s) - $(date -r $POWERLEVEL9K_PUBLIC_IP_FILE +%s)))
fi
(( timediff > POWERLEVEL9K_PUBLIC_IP_TIMEOUT )) && refresh_ip=true
# If tmp file is empty get a fresh IP
[[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) ]] && refresh_ip=true
[[ -n $POWERLEVEL9K_PUBLIC_IP_NONE ]] && [[ $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) =~ "$POWERLEVEL9K_PUBLIC_IP_NONE" ]] && refresh_ip=true
else
touch $POWERLEVEL9K_PUBLIC_IP_FILE && refresh_ip=true
fi
# grab a fresh IP if needed
local fresh_ip
if [[ $refresh_ip == true && -w $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then
for method in "${POWERLEVEL9K_PUBLIC_IP_METHODS[@]}"; do
case $method in case $method in
'dig') 'dig')
fresh_ip="$(dig +time=1 +tries=1 +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)" if (( $+commands[dig] )); then
[[ "$fresh_ip" =~ ^\; ]] && unset fresh_ip _P9K_PUBLIC_IP="$(command dig +time=1 +tries=1 +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)"
;; [[ $_P9K_PUBLIC_IP == ';'* ]] && _P9K_PUBLIC_IP=''
fi
;;
'curl') 'curl')
fresh_ip="$(curl --max-time 10 -w '\n' "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" if (( $+commands[curl] )); then
;; _P9K_PUBLIC_IP="$(command curl --max-time 10 -w '\n' "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)"
fi
;;
'wget') 'wget')
fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" if (( $+commands[wget] )); then
;; _P9K_PUBLIC_IP="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)"
fi
;;
esac esac
# If we found a fresh IP, break loop. if [[ -n $_P9K_PUBLIC_IP ]]; then
if [[ -n "${fresh_ip}" ]]; then _P9K_PUBLIC_IP_TIMESTAMP=$EPOCHREALTIME
break; break
fi fi
done done
# write IP to tmp file or clear tmp file if an IP was not retrieved
# Redirection with `>!`. From the manpage: Same as >, except that the file
# is truncated to zero length if it exists, even if CLOBBER is unset.
# If the file already exists, and a simple `>` redirection and CLOBBER
# unset, ZSH will produce an error.
[[ -n "${fresh_ip}" ]] && echo $fresh_ip >! $POWERLEVEL9K_PUBLIC_IP_FILE || echo $POWERLEVEL9K_PUBLIC_IP_NONE >! $POWERLEVEL9K_PUBLIC_IP_FILE
fi fi
# read public IP saved to tmp file local ip=${_P9K_PUBLIC_IP:-$POWERLEVEL9K_PUBLIC_IP_NONE}
local public_ip="$(cat $POWERLEVEL9K_PUBLIC_IP_FILE)" [[ -n $ip ]] || return
# Draw the prompt segment local icon='PUBLIC_IP_ICON'
if [[ -n $public_ip ]]; then if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then
icon='PUBLIC_IP_ICON' _p9k_parse_ip $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE && icon='VPN_ICON'
# Check VPN is on if VPN interface is set
if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then
local vpnIp="$(p9k::parseIp "${POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE}" "${ROOT_PREFIX}")"
if [[ -n "$vpnIp" ]]; then
icon='VPN_ICON'
fi
fi
$1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "$icon" 0 '' "${public_ip}"
fi fi
$1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "$icon" 0 '' "${ip//\%/%%}"
} }
################################################################ ################################################################
@ -1014,12 +984,8 @@ prompt_icons_test() {
# Segment to display the current IP address # Segment to display the current IP address
set_default POWERLEVEL9K_IP_INTERFACE "^[^ ]+" set_default POWERLEVEL9K_IP_INTERFACE "^[^ ]+"
prompt_ip() { prompt_ip() {
local ROOT_PREFIX="${4}" _p9k_parse_ip $POWERLEVEL9K_IP_INTERFACE || return
local ip=$(p9k::parseIp "${POWERLEVEL9K_IP_INTERFACE}" "${ROOT_PREFIX}") "$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" 'NETWORK_ICON' 0 '' "${_P9K_RETVAL//\%/%%}"
if [[ -n "$ip" ]]; then
"$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" 'NETWORK_ICON' 0 '' "${ip//\%/%%}"
fi
} }
################################################################ ################################################################
@ -1027,12 +993,8 @@ prompt_ip() {
set_default POWERLEVEL9K_VPN_IP_INTERFACE "tun" set_default POWERLEVEL9K_VPN_IP_INTERFACE "tun"
# prompt if vpn active # prompt if vpn active
prompt_vpn_ip() { prompt_vpn_ip() {
local ROOT_PREFIX="${4}" _p9k_parse_ip $POWERLEVEL9K_VPN_IP_INTERFACE || return
local ip=$(p9k::parseIp "${POWERLEVEL9K_VPN_IP_INTERFACE}" "${ROOT_PREFIX}") "$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" 'VPN_ICON' 0 '' "${_P9K_RETVAL//\%/%%}"
if [[ -n "${ip}" ]]; then
"$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" 'VPN_ICON' 0 '' "${ip//\%/%%}"
fi
} }
################################################################ ################################################################