Skip to content

Added "auto" option for the weather section #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 14, 2025
Merged

Conversation

jackdeye
Copy link
Contributor

Added the option to put "auto" in the latitude and longitude section of the theme, and if set, will query an api to get your location with your ip address.
Implements caching and a backup api call if the first does not work.

jackdeye and others added 2 commits April 11, 2025 22:11
Added the option to put "auto" in the latitude and longitude section of
the theme, and if set, will query an api to get your location with your
ip address.
Implements caching and a backup api call if the first does not work.
Copy link
Owner

@erikw erikw left a comment

Choose a reason for hiding this comment

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

Nice idea, I like it!

I checked out the branch but unfortuantely it did not work for me. I set both long/lat config to auto.

I see in the /tmp/.weather_location_cache the following:

set -x
exec 2>/tmp/tmux-powerline.log
<code to debug>
set +x

Then I inserted these set -x debug commands around the call to the function get_auto_location() to see the execution of this function:

++ get_auto_location
++ local cache_file=/tmp/.weather_location_cache
++ local max_cache_age=86400
++ [[ -f /tmp/.weather_location_cache ]]
++ local location_data
++ apis=("https://ipapi.co/json" "https://ipinfo.io/json")
++ local -a apis
++ for api in "${apis[@]}"
+++ curl --max-time 4 -s https://ipapi.co/json
++ location_data='{
"error": true,
"reason": "RateLimited",
"message": "Visit https://ipapi.co/ratelimited/ for details"
}'
++ case "$api" in
+++ echo '{
"error": true,
"reason": "RateLimited",
"message": "Visit https://ipapi.co/ratelimited/ for details"
}'
+++ jq -r .latitude
++ TMUX_POWERLINE_SEG_WEATHER_LAT=null
+++ echo '{
"error": true,
"reason": "RateLimited",
"message": "Visit https://ipapi.co/ratelimited/ for details"
}'
+++ jq -r .longitude
++ TMUX_POWERLINE_SEG_WEATHER_LON=null
++ [[ -n null ]]
++ [[ -n null ]]
+++ dirname /tmp/.weather_location_cache
++ mkdir -p /tmp
++ echo 'export TMUX_POWERLINE_SEG_WEATHER_LAT='''null''''
++ echo 'export TMUX_POWERLINE_SEG_WEATHER_LON='''null''''
++ return 0
++ set +x
jq: error (at :1): Cannot index number with string "properties"
parse error: Invalid numeric literal at line 1, column 8
jq: error (at :1): Cannot index number with string "properties"
parse error: Invalid numeric literal at line 1, column 8

Looks like ratelimiting. If I visit in my browser https://ipapi.co/json it works.

@@ -179,3 +184,48 @@ __read_tmp_file() {
cat "${tmp_file}"
exit
}

get_auto_location() {
local cache_file="${TMUX_POWERLINE_DIR_USER:-/tmp}/.weather_location_cache"
Copy link
Owner

Choose a reason for hiding this comment

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

In other segments $TMUX_POWERLINE_DIR_TEMPORARY is used from

export TMUX_POWERLINE_DIR_TEMPORARY="${TMPDIR:-/tmp/tmux-powerline_${USER}}"
. It would make sense to follow this pattern :)

Also removed the bash arrays to be more compatible with older versions
of bash
@jackdeye
Copy link
Contributor Author

Thank you for checking this out!
I updated the temporary directory structure to follow that pattern. I'm hoping the rate-limiting issue results from the cached file being unable to be located on your machine. I also removed the bash array to make it more accessible to older versions of bash.

Copy link
Owner

@erikw erikw left a comment

Choose a reason for hiding this comment

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

Thanks for the update. I tried it again and now it works for me, no more rate limiting :-)

@erikw erikw merged commit 9e98e7a into erikw:main Apr 14, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants