Skip to content

Commit 6a0d43b

Browse files
Merge pull request #338 from luftaquila/feature/cache-public-ip
Cache ip address in network-public-ip plugin
2 parents 00c7479 + 15e41b2 commit 6a0d43b

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

scripts/network-public-ip.sh

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@
33
# setting the locale, some users have issues with different locales, this forces the correct one
44
export LC_ALL=en_US.UTF-8
55

6-
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7-
source $current_dir/utils.sh
6+
DATAFILE="/tmp/.dracula-tmux-public-ip-data"
7+
LAST_EXEC_FILE="/tmp/.dracula-tmux-public-ip-last-exec"
8+
INTERVAL=1200
89

910
main() {
10-
IP_SERVER="ifconfig.me"
11-
ip=$(curl -s "$IP_SERVER")
11+
local _current_dir _last _now
12+
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
13+
_last=$(cat "$LAST_EXEC_FILE" 2>/dev/null || echo 0)
14+
_now=$(date +%s)
1215

13-
IP_LABEL=$(get_tmux_option "@dracula-network-public-ip-label" "")
14-
echo "$IP_LABEL $ip"
16+
source $current_dir/utils.sh
17+
18+
if (((_now - _last) > INTERVAL)); then
19+
IP_SERVER="ifconfig.me"
20+
ip=$(curl -s "$IP_SERVER")
21+
22+
echo "$(get_tmux_option "@dracula-network-public-ip-label" "")$ip" > "${DATAFILE}"
23+
printf '%s' "$_now" > "${LAST_EXEC_FILE}"
24+
fi
25+
26+
cat "${DATAFILE}"
1527
}
1628

1729
# run the main driver

scripts/weather_wrapper.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# setting the locale, some users have issues with different locales, this forces the correct one
33
export LC_ALL=en_US.UTF-8
44

5-
DATAFILE="/tmp/.dracula-tmux-data"
5+
DATAFILE="/tmp/.dracula-tmux-weather-data"
66
LAST_EXEC_FILE="/tmp/.dracula-tmux-weather-last-exec"
77
INTERVAL=1200
88

0 commit comments

Comments
 (0)