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

Public ip segment #368

Merged
merged 22 commits into from
Jan 21, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
29233c1
added public IP segment for review/discussion
rjorgenson Dec 30, 2016
2ddf2a2
fixed colors to defaults
rjorgenson Dec 30, 2016
fa21bff
reverted trailing space edit
rjorgenson Dec 30, 2016
cf88f86
fixed boolean values
rjorgenson Jan 2, 2017
5fdfd63
added public IP segment for review/discussion
rjorgenson Dec 30, 2016
a6c8c5c
fixed colors to defaults
rjorgenson Dec 30, 2016
0682105
reverted trailing space edit
rjorgenson Dec 30, 2016
5cf78c5
fixed boolean values
rjorgenson Jan 2, 2017
fa51798
fixed some comments and handled an edge case
rjorgenson Jan 3, 2017
f9bb7a2
Merge branch 'public_ip_segment' of github.com:rjorgenson/powerlevel9…
rjorgenson Jan 3, 2017
6c46410
updated some comments
rjorgenson Jan 3, 2017
1b83824
added configurable string when there is no IP
rjorgenson Jan 4, 2017
20a5556
added the ability to specify and only attempt a particular method
rjorgenson Jan 9, 2017
ceb5c4e
Merge branch 'public_ip_segment' of github.com:rjorgenson/powerlevel9…
rjorgenson Jan 9, 2017
6e1e4ac
added README section for public_ip segment
rjorgenson Jan 9, 2017
f33f843
fixed issue with POWERLEVEL9K_PUBLIC_IP_NONE being empty
rjorgenson Jan 9, 2017
f11ca06
fixed issue with POWERLEVEL9K_PUBLIC_IP_NONE being empty for real
rjorgenson Jan 9, 2017
f5b544f
added link to segemnt documenation in README
rjorgenson Jan 9, 2017
2cc6e66
reverted testing value to default value
rjorgenson Jan 9, 2017
2cb1f84
fixed typo
rjorgenson Jan 9, 2017
108335f
fixed typo
rjorgenson Jan 9, 2017
6321dd9
more typos
rjorgenson Jan 9, 2017
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
3 changes: 3 additions & 0 deletions functions/icons.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ case $POWERLEVEL9K_MODE in
RUST_ICON ''
PYTHON_ICON $'\U1F40D' # 🐍
SWIFT_ICON ''
PUBLIC_IP_ICON ''
)
;;
'awesome-fontconfig')
Expand Down Expand Up @@ -134,6 +135,7 @@ case $POWERLEVEL9K_MODE in
RUST_ICON $'\uE6A8' # 
PYTHON_ICON $'\U1F40D' # 🐍
SWIFT_ICON ''
PUBLIC_IP_ICON ''
)
;;
*)
Expand Down Expand Up @@ -192,6 +194,7 @@ case $POWERLEVEL9K_MODE in
RUST_ICON ''
PYTHON_ICON ''
SWIFT_ICON 'Swift'
PUBLIC_IP_ICON ''
)
;;
esac
Expand Down
45 changes: 45 additions & 0 deletions powerlevel9k.zsh-theme
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,51 @@ prompt_battery() {
fi
}

prompt_public_ip() {
# set default values for segment
set_default POWERLEVEL9K_PUBLIC_IP_TIMOUT "300"
set_default POWERLEVEL9K_PUBLIC_IP_FILE "/tmp/p9k_public_ip"
set_default POWERLEVEL9K_PUBLIC_IP_HOST "http://ident.me"

# Do we need a fresh IP?
local refresh_ip=false
if [[ -f $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then
typeset -i timediff
timediff=$(($(date +%s) - $(date -r $POWERLEVEL9K_PUBLIC_IP_FILE +%s)))
[[ $timediff -gt '500' ]] && refresh_ip=true
# this will run the IP refresh with each new prompt while disconnected
# but will get a new IP immediately once reconnected rather than waiting
# for the timeout, not sure if this is ideal behavior or not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good default behavior - let's roll with it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I forgot to reread this section before pushing, I added a conditional to line 463 which makes this not behave exactly like this anymore. As it's written currently it will always create the tmp file if it doesn't exist and it will always check for a new ip if the tmp file is empty but it will no longer overwrite the tmp file with an empty result. If a fresh ip grab is attempted and none of the checks are successful then it will leave the tmp file alone. For example your connection to the internet dies, the segment will continue on doing nothing until the timeout is reached, at which point the prompt will attempt to grab a new IP every time the prompt segment is generated. I like this behavior but I think maybe we could add a touch to the file if the check is unsuccessful, that way it will still only check every X seconds even when your connection is down. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good with that plan. I think the important behaviors are that:

  • If the connection is lost, on the next update, the segment no longer displays an IP.
  • If the connection comes back, the user does not need to manually kick the segment to bring the IP back.

What you described, above, achieves that. I think if you update the comments to reflect the actual behavior, we're good to go.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it should actually still display your last IP once you lose connection. I like that though. let me see what I can do to get that functionality in. I updated the comments already and added the behavior to touch the file to update it's modified time.

[[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) ]] && refresh_ip=true
else
touch $POWERLEVEL9K_PUBLIC_IP_FILE && refresh_ip=true
fi

# grab a fresh IP if needed
if [[ $refresh_ip =~ true && -w $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then
if type -p dig >/dev/null; then
fresh_ip="$(dig +time=1 +tries=1 +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)"
[[ "$fresh_ip" =~ ^\; ]] && unset fresh_ip
fi

if [[ -z "$fresh_ip" ]] && type -p curl >/dev/null; then
fresh_ip="$(curl --max-time 10 -w '\n' "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)"
fi

if [[ -z "$fresh_ip" ]] && type -p wget >/dev/null; then
fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)"
fi
[[ -n $fresh_ip ]] && echo $fresh_ip > $POWERLEVEL9K_PUBLIC_IP_FILE
fi

# write IP to tmp file
local public_ip=$(cat $POWERLEVEL9K_PUBLIC_IP_FILE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be mis-reading this, but the comment on L466 and the code on L467 are confusing me a bit. Doesn't this code just readout the IP, not write it into the file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this comment is supposed to be a few lines up. Will fix, sorry about that =]


if [[ -n $public_ip ]]; then
$1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" 'PUBLIC_IP_ICON'
fi
}

# Context: user@hostname (who am I and where am I)
# Note that if $DEFAULT_USER is not set, this prompt segment will always print
prompt_context() {
Expand Down