From 09adbc557969579950f179c9c52c2a9f2c59543e Mon Sep 17 00:00:00 2001 From: historia Date: Tue, 30 Jul 2024 17:14:53 -0400 Subject: Date groups working --- src/then.nim | 61 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 19 deletions(-) (limited to 'src/then.nim') diff --git a/src/then.nim b/src/then.nim index fb7de90..6137fdf 100644 --- a/src/then.nim +++ b/src/then.nim @@ -7,6 +7,9 @@ const configDir = expandTilde("~/.config/then/") const defaultConfigFile = configDir & "then.toml" const defaultCalendarFile = configDir & "calendar.then" +# Forward declarations +proc checkCond(cond: string, date: DateTime): bool + # Creates a default configuration file if it doesn't exist proc createDefaultConfigFile() = if not fileExists(defaultConfigFile): @@ -50,11 +53,9 @@ proc printVersion() = echo "then " & VERSION quit() -# Check an individual condition c (Jan) and return the condition type (month) +# Check an individual condition (Jan) and return the condition type (month) proc checkType(c: string): string = var cond = c - if cond[0] == '!': - cond.delete(0..0) if find(cond, re"^\d{4}$") > -1: return "year" if find(cond, re"^\d{1,2}$") > -1: return "day" if find(cond, re"^\dw$") > -1: return "firstweek" @@ -86,28 +87,50 @@ proc getWeekFromEnd(date: DateTime): int = proc getWeek(date: DateTime): int = return int((date.monthDay - 1) / 7) + 1 +# Check each condition in a date group like (Jan 1 2025) +proc checkDateGroup(s: string, date: DateTime): bool = + var conditions = s + let cs = conditions[1..conditions.len-2].splitWhitespace + for c in cs: + if checkCond(c, date) == false: + return false + return true + proc checkCond(cond: string, date: DateTime): bool = - case checkType(cond): + var condition = cond + var invert = false + var found = false + if condition[0] == '!': + condition.delete(0..0) + invert = true + case checkType(condition): + of "time": + return true of "year": - if parseInt(cond) == date.year: return true + if parseInt(condition) == date.year: + found = true of "day": - if parseInt(cond) == parseInt(date.format("d")): return true - if parseInt(cond) == parseInt(date.format("dd")): return true + if condition == date.format("d") or condition == date.format("dd"): + found = true of "month": - if cond == date.format("MMM"): return true - if cond == date.format("MMMM"): return true + if condition == date.format("MMM") or condition == date.format("MMMM"): + found = true of "dayofweek": - if cond == date.format("ddd"): return true - if cond == date.format("dddd"): return true - of "time": - return true - #of "firstweek": - # if cond[0] == getWeek(date): return true - #of "lastweek": - # if cond[0] == getWeekFromEnd(date): return true + if condition == date.format("ddd") or condition == date.format("dddd"): + found = true + of "firstweek": + if parseInt(condition[0..1]) == getWeek(date): + found = true + of "lastweek": + if parseInt(condition[0..1]) == getWeekFromEnd(date): + found = true + of "dategroup": + found = checkDateGroup(condition, date) of "unknown": - echo "ERROR: Unknown condition: ", cond - return false + echo "ERROR: Unknown condition: ", condition + if invert == true: + return not found + return found # Splits a string on whitespace, but maintains whitespace in date groups proc splitWhitespaceExceptParens(str:string): seq[string] = -- cgit v1.2.3