This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 947
Public ip segment #368
Merged
Merged
Public ip segment #368
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 2ddf2a2
fixed colors to defaults
rjorgenson fa21bff
reverted trailing space edit
rjorgenson cf88f86
fixed boolean values
rjorgenson 5fdfd63
added public IP segment for review/discussion
rjorgenson a6c8c5c
fixed colors to defaults
rjorgenson 0682105
reverted trailing space edit
rjorgenson 5cf78c5
fixed boolean values
rjorgenson fa51798
fixed some comments and handled an edge case
rjorgenson f9bb7a2
Merge branch 'public_ip_segment' of github.com:rjorgenson/powerlevel9…
rjorgenson 6c46410
updated some comments
rjorgenson 1b83824
added configurable string when there is no IP
rjorgenson 20a5556
added the ability to specify and only attempt a particular method
rjorgenson ceb5c4e
Merge branch 'public_ip_segment' of github.com:rjorgenson/powerlevel9…
rjorgenson 6e1e4ac
added README section for public_ip segment
rjorgenson f33f843
fixed issue with POWERLEVEL9K_PUBLIC_IP_NONE being empty
rjorgenson f11ca06
fixed issue with POWERLEVEL9K_PUBLIC_IP_NONE being empty for real
rjorgenson f5b544f
added link to segemnt documenation in README
rjorgenson 2cc6e66
reverted testing value to default value
rjorgenson 2cb1f84
fixed typo
rjorgenson 108335f
fixed typo
rjorgenson 6321dd9
more typos
rjorgenson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
[[ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
What you described, above, achieves that. I think if you update the comments to reflect the actual behavior, we're good to go.
There was a problem hiding this comment.
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.