From eb907e8e36a120ad42f6bceff6b8de5ad12af4bd Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Thu, 3 Nov 2016 12:49:20 -0500 Subject: [PATCH] Re-read plan file if the file has a newer modification date than last time we checked. --- daily_notifier.nim | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/daily_notifier.nim b/daily_notifier.nim index 09feb6a..3b8e341 100644 --- a/daily_notifier.nim +++ b/daily_notifier.nim @@ -33,8 +33,6 @@ proc parseDailyPlan(filename: string): seq[PlanItem] = result = @[] - if not existsFile(filename): return - var parseState = BeforeHeading let planItemRe = re"\s*(\d{4})\s+(.*)" const timeFmt = "HHmm" @@ -103,19 +101,31 @@ proc mainLoop(args: Table[string, Value]): void = loadConfig(0) signal(SIGHUP, loadConfig) var curDay: TimeInfo = getLocalTime(fromSeconds(0)) + var todaysFile = "nofile" + var lastModTime = fromSeconds(0) var todaysItems: seq[PlanItem] = @[] while true: + # Check the date let now = getLocalTime(getTime()) - var today = startOfDay(now) + let today = startOfDay(now) - # Load today's plan items - if today != curDay or todaysItems.len == 0: + # If we need to change day, look for a new file. + if today != curDay: curDay = today - let todaysPlanFile = - cfg.planDir & "/" & today.format(cfg.dateFmt) & "-plan.md" - todaysItems = parseDailyPlan(todaysPlanFile) + todaysFile = cfg.planDir & "/" & today.format(cfg.dateFmt) & "-plan.md" + todaysItems = @[] + lastModTime = fromSeconds(0) + + # Check for our plan file. + if fileExists(todaysFile): + let fileModTime = getLastModificationTime(todaysFile) + + # Check if the file has been modified or we have no itmes + if fileModTime > lastModTime or todaysItems.len == 0: + lastModTime = fileModTime + todaysItems = parseDailyPlan(todaysFile) # Check to see if any items are happening soon. let cutoff = now + seconds(cfg.notificationSecs)