diff --git a/config/awesome/evil/weather.lua b/config/awesome/evil/weather.lua index d9f4fb9..4189327 100644 --- a/config/awesome/evil/weather.lua +++ b/config/awesome/evil/weather.lua @@ -4,6 +4,7 @@ -- description (string) -- icon_code (string) local awful = require("awful") +local helpers = require("helpers") -- Configuration local key = user.openweathermap_key @@ -31,8 +32,7 @@ local weather_details_script = [[ fi ']] --- Periodically get weather info -awful.widget.watch(weather_details_script, update_interval, function(widget, stdout) +helpers.remote_watch(weather_details_script, update_interval, "/tmp/awesomewm-evil-weather", function(stdout) local icon_code = string.sub(stdout, 1, 3) local weather_details = string.sub(stdout, 5) weather_details = string.gsub(weather_details, '^%s*(.-)%s*$', '%1') diff --git a/config/awesome/helpers.lua b/config/awesome/helpers.lua index c5e26e2..74ce4f9 100644 --- a/config/awesome/helpers.lua +++ b/config/awesome/helpers.lua @@ -436,5 +436,46 @@ function helpers.screen_mask(s, bg) return mask end +-- Useful for periodically checking the output of a command that +-- requires internet access. +-- Ensures that `command` will be run only once during the desired +-- `interval`, even if awesome restarts multiple times during this time. +-- Saves output in `output_file` and checks its last modification +-- time to determine whether to run the command again or not. +-- Passes the output of `command` to `callback` function. +function helpers.remote_watch(command, interval, output_file, callback) + + local run_the_thing = function() + -- Pass output to callback AND write it to file + awful.spawn.easy_async_with_shell(command.." | tee "..output_file, function(out) callback(out) end) + end + + gears.timer { + timeout = interval, + call_now = true, + autostart = true, + single_shot = false, + callback = function() + awful.spawn.easy_async_with_shell("date -r "..output_file.." +%s", function(last_update, _, __, exitcode) + + -- Probably the file does not exist yet (first time + -- running after reboot) + if exitcode == 1 then + run_the_thing() + return + end + + local diff = os.time() - tonumber(last_update) + if diff > interval then + run_the_thing() + else + -- Pass the date saved in the file since it is fresh enough + awful.spawn.easy_async_with_shell("cat "..output_file, function(out) callback(out) end) + end + end) + end + } +end + return helpers