From 6d419844c18f2eb1f74a2b36c8e37071cde6a440 Mon Sep 17 00:00:00 2001 From: Historia Date: Tue, 23 Jul 2024 15:35:27 -0400 Subject: Basic scheduling with simple conditions --- src/then.nim | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 1 deletion(-) (limited to 'src/then.nim') diff --git a/src/then.nim b/src/then.nim index b346329..67700a4 100644 --- a/src/then.nim +++ b/src/then.nim @@ -1 +1,150 @@ -echo "Hello world. Thanks for checking the first commit." +import std/[times, os, strutils, strbasics, strscans, sets], parseopt + +const VERSION = "0.1" +const defaultConfigDir = expandTilde("~/.config/then/") +const defaultConfigFile = defaultConfigDir & "then.toml" +const defaultCalendarFile = defaultConfigDir & "calendar.then" + +# Creates a default configuration file if it doesn't exist +proc createDefaultConfigFile() = + if not fileExists(defaultConfigFile): + if not dirExists(defaultConfigDir): + createDir(defaultConfigDir) + 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(defaultConfigDir): + createDir(defaultConfigDir) + writeFile(defaultCalendarFile, """ + # Default calendar file + # 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(cond: string): string = + if cond.len == 4 and parseInt(cond) != 0: return "year" + if cond.len in [1, 2] and parseInt(cond) != 0: return "day" + if cond in ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"].toHashSet: return "month" + return "unknown" + +proc checkCond(cond: string, day: DateTime): bool = + case checkType(cond): + of "year": + if parseInt(cond) == day.year: return true + of "day": + if parseInt(cond) == parseInt(day.format("d")): return true + if parseInt(cond) == parseInt(day.format("dd")): return true + of "month": + if cond == day.format("MMM"): return true + if cond == day.format("MMMM"): return true + of "unknown": + echo "UNKNOWN THING ", cond + return false + +proc getConditions(dateStr: string): seq[string] = + var conditions: seq[string] + conditions = dateStr.rsplit(' ') + return conditions + +proc getEvents(cal: seq[string], day: DateTime): seq[string] = + var events: seq[string] + for line in cal: + var (success, dateStr, eventStr) = scanTuple(line, "$+,$*") + if success: + dateStr.strip + eventStr.strip + let conditions = getConditions(dateStr) + var match = true + for c in conditions: + if checkCond(c, day) == false: + match = false + break + if match: + events.add(eventStr) + else: + echo "WARNING: Cannot parse line: ", line + return events + +proc printEvents(cal: seq[string], day: DateTime) = + let events = getEvents(cal, day) + if events.len > 0: + echo day.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() -- cgit v1.2.3