import std/[times, os, strutils, strbasics, strscans, sets, re, tables], parseopt const VERSION = "0.1" const configDir = expandTilde("~/.config/then/") const defaultConfigFile = configDir & "then.toml" const defaultCalendarFile = configDir & "calendar.then" type Token = object token: string precedence: int # Creates a default configuration file if it doesn't exist proc createDefaultConfigFile() = if not fileExists(defaultConfigFile): if not dirExists(configDir): createDir(configDir) writeFile(defaultConfigFile, """ [config] directory = "~/.config/then/" """) # Creates a default calendar file if it doesn't exist proc createDefaultCalendarFile() = if not fileExists(defaultCalendarFile): if not dirExists(configDir): createDir(configDir) writeFile(defaultCalendarFile, """ # Add your events here """) proc printHelp(badCmd: string) = if badCmd != "": echo badCmd & " is not a valid option\n" echo """ Usage: then [options] [commands] OPTIONS -h, --help show list of CLI options -v, --version show then version --future=DAYS set future --past=DAYS set past COMMANDS a print upcoming agenda (default command) e open default calendar file in $EDITOR j print the modified julian day """ quit() proc printVersion() = echo "then " & VERSION quit() 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 cond in DefaultLocale.MMM.toHashSet or cond in DefaultLocale.MMMM.toHashSet: return "month" if cond in DefaultLocale.ddd.toHashSet or cond in DefaultLocale.dddd.toHashSet: return "dayofweek" if find(cond, re"^\d{1,2}:\d{2}(-\d{1,2}:\d{2})?$") > -1: return "time" if find(cond, re"^\dw$") > -1: return "firstweek" if find(cond, re"^\dl$") > -1: return "lastweek" if find(cond, re"^\(?\s*J\s*%\s*\d\s*([-+]\d)?\s*\)?$") > -1: return "julian" if find(cond, re"^\(.*\)$") > -1: return "dategroup" if find(cond, re"^\d{4}-\d{4}$") > -1: return "rangeyear" if find(cond, re"^\d{1,2}-\d{1,2}$") > -1: return "rangeday" var cs = cond.split("-", maxsplit = 1) if cs.len != 2: return "unknown" if (cs[0] in DefaultLocale.MMM.toHashSet or cs[0] in DefaultLocale.MMMM.toHashSet) and (cs[1] in DefaultLocale.MMM.toHashSet or cs[1] in DefaultLocale.MMMM.toHashSet): return "rangemonth" if (cs[0] in DefaultLocale.ddd.toHashSet or cs[0] in DefaultLocale.dddd.toHashSet) and (cs[1] in DefaultLocale.ddd.toHashSet or cs[1] in DefaultLocale.dddd.toHashSet): return "rangedayofweek" if find(cs[0], re"^\(.*\)$") > -1 and find(cs[1], re"^\(.*\)$") > -1: return "rangedategroup" return "unknown" proc getWeekFromEnd(date: DateTime): int = let daysInMonth = getDaysInMonth(date.month, date.year) return int((daysInMonth - date.monthday) / 7) + 1 proc getWeek(date: DateTime): int = return int((date.monthDay - 1) / 7) + 1 proc checkCond(cond: string, date: DateTime): bool = case checkType(cond): of "year": if parseInt(cond) == date.year: return true of "date": if parseInt(cond) == parseInt(date.format("d")): return true if parseInt(cond) == parseInt(date.format("dd")): return true of "month": if cond == date.format("MMM"): return true if cond == date.format("MMMM"): return 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 of "unknown": echo "ERROR: Unknown condition: ", cond return false # Splits a string on whitespace, but maintains whitespace in date groups proc splitWhitespaceExceptParens(str:string): seq[string] = var result: seq[string] = newSeq[string]() var inParens = false for token in str.splitWhitespace: if inParens: result.add(result.pop & ' ' & token) else: result.add(token) for c in token: if c == '(': inParens = true if c == ')': inParens = false return result # Returns Table[conditionType, seq of conditions] proc parseDateToTable(date:string): Table[string,seq[string]] = var table = initTable[string,seq[string]]() let conditions = splitWhitespaceExceptParens(date) for c in conditions: let condType = checkType(c) if not table.hasKey(condType): table[condType] = @[] table[condType].add(c) return table proc getEvents(cal: seq[string], date: DateTime): seq[string] = var events: seq[string] = newSeq[string]() for line in cal: var (success, date, event) = scanTuple(line, "$*,$*") if success: date.strip event.strip let conditions = parseDateToTable(date) echo conditions # var match = true # for k,cs in conditions: # for c in cs: # var cond = c.toLower.capitalizeAscii # #if checkCond(cond, date) == false: # # match = false # # break # if match: # events.add(event) # break #else: # echo "WARNING: Cannot parse line: ", line return events proc printEvents(cal: seq[string], date: DateTime) = let events = getEvents(cal, date) if events.len > 0: echo date.format("ddd yyyy MMM dd") for e in events: echo " - ", e proc openCal(file: string): seq[string] = let f = open(file) var cal: seq[string] for line in f.lines: cal.add(line) f.close() return cal proc printCalendar() = let cal = openCal(defaultCalendarFile) var past = 3 var future = 7 let now = now() while past > 0: let curDay = now - past.days printEvents(cal, curDay) dec past printEvents(cal, now) for i in 1..future: let curDay = now + i.days printEvents(cal, curDay) proc main() = var past: int = 1 var future: int = 14 var noArg: bool = true for kind, key, val in getopt(): case kind of cmdLongOption, cmdShortOption: noArg = false case key of "help", "h": printHelp("") of "version", "v": printVersion() else: printHelp(key) else: discard if noArg: printCalendar() when isMainModule: main()