Merge branch 'public_ip_segment' of github.com:rjorgenson/powerlevel9k into public_ip_segment

pull/22/head
rjorgenson 8 years ago
commit ceb5c4ec32

@ -240,9 +240,17 @@ it, but only display it if you are not your normal user or on a remote host
To use this feature, make sure the `context` segment is enabled in your prompt To use this feature, make sure the `context` segment is enabled in your prompt
elements (it is by default), and define a `DEFAULT_USER` in your `~/.zshrc`: elements (it is by default), and define a `DEFAULT_USER` in your `~/.zshrc`:
You can set the `POWERLEVEL9K_CONTEXT_HOST_DEPTH` variable to change how the
hostname is displayed. See (ZSH Manual)[http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Login-information]
for details. Default is set to %m which will show the hostname up to the first .
You can set it to %{N}m where N is an integer to show that many segments of system
hostname. Setting N to a negative integer will show that many segments from the
end of the hostname.
| Variable | Default Value | Description | | Variable | Default Value | Description |
|----------|---------------|-------------| |----------|---------------|-------------|
|`DEFAULT_USER`|None|Username to consider a "default context" (you can also use `$USER`)| |`DEFAULT_USER`|None|Username to consider a "default context" (you can also use `$USER`)|
|`POWERLEVEL9K_CONTEXT_HOST_DEPTH`|%m|Customizable host depth on prompt|
##### dir ##### dir

@ -429,6 +429,7 @@ prompt_battery() {
prompt_public_ip() { prompt_public_ip() {
# set default values for segment # set default values for segment
set_default POWERLEVEL9K_PUBLIC_IP_TIMOUT "300" set_default POWERLEVEL9K_PUBLIC_IP_TIMOUT "300"
set_default POWERLEVEL9K_PUBLIC_IP_NONE ""
set_default POWERLEVEL9K_PUBLIC_IP_FILE "/tmp/p9k_public_ip" 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"
@ -436,12 +437,11 @@ prompt_public_ip() {
local refresh_ip=false local refresh_ip=false
if [[ -f $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then if [[ -f $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then
typeset -i timediff typeset -i timediff
# if saved IP is more than
timediff=$(($(date +%s) - $(date -r $POWERLEVEL9K_PUBLIC_IP_FILE +%s))) timediff=$(($(date +%s) - $(date -r $POWERLEVEL9K_PUBLIC_IP_FILE +%s)))
[[ $timediff -gt '500' ]] && refresh_ip=true [[ $timediff -gt $POWERLEVEL9K_PUBLIC_IP_TIMOUT ]] && refresh_ip=true
# this will run the IP refresh with each new prompt while disconnected # If tmp file is empty get a fresh IP
# but will get a new IP immediately once reconnected rather than waiting [[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) || $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) =~ $POWERLEVEL9K_PUBLIC_IP_NONE ]] && refresh_ip=true
# for the timeout, not sure if this is ideal behavior or not
[[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) ]] && refresh_ip=true
else else
touch $POWERLEVEL9K_PUBLIC_IP_FILE && refresh_ip=true touch $POWERLEVEL9K_PUBLIC_IP_FILE && refresh_ip=true
fi fi
@ -485,10 +485,12 @@ prompt_public_ip() {
fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)"
fi fi
fi fi
[[ -n $fresh_ip ]] && echo $fresh_ip > $POWERLEVEL9K_PUBLIC_IP_FILE
# write IP to tmp file or clear tmp file if an IP was not retrieved
[[ -n $fresh_ip ]] && echo $fresh_ip > $POWERLEVEL9K_PUBLIC_IP_FILE || echo $POWERLEVEL9K_PUBLIC_IP_NONE > $POWERLEVEL9K_PUBLIC_IP_FILE
fi fi
# write IP to tmp file # read public IP saved to tmp file
local public_ip=$(cat $POWERLEVEL9K_PUBLIC_IP_FILE) local public_ip=$(cat $POWERLEVEL9K_PUBLIC_IP_FILE)
if [[ -n $public_ip ]]; then if [[ -n $public_ip ]]; then
@ -499,12 +501,13 @@ prompt_public_ip() {
# Context: user@hostname (who am I and where am I) # Context: user@hostname (who am I and where am I)
# Note that if $DEFAULT_USER is not set, this prompt segment will always print # Note that if $DEFAULT_USER is not set, this prompt segment will always print
prompt_context() { prompt_context() {
set_default POWERLEVEL9K_CONTEXT_HOST_DEPTH "%m"
if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
if [[ $(print -P "%#") == '#' ]]; then if [[ $(print -P "%#") == '#' ]]; then
# Shell runs as root # Shell runs as root
"$1_prompt_segment" "$0_ROOT" "$2" "$DEFAULT_COLOR" "yellow" "$USER@%m" "$1_prompt_segment" "$0_ROOT" "$2" "$DEFAULT_COLOR" "yellow" "$USER@$POWERLEVEL9K_CONTEXT_HOST_DEPTH"
else else
"$1_prompt_segment" "$0_DEFAULT" "$2" "$DEFAULT_COLOR" "011" "$USER@%m" "$1_prompt_segment" "$0_DEFAULT" "$2" "$DEFAULT_COLOR" "011" "$USER@$POWERLEVEL9K_CONTEXT_HOST_DEPTH"
fi fi
fi fi
} }

Loading…
Cancel
Save