From 29233c1963ce85241972e7962b92c73535e4a1d6 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Fri, 30 Dec 2016 14:04:18 -0700 Subject: [PATCH 01/20] added public IP segment for review/discussion --- functions/icons.zsh | 5 ++++- powerlevel9k.zsh-theme | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/functions/icons.zsh b/functions/icons.zsh index 06d6ab20..b13e3463 100644 --- a/functions/icons.zsh +++ b/functions/icons.zsh @@ -76,6 +76,7 @@ case $POWERLEVEL9K_MODE in RUST_ICON '' PYTHON_ICON $'\U1F40D' # 🐍 SWIFT_ICON '' + PUBLIC_IP_ICON '' ) ;; 'awesome-fontconfig') @@ -131,9 +132,10 @@ case $POWERLEVEL9K_MODE in VCS_GIT_GITLAB_ICON $'\uF296 ' #  VCS_HG_ICON $'\uF0C3 ' #  VCS_SVN_ICON '(svn) ' - RUST_ICON $'\uE6A8' #  + RUST_ICON $'\uE6A8' #  PYTHON_ICON $'\U1F40D' # 🐍 SWIFT_ICON '' + PUBLIC_IP_ICON '' ) ;; *) @@ -192,6 +194,7 @@ case $POWERLEVEL9K_MODE in RUST_ICON '' PYTHON_ICON '' SWIFT_ICON 'Swift' + PUBLIC_IP_ICON '' ) ;; esac diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 9ea12aa7..34d1f640 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -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) + + if [[ -n $public_ip ]]; then + $1_prompt_segment "$0" "$2" "black" "249" "${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() { From 2ddf2a2f2039e6f800be620e5d82b94c68019364 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Fri, 30 Dec 2016 14:09:43 -0700 Subject: [PATCH 02/20] fixed colors to defaults --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 34d1f640..65d3b11d 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -467,7 +467,7 @@ prompt_public_ip() { local public_ip=$(cat $POWERLEVEL9K_PUBLIC_IP_FILE) if [[ -n $public_ip ]]; then - $1_prompt_segment "$0" "$2" "black" "249" "${public_ip}" 'PUBLIC_IP_ICON' + $1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" 'PUBLIC_IP_ICON' fi } From fa21bfff4595eb9451917ed219d29b83ea74c2e4 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Fri, 30 Dec 2016 14:10:43 -0700 Subject: [PATCH 03/20] reverted trailing space edit --- functions/icons.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/icons.zsh b/functions/icons.zsh index b13e3463..77015f0b 100644 --- a/functions/icons.zsh +++ b/functions/icons.zsh @@ -132,7 +132,7 @@ case $POWERLEVEL9K_MODE in VCS_GIT_GITLAB_ICON $'\uF296 ' #  VCS_HG_ICON $'\uF0C3 ' #  VCS_SVN_ICON '(svn) ' - RUST_ICON $'\uE6A8' #  + RUST_ICON $'\uE6A8' #  PYTHON_ICON $'\U1F40D' # 🐍 SWIFT_ICON '' PUBLIC_IP_ICON '' From cf88f861fa29d0d78ca08c7f831717945abb94eb Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Sun, 1 Jan 2017 23:47:01 -0700 Subject: [PATCH 04/20] fixed boolean values --- powerlevel9k.zsh-theme | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 65d3b11d..437a1fb1 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -433,21 +433,21 @@ prompt_public_ip() { set_default POWERLEVEL9K_PUBLIC_IP_HOST "http://ident.me" # Do we need a fresh IP? - local refresh_ip=FALSE + 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 + [[ $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 + [[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) ]] && refresh_ip=true else - touch $POWERLEVEL9K_PUBLIC_IP_FILE && refresh_ip=TRUE + 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 [[ $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 From 5fdfd63e467853a0b69267d8426fa99005b59b17 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Fri, 30 Dec 2016 14:04:18 -0700 Subject: [PATCH 05/20] added public IP segment for review/discussion --- functions/icons.zsh | 5 ++++- powerlevel9k.zsh-theme | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/functions/icons.zsh b/functions/icons.zsh index 06d6ab20..b13e3463 100644 --- a/functions/icons.zsh +++ b/functions/icons.zsh @@ -76,6 +76,7 @@ case $POWERLEVEL9K_MODE in RUST_ICON '' PYTHON_ICON $'\U1F40D' # 🐍 SWIFT_ICON '' + PUBLIC_IP_ICON '' ) ;; 'awesome-fontconfig') @@ -131,9 +132,10 @@ case $POWERLEVEL9K_MODE in VCS_GIT_GITLAB_ICON $'\uF296 ' #  VCS_HG_ICON $'\uF0C3 ' #  VCS_SVN_ICON '(svn) ' - RUST_ICON $'\uE6A8' #  + RUST_ICON $'\uE6A8' #  PYTHON_ICON $'\U1F40D' # 🐍 SWIFT_ICON '' + PUBLIC_IP_ICON '' ) ;; *) @@ -192,6 +194,7 @@ case $POWERLEVEL9K_MODE in RUST_ICON '' PYTHON_ICON '' SWIFT_ICON 'Swift' + PUBLIC_IP_ICON '' ) ;; esac diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 9e89f68d..dd0ad0ec 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -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) + + if [[ -n $public_ip ]]; then + $1_prompt_segment "$0" "$2" "black" "249" "${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() { From a6c8c5c2dd62474095d9213f34193bb0a3d5b9dd Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Fri, 30 Dec 2016 14:09:43 -0700 Subject: [PATCH 06/20] fixed colors to defaults --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index dd0ad0ec..dfc37d2c 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -467,7 +467,7 @@ prompt_public_ip() { local public_ip=$(cat $POWERLEVEL9K_PUBLIC_IP_FILE) if [[ -n $public_ip ]]; then - $1_prompt_segment "$0" "$2" "black" "249" "${public_ip}" 'PUBLIC_IP_ICON' + $1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" 'PUBLIC_IP_ICON' fi } From 0682105ae8baafd695b58094f8e44a589cc9d236 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Fri, 30 Dec 2016 14:10:43 -0700 Subject: [PATCH 07/20] reverted trailing space edit --- functions/icons.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/icons.zsh b/functions/icons.zsh index b13e3463..77015f0b 100644 --- a/functions/icons.zsh +++ b/functions/icons.zsh @@ -132,7 +132,7 @@ case $POWERLEVEL9K_MODE in VCS_GIT_GITLAB_ICON $'\uF296 ' #  VCS_HG_ICON $'\uF0C3 ' #  VCS_SVN_ICON '(svn) ' - RUST_ICON $'\uE6A8' #  + RUST_ICON $'\uE6A8' #  PYTHON_ICON $'\U1F40D' # 🐍 SWIFT_ICON '' PUBLIC_IP_ICON '' From 5cf78c5a1314967b86e37dc330f889e3c5cba805 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Sun, 1 Jan 2017 23:47:01 -0700 Subject: [PATCH 08/20] fixed boolean values --- powerlevel9k.zsh-theme | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index dfc37d2c..f5c810bc 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -433,21 +433,21 @@ prompt_public_ip() { set_default POWERLEVEL9K_PUBLIC_IP_HOST "http://ident.me" # Do we need a fresh IP? - local refresh_ip=FALSE + 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 + [[ $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 + [[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) ]] && refresh_ip=true else - touch $POWERLEVEL9K_PUBLIC_IP_FILE && refresh_ip=TRUE + 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 [[ $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 From fa51798ce76fd5e50ac7be2fee333e090697ef17 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Tue, 3 Jan 2017 16:05:15 -0700 Subject: [PATCH 09/20] fixed some comments and handled an edge case --- powerlevel9k.zsh-theme | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f5c810bc..dd57ac05 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -460,10 +460,11 @@ prompt_public_ip() { 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 + # write IP to tmp file or touch tmp file if an IP was not retrieved + [[ -n $fresh_ip ]] && echo $fresh_ip > $POWERLEVEL9K_PUBLIC_IP_FILE || touch $POWERLEVEL9K_PUBLIC_IP_FILE fi - # write IP to tmp file + # read public IP saved to tmp file local public_ip=$(cat $POWERLEVEL9K_PUBLIC_IP_FILE) if [[ -n $public_ip ]]; then From 6c46410a2f300a015254b3662b06ab572815c9f3 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Tue, 3 Jan 2017 16:55:33 -0700 Subject: [PATCH 10/20] updated some comments changed some offline behavior --- powerlevel9k.zsh-theme | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 3693f02d..22e61d62 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -436,11 +436,10 @@ prompt_public_ip() { local refresh_ip=false if [[ -f $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then typeset -i timediff + # if saved IP is more than 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 + [[ $timediff -gt $POWERLEVEL9K_PUBLIC_IP_TIMOUT ]] && refresh_ip=true + # If tmp file is empty get a fresh IP [[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) ]] && refresh_ip=true else touch $POWERLEVEL9K_PUBLIC_IP_FILE && refresh_ip=true @@ -461,8 +460,8 @@ prompt_public_ip() { fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" fi - # write IP to tmp file or touch tmp file if an IP was not retrieved - [[ -n $fresh_ip ]] && echo $fresh_ip > $POWERLEVEL9K_PUBLIC_IP_FILE || touch $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_FILE fi # read public IP saved to tmp file From 1b838241fb5fe6628a00acae7d154c51615b6458 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Tue, 3 Jan 2017 17:07:52 -0700 Subject: [PATCH 11/20] added configurable string when there is no IP --- powerlevel9k.zsh-theme | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 22e61d62..193a1a85 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -429,6 +429,7 @@ prompt_battery() { prompt_public_ip() { # set default values for segment 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_HOST "http://ident.me" @@ -440,7 +441,7 @@ prompt_public_ip() { timediff=$(($(date +%s) - $(date -r $POWERLEVEL9K_PUBLIC_IP_FILE +%s))) [[ $timediff -gt $POWERLEVEL9K_PUBLIC_IP_TIMOUT ]] && refresh_ip=true # If tmp file is empty get a fresh IP - [[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) ]] && refresh_ip=true + [[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) || $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) =~ $POWERLEVEL9K_PUBLIC_IP_NONE ]] && refresh_ip=true else touch $POWERLEVEL9K_PUBLIC_IP_FILE && refresh_ip=true fi @@ -461,7 +462,7 @@ prompt_public_ip() { fi # 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_FILE + [[ -n $fresh_ip ]] && echo $fresh_ip > $POWERLEVEL9K_PUBLIC_IP_FILE || echo $POWERLEVEL9K_PUBLIC_IP_NONE > $POWERLEVEL9K_PUBLIC_IP_FILE fi # read public IP saved to tmp file From 20a5556642c4cca23d4b65e05fa64999c492a13d Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Mon, 9 Jan 2017 14:41:46 -0700 Subject: [PATCH 12/20] added the ability to specify and only attempt a particular method --- powerlevel9k.zsh-theme | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 437a1fb1..ef945613 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -448,17 +448,42 @@ prompt_public_ip() { # 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 + # if method specified, don't use fallback methods + if [[ -n $POWERLEVEL9K_PUBLIC_IP_METHOD ]] && [[ $POWERLEVEL9K_PUBLIC_IP_METHOD =~ 'wget|curl|dig' ]]; then + local method=$POWERLEVEL9K_PUBLIC_IP_METHOD fi + if [[ -n $method ]]; then + case $method in + 'dig') + 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 + ;; + 'curl') + 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 + ;; + 'wget') + if [[ -z "$fresh_ip" ]] && type -p wget >/dev/null; then + fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" + fi + ;; + esac + else + 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 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)" + if [[ -z "$fresh_ip" ]] && type -p wget >/dev/null; then + fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" + fi fi [[ -n $fresh_ip ]] && echo $fresh_ip > $POWERLEVEL9K_PUBLIC_IP_FILE fi From 6e1e4ac241fcc113170f28656c99ef9b821aba9a Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Mon, 9 Jan 2017 14:53:28 -0700 Subject: [PATCH 13/20] added README section for public_ip segment --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 54b5c519..87fd9182 100644 --- a/README.md +++ b/README.md @@ -315,6 +315,29 @@ specify the correct network interface by setting: |----------|---------------|-------------| |`POWERLEVEL9K_IP_INTERFACE`|None|The NIC for which you wish to display the IP address. Example: `eth0`.| +##### public_ip + +This segment will display your public IP address. There are several methods of obtaining this +information and by default it will try all of them starting with the most efficient. You can +also specify which method you would like it to use. The methods available are dig using opendns, +curl, or wget. The host used for wget and curl is http://ident.me by default but can be set to +another host if you prefer. + +The public_ip segment will attempt to update your public IP address every 5 minutes by default(also +configurable by the user). If you lose connection your cached IP address will be displayed until +your timeout expires at which point every time your prompt is generated a new attempt will be made. +Until an IP is successfully pulled the value of $POWERLEVEL9K_PUBLIC_IP_NONE will be displayed for +this segment. If this value is empty(the default)and $POWERLEVEL9K_PUBLIC_IP_FILE is empty the +segment will not be displayed. + +| Variable | Default Value | Description | +|----------|---------------|-------------| +|`POWERLEVEL9K_PUBLIC_IP_FILE`|'/tmp/p8k_public_ip'|This is the file your public IP is cached in.| +|`POWERLEVEL9K_PUBLIC_IP_HOST`|'http://ident.me'|This is the default host to get your public IP.| +|`POWERLEVEL9K_PUBLIC_IP_TIMOUT`|300|The amount of time in seconds between refreshing your cached IP.| +|`POWERLEVEL9K_PUBLIC_IP_METHOD`|None|You can set this to any of 'dig', 'curl', or 'wget' to only use that method to refresh your IP.| +|`POWERLEVEL9K_PUBLIC_IP_NONE`|None|The string displayed when an IP was not obtained| + ##### rbenv This segment shows the version of Ruby being used when using `rbenv` to change your current Ruby stack. From f33f843e92c565453b253531d9d7be06faaf42fc Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Mon, 9 Jan 2017 14:56:22 -0700 Subject: [PATCH 14/20] fixed issue with POWERLEVEL9K_PUBLIC_IP_NONE being empty --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 10d1564e..368254ee 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -441,7 +441,7 @@ prompt_public_ip() { timediff=$(($(date +%s) - $(date -r $POWERLEVEL9K_PUBLIC_IP_FILE +%s))) [[ $timediff -gt $POWERLEVEL9K_PUBLIC_IP_TIMOUT ]] && refresh_ip=true # If tmp file is empty get a fresh IP - [[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) || $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) =~ $POWERLEVEL9K_PUBLIC_IP_NONE ]] && refresh_ip=true + [[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) || $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) =~ '$POWERLEVEL9K_PUBLIC_IP_NONE' ]] && refresh_ip=true else touch $POWERLEVEL9K_PUBLIC_IP_FILE && refresh_ip=true fi From f11ca0664c9d9aba9d456b70058ba64909e1e098 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Mon, 9 Jan 2017 15:00:20 -0700 Subject: [PATCH 15/20] fixed issue with POWERLEVEL9K_PUBLIC_IP_NONE being empty for real --- powerlevel9k.zsh-theme | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 368254ee..78b014e5 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -429,7 +429,7 @@ prompt_battery() { prompt_public_ip() { # set default values for segment set_default POWERLEVEL9K_PUBLIC_IP_TIMOUT "300" - set_default POWERLEVEL9K_PUBLIC_IP_NONE "" + set_default POWERLEVEL9K_PUBLIC_IP_NONE "none" set_default POWERLEVEL9K_PUBLIC_IP_FILE "/tmp/p9k_public_ip" set_default POWERLEVEL9K_PUBLIC_IP_HOST "http://ident.me" @@ -441,7 +441,7 @@ prompt_public_ip() { timediff=$(($(date +%s) - $(date -r $POWERLEVEL9K_PUBLIC_IP_FILE +%s))) [[ $timediff -gt $POWERLEVEL9K_PUBLIC_IP_TIMOUT ]] && refresh_ip=true # If tmp file is empty get a fresh IP - [[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) || $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) =~ '$POWERLEVEL9K_PUBLIC_IP_NONE' ]] && refresh_ip=true + [[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) || $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) =~ "$POWERLEVEL9K_PUBLIC_IP_NONE" ]] && refresh_ip=true else touch $POWERLEVEL9K_PUBLIC_IP_FILE && refresh_ip=true fi From f5b544f4c8229291c83d7c9feef4546544ae5372 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Mon, 9 Jan 2017 15:06:14 -0700 Subject: [PATCH 16/20] added link to segemnt documenation in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 87fd9182..ee1648f8 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ The segments that are currently available are: * [`dir`](#dir) - Your current working directory. * `history` - The command number for the current line. * [`ip`](#ip) - Shows the current IP address. +* [`public_ip`](#public_ip) - Shows your public IP address. * `load` - Your machine's load averages. * `os_icon` - Display a nice little icon, depending on your operating system. * `ram` - Show free RAM. From 2cc6e661b473339e8f39489f879b2f19a2ebf856 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Mon, 9 Jan 2017 15:14:05 -0700 Subject: [PATCH 17/20] reverted testing value to default value --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 78b014e5..05bf3d14 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -429,7 +429,7 @@ prompt_battery() { prompt_public_ip() { # set default values for segment set_default POWERLEVEL9K_PUBLIC_IP_TIMOUT "300" - set_default POWERLEVEL9K_PUBLIC_IP_NONE "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" From 2cb1f84ce7fa135c480712e684d67c03299a3615 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Mon, 9 Jan 2017 15:24:50 -0700 Subject: [PATCH 18/20] fixed typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ee1648f8..2ad799c4 100644 --- a/README.md +++ b/README.md @@ -335,7 +335,7 @@ segment will not be displayed. |----------|---------------|-------------| |`POWERLEVEL9K_PUBLIC_IP_FILE`|'/tmp/p8k_public_ip'|This is the file your public IP is cached in.| |`POWERLEVEL9K_PUBLIC_IP_HOST`|'http://ident.me'|This is the default host to get your public IP.| -|`POWERLEVEL9K_PUBLIC_IP_TIMOUT`|300|The amount of time in seconds between refreshing your cached IP.| +|`POWERLEVEL9K_PUBLIC_IP_TIEMOUT`|300|The amount of time in seconds between refreshing your cached IP.| |`POWERLEVEL9K_PUBLIC_IP_METHOD`|None|You can set this to any of 'dig', 'curl', or 'wget' to only use that method to refresh your IP.| |`POWERLEVEL9K_PUBLIC_IP_NONE`|None|The string displayed when an IP was not obtained| From 108335f18f95fd9cbd63157699925202b4df72d9 Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Mon, 9 Jan 2017 15:25:02 -0700 Subject: [PATCH 19/20] fixed typo fixed issue with POWERLEVEL9K_PUBLIC_IP_NONE being empty .. i hope --- powerlevel9k.zsh-theme | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 05bf3d14..95a2a5ea 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -428,7 +428,7 @@ prompt_battery() { prompt_public_ip() { # set default values for segment - set_default POWERLEVEL9K_PUBLIC_IP_TIMOUT "300" + set_default POWERLEVEL9K_PUBLIC_IP_TIMEOUT "300" 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" @@ -439,9 +439,10 @@ prompt_public_ip() { typeset -i timediff # if saved IP is more than timediff=$(($(date +%s) - $(date -r $POWERLEVEL9K_PUBLIC_IP_FILE +%s))) - [[ $timediff -gt $POWERLEVEL9K_PUBLIC_IP_TIMOUT ]] && refresh_ip=true + [[ $timediff -gt $POWERLEVEL9K_PUBLIC_IP_TIMEOUT ]] && refresh_ip=true # If tmp file is empty get a fresh IP - [[ -z $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) || $(cat $POWERLEVEL9K_PUBLIC_IP_FILE) =~ "$POWERLEVEL9K_PUBLIC_IP_NONE" ]] && refresh_ip=true + [[ -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 From 6321dd99c5e6050b24598cca8b37d54f520a6ffd Mon Sep 17 00:00:00 2001 From: rjorgenson Date: Mon, 9 Jan 2017 15:30:15 -0700 Subject: [PATCH 20/20] more typos --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2ad799c4..ac0f86f4 100644 --- a/README.md +++ b/README.md @@ -335,7 +335,7 @@ segment will not be displayed. |----------|---------------|-------------| |`POWERLEVEL9K_PUBLIC_IP_FILE`|'/tmp/p8k_public_ip'|This is the file your public IP is cached in.| |`POWERLEVEL9K_PUBLIC_IP_HOST`|'http://ident.me'|This is the default host to get your public IP.| -|`POWERLEVEL9K_PUBLIC_IP_TIEMOUT`|300|The amount of time in seconds between refreshing your cached IP.| +|`POWERLEVEL9K_PUBLIC_IP_TIMEOUT`|300|The amount of time in seconds between refreshing your cached IP.| |`POWERLEVEL9K_PUBLIC_IP_METHOD`|None|You can set this to any of 'dig', 'curl', or 'wget' to only use that method to refresh your IP.| |`POWERLEVEL9K_PUBLIC_IP_NONE`|None|The string displayed when an IP was not obtained|