summaryrefslogtreecommitdiff
path: root/weather-widget
diff options
context:
space:
mode:
authorblix4 <bblix@protonmail.com>2019-08-26 22:28:43 -0700
committerblix4 <bblix@protonmail.com>2019-08-26 23:27:23 -0700
commitcd6776c8b0e23ef7cb34a9979012c2a6edb8b295 (patch)
tree49911ca4b8571c9e82ae2cfe33436c390c78616d /weather-widget
parent372ae3c9e7cab4b64b8dcaf31d9d2d921a723585 (diff)
add option to show temperature in both units
Separate options for the widget itself, and in the popup. Also note that this changes the precision slightly: * on the widget itself: now uses string formatting to round, instead of removing the decimal * on the popup: rounds to 1 decimal place in popup (was showing 2 decimal places before)
Diffstat (limited to 'weather-widget')
-rw-r--r--weather-widget/weather.lua41
1 files changed, 35 insertions, 6 deletions
diff --git a/weather-widget/weather.lua b/weather-widget/weather.lua
index 2d80947..d6693cf 100644
--- a/weather-widget/weather.lua
+++ b/weather-widget/weather.lua
@@ -98,9 +98,39 @@ local function to_direction(degrees)
return directions[math.floor((degrees % 360) / 22.5) + 1]
end
+-- Convert degrees Celsius to Fahrenheit
+local function celsius_to_fahrenheit(c)
+ return c*9/5+32
+end
+
+-- Convert degrees Fahrenheit to Celsius
+local function fahrenheit_to_celsius(f)
+ return (f-32)*5/9
+end
+
local weather_timer = gears.timer({ timeout = 60 })
local resp
+local function gen_temperature_str(temp, fmt_str, show_other_units)
+ local temp_str = string.format(fmt_str, temp)
+ local s = temp_str .. '°' .. (secrets.weather_widget_units == 'metric' and 'C' or 'F')
+
+ if (show_other_units) then
+ local temp_conv, units_conv
+ if (secrets.weather_widget_units == 'metric') then
+ temp_conv = celsius_to_fahrenheit(temp)
+ units_conv = 'F'
+ else
+ temp_conv = fahrenheit_to_celsius(temp)
+ units_conv = 'C'
+ end
+
+ local temp_conv_str = string.format(fmt_str, temp_conv)
+ s = s .. ' ' .. '('.. temp_conv_str .. '°' .. units_conv .. ')'
+ end
+ return s
+end
+
weather_timer:connect_signal("timeout", function ()
local resp_json = {}
local res, status = http.request{
@@ -132,9 +162,8 @@ weather_timer:connect_signal("timeout", function ()
elseif (resp_json ~= nil and resp_json ~= '') then
resp = json.decode(resp_json)
icon_widget.image = path_to_icons .. icon_map[resp.weather[1].icon]
- temp_widget:set_text(string.gsub(resp.main.temp, "%.%d+", "")
- .. '°'
- .. (secrets.weather_widget_units == 'metric' and 'C' or 'F'))
+ temp_widget:set_text(gen_temperature_str(resp.main.temp, '%.0f',
+ secrets.weather_both_temp_units_widget))
end
end)
weather_timer:start()
@@ -149,13 +178,13 @@ weather_widget:connect_signal("mouse::enter", function()
text =
'<big>' .. resp.weather[1].main .. ' (' .. resp.weather[1].description .. ')</big><br>' ..
'<b>Humidity:</b> ' .. resp.main.humidity .. '%<br>' ..
- '<b>Temperature:</b> ' .. resp.main.temp .. '°'
- .. (secrets.weather_widget_units == 'metric' and 'C' or 'F') .. '<br>' ..
+ '<b>Temperature:</b> ' .. gen_temperature_str(resp.main.temp, '%.1f',
+ secrets.weather_both_temp_units_popup) .. '<br>' ..
'<b>Pressure:</b> ' .. resp.main.pressure .. 'hPa<br>' ..
'<b>Clouds:</b> ' .. resp.clouds.all .. '%<br>' ..
'<b>Wind:</b> ' .. resp.wind.speed .. 'm/s (' .. to_direction(resp.wind.deg) .. ')',
timeout = 5, hover_timeout = 10,
- width = 200
+ width = (secrets.weather_both_temp_units_popup == true and 210 or 200)
}
end)