diff options
| author | historia <gravel.justness270@slmail.me> | 2024-08-09 04:30:05 -0400 |
|---|---|---|
| committer | historia <gravel.justness270@slmail.me> | 2024-08-09 04:30:05 -0400 |
| commit | 63e113de4afe54c47789d4ce20b26a1665880c2a (patch) | |
| tree | 0d3bfbc80e1c5d9dae3aefa8d5b7050b5655f045 /src/soon.nim | |
| parent | 8e3a4ecb66f30bbb8cd57cbf1b51235ef8b54d82 (diff) | |
| download | soon-63e113de4afe54c47789d4ce20b26a1665880c2a.tar.gz | |
Feature: Parsing todos, attachments, and deadlines
Diffstat (limited to 'src/soon.nim')
| -rw-r--r-- | src/soon.nim | 84 |
1 files changed, 5 insertions, 79 deletions
diff --git a/src/soon.nim b/src/soon.nim index d154846..cede38c 100644 --- a/src/soon.nim +++ b/src/soon.nim @@ -1,13 +1,7 @@ -import std/[times, os, strutils, strbasics, sequtils, strscans, sets, strtabs, tables], parseopt -import soon/[conditions, config, typechecker] +import std/[os, strutils, strtabs, sequtils, parseopt] +import soon/[config, agenda, todos] const VERSION = "0.1" -const configDir = expandTilde("~/.config/soon/") -const defaultConfigFile = configDir & "soon.toml" -const defaultCalendarFile = configDir & "calendar.soon" - -const MONTHS = DefaultLocale.MMM.toHashSet + DefaultLocale.MMMM.toHashSet -const WEEKDAYS = DefaultLocale.ddd.toHashSet + DefaultLocale.dddd.toHashSet proc printHelp(badCmd: string) = if badCmd != "": @@ -41,73 +35,6 @@ proc createDefaultCalendarFile(file: string) = if not fileExists(file): writeFile(file, "# This is your first calendar file. Put events and todos in here.") -# Splits a string on whitespace, but maintains whitespace in date groups -proc splitWhitespaceExceptParens(str:string): seq[string] = - var output: seq[string] = newSeq[string]() - var inParens = false - for token in str.splitWhitespace: - if inParens: - output.add(output.pop & ' ' & token) - else: - output.add(token) - for c in token: - if c == '(': inParens = true - if c == ')': inParens = false - return output - -# 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 checkAllConditions(dateConditions: string, date: DateTime): bool = - let conditions = parseDateToTable(dateConditions.strip) - for k,cs in conditions: - var keyMatched = false - for c in cs: - if checkCond(c, date) == true: - keyMatched = true - break - if not keyMatched: - return false - return true - -# Returns Table[eventName, eventAttachedLines] -proc getEventsForDate(cal: seq[string], date: DateTime): Table[string, seq[string]] = - var lastInsertion = "" - for line in cal: - var (success, dateConditions, event) = scanTuple(line, "$*,$*") - if success: - if checkAllConditions(dateConditions, date) == true: - event.strip - result[event] = @[] - lastInsertion = event - elif line.strip[0] == '+': - if result.hasKey(lastInsertion): - result[lastInsertion].add(line) - else: - echo "WARNING: Cannot parse line: ", line - -proc printEvents(cal: seq[string], date: DateTime, format: string) = - let events = getEventsForDate(cal, date) - if events.len > 0: - for e in events.keys: - echo date.format(format), " ", e - for attachment in events[e]: - echo attachment.indent(date.format(format).len + 2) - -proc printAgenda(calendar: seq[string], past: int, future: int) = - let now = now() - for i in past..future: - let curDay = now + i.days - printEvents(calendar, curDay, "ddd yyyy MMM dd") - proc getCalendarFileLines(file: string): seq[string] = let f = open(file) for line in f.lines: @@ -121,7 +48,8 @@ proc processCalendars(calendarPath: string, past: int, future: int) = for file in os.walkDirRec(calendarPath): if file.toLower().endsWith(".soon"): calendar = calendar.concat(getCalendarFileLines(file)) - printAgenda(calendar, past, future) + agenda.printAgenda(calendar, past, future) + todos.printTodos(calendar) proc parseArgs(config: StringTableRef): seq[string] = for kind, key, val in getopt(): @@ -145,9 +73,7 @@ proc main() = var conf = config.loadConfig() let commands = parseArgs(conf) validateConfig(conf) - let past = 0 - parseInt(conf["past"]) - let future = parseInt(conf["future"]) - processCalendars(conf["calendarPath"], past, future) + processCalendars(conf["calendarPath"], 0 - parseInt(conf["past"]), parseInt(conf["future"])) when isMainModule: main() |
