31
31
run_segment () {
32
32
local cache_weather_file=" ${TMUX_POWERLINE_DIR_TEMPORARY} /weather_cache_data.txt"
33
33
local cache_location_file=" ${TMUX_POWERLINE_DIR_TEMPORARY} /weather_cache_location.txt"
34
- local weather
34
+ local weather= " "
35
35
36
36
# Check if the weather data is still a valid cache hit
37
37
if [ -f " $cache_weather_file " ]; then
38
38
last_update=$( __read_file_last_update " $cache_weather_file " )
39
39
time_now=$( date +%s)
40
-
41
40
up_to_date=$( echo " (${time_now} -${last_update} ) < ${TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD} " | bc)
42
41
if [ " $up_to_date " -eq 1 ]; then
43
- __read_file_content " $cache_weather_file "
44
- return
42
+ weather=$( __read_file_content " $cache_weather_file " )
45
43
fi
46
44
fi
47
-
48
- __process_settings
49
- case " $TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER " in
50
- " yrno" ) weather=$( __yrno) ;;
51
- * )
52
- echo " Unknown weather provider [$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER ]"
53
- return 1
54
- ;;
55
- esac
56
- if [ -n " $weather " ]; then
57
- echo " $weather "
45
+
46
+ # Fetch from provider if empty
47
+ # If a new provider is implemented, please set the $weather variable!
48
+ if [ -z " $weather " ]; then
49
+ __process_settings
50
+ case " $TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER " in
51
+ " yrno" )
52
+ weather=$( __yrno)
53
+ ;;
54
+ * )
55
+ # Just read the stale cache, default to empty/blank
56
+ # Better not overwriting the previous healthy content
57
+ weather=$( __read_file_content " $cache_weather_file " )
58
+ ;;
59
+ esac
60
+ echo " $weather " > " $cache_weather_file "
58
61
fi
62
+
63
+ echo " $weather "
59
64
}
60
65
61
66
__process_settings () {
@@ -71,57 +76,49 @@ __process_settings() {
71
76
if [ -z " $TMUX_POWERLINE_SEG_WEATHER_LOCATION_UPDATE_PERIOD " ]; then
72
77
export TMUX_POWERLINE_SEG_WEATHER_LOCATION_UPDATE_PERIOD=" ${TMUX_POWERLINE_SEG_WEATHER_LOCATION_UPDATE_PERIOD_DEFAULT} "
73
78
fi
74
- if [ -z " $TMUX_POWERLINE_SEG_WEATHER_GREP " ]; then
75
- export TMUX_POWERLINE_SEG_WEATHER_GREP=" ${TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT} "
76
- fi
77
79
if [ " $TMUX_POWERLINE_SEG_WEATHER_LAT " = " auto" ] || [ " $TMUX_POWERLINE_SEG_WEATHER_LON " = " auto" ] || [ -z " $TMUX_POWERLINE_SEG_WEATHER_LON " ] || [ -z " $TMUX_POWERLINE_SEG_WEATHER_LAT " ]; then
78
80
if ! get_auto_location; then
79
81
exit 8
80
82
fi
81
83
fi
82
84
}
83
85
86
+ # An implementation of a weather provider, just need to echo the result, run_segment() will take care of the rest
84
87
__yrno () {
85
- degree=" "
88
+ local degree=" "
86
89
87
- if [ -z " $degree " ]; then
88
- # There's a chance that you will get rate limited or both location APIs are not working
89
- # Then long and lat will be "null", as literal string
90
- if [ -z " $TMUX_POWERLINE_SEG_WEATHER_LAT " ] || [ -z " $TMUX_POWERLINE_SEG_WEATHER_LON " ] || [ " $TMUX_POWERLINE_SEG_WEATHER_LAT " == null ] || [ " $TMUX_POWERLINE_SEG_WEATHER_LON " == null ]; then
91
- __read_file_content " $cache_weather_file "
92
- return
93
- fi
94
- if weather_data=$( curl --max-time 4 -s " https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=${TMUX_POWERLINE_SEG_WEATHER_LAT} &lon=${TMUX_POWERLINE_SEG_WEATHER_LON} " ) ; then
95
- error=$( echo " $weather_data " | grep -i " error" )
96
- if [ -n " $error " ]; then
97
- echo " error"
98
- return 1
99
- fi
100
- degree=$( echo " $weather_data " | jq -r ' .properties.timeseries | .[0].data.instant.details.air_temperature' )
101
- condition=$( echo " $weather_data " | jq -r ' .properties.timeseries | .[0].data.next_1_hours.summary.symbol_code' )
102
- elif [ -f " ${cache_weather_file} " ]; then
103
- __read_file_content " $cache_weather_file "
104
- return
105
- fi
90
+ # There's a chance that you will get rate limited or both location APIs are not working
91
+ # Then long and lat will be "null", as literal string
92
+ if [ -z " $TMUX_POWERLINE_SEG_WEATHER_LAT " ] || [ -z " $TMUX_POWERLINE_SEG_WEATHER_LON " ]; then
93
+ echo " Err: Unable to auto-detect your location"
94
+ return 1
106
95
fi
107
96
108
- if [ -n " $degree " ]; then
109
- if [ " $TMUX_POWERLINE_SEG_WEATHER_UNIT " == " k" ]; then
110
- degree=$( echo " ${degree} + 273.15" | bc)
111
- fi
112
- if [ " $TMUX_POWERLINE_SEG_WEATHER_UNIT " == " f" ]; then
113
- degree=$( echo " ${degree} * 9 / 5 + 32" | bc)
97
+ if weather_data=$( curl --max-time 4 -s " https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=${TMUX_POWERLINE_SEG_WEATHER_LAT} &lon=${TMUX_POWERLINE_SEG_WEATHER_LON} " ) ; then
98
+ error=$( echo " $weather_data " | grep -i " error" )
99
+ if [ -n " $error " ]; then
100
+ echo " error"
101
+ return 1
114
102
fi
115
- # condition_symbol=$(__get_yrno_condition_symbol "$condition" "$sunrise" "$sunset")
116
- condition_symbol=$( __get_yrno_condition_symbol " $condition " )
117
- # Write the <content @ date>, separated by 2 spaces and @, so we can fetch it later on without having to call 'stat'
118
- echo " ${condition_symbol} ${degree} °$( echo " $TMUX_POWERLINE_SEG_WEATHER_UNIT " | tr ' [:lower:]' ' [:upper:]' ) @$( date +%s) " > " $cache_weather_file "
119
- __read_file_content " $cache_weather_file "
120
- return
103
+ degree=$( echo " $weather_data " | jq -r ' .properties.timeseries | .[0].data.instant.details.air_temperature' )
104
+ condition=$( echo " $weather_data " | jq -r ' .properties.timeseries | .[0].data.next_1_hours.summary.symbol_code' )
105
+ fi
106
+
107
+ if [ -z " $degree " ]; then
108
+ echo " yr.no err: unable to fetch weather data"
109
+ return 1
121
110
fi
122
111
123
- __read_file_content " $cache_weather_file "
124
- return
112
+ if [ " $TMUX_POWERLINE_SEG_WEATHER_UNIT " == " k" ]; then
113
+ degree=$( echo " ${degree} + 273.15" | bc)
114
+ fi
115
+ if [ " $TMUX_POWERLINE_SEG_WEATHER_UNIT " == " f" ]; then
116
+ degree=$( echo " ${degree} * 9 / 5 + 32" | bc)
117
+ fi
118
+ # condition_symbol=$(__get_yrno_condition_symbol "$condition" "$sunrise" "$sunset")
119
+ condition_symbol=$( __get_yrno_condition_symbol " $condition " )
120
+ # Write the <content@date>, separated by a @ character, so we can fetch it later on without having to call 'stat'
121
+ echo " ${condition_symbol} ${degree} °$( echo " $TMUX_POWERLINE_SEG_WEATHER_UNIT " | tr ' [:lower:]' ' [:upper:]' ) @$( date +%s) "
125
122
}
126
123
127
124
# Get symbol for condition. Available symbol names: https://api.met.no/weatherapi/weathericon/2.0/documentation#List_of_symbols
@@ -180,23 +177,28 @@ __get_yrno_condition_symbol() {
180
177
}
181
178
182
179
__read_file_split () {
183
- if [ ! -f " $1 " ]; then
184
- echo " $3 "
180
+ file_to_read=" $1 "
181
+ lookup_index=" $2 "
182
+ fallback_value=" $3 "
183
+ if [ ! -f " $file_to_read " ]; then
184
+ echo " $fallback_value "
185
185
return
186
186
fi
187
187
local -a file_arr
188
- IFS=' @' read -ra file_arr <<< " $(cat " $1 " )"
189
- if [ -z " ${file_arr[$2 ]} " ]; then
190
- echo " $3 "
188
+ IFS=' @' read -ra file_arr <<< " $(cat " $file_to_read " )"
189
+ if [ -z " ${file_arr[$lookup_index ]} " ]; then
190
+ echo " $fallback_value "
191
191
return
192
192
fi
193
- echo " ${file_arr[$2 ]} "
193
+ echo " ${file_arr[$lookup_index ]} "
194
194
}
195
195
196
+ # Default to empty/blank
196
197
__read_file_content () {
197
- __read_file_split " $1 " 0 " N/A "
198
+ __read_file_split " $1 " 0 " "
198
199
}
199
200
201
+ # Default to 0
200
202
__read_file_last_update () {
201
203
__read_file_split " $1 " 1 0
202
204
}
0 commit comments