aboutsummaryrefslogtreecommitdiff
path: root/src/soon.nim
diff options
context:
space:
mode:
authorhistoria <gravel.justness270@slmail.me>2024-08-09 02:52:41 -0400
committerhistoria <gravel.justness270@slmail.me>2024-08-09 02:52:41 -0400
commit8e3a4ecb66f30bbb8cd57cbf1b51235ef8b54d82 (patch)
tree49fc251e40f2491fb8a2f072c12f4d085889917a /src/soon.nim
parentedd24af0a7b79f418356d6a0c45d2ee23e3325df (diff)
downloadsoon-8e3a4ecb66f30bbb8cd57cbf1b51235ef8b54d82.tar.gz
Feature: Attachments ('+') for events
Diffstat (limited to 'src/soon.nim')
-rw-r--r--src/soon.nim68
1 files changed, 36 insertions, 32 deletions
diff --git a/src/soon.nim b/src/soon.nim
index 0850bd6..d154846 100644
--- a/src/soon.nim
+++ b/src/soon.nim
@@ -56,7 +56,7 @@ proc splitWhitespaceExceptParens(str:string): seq[string] =
return output
# Returns Table[conditionType, seq of conditions]
-proc parseDateToTable(date:string): Table[string,seq[string]] =
+proc parseDateToTable(date:string): Table[string, seq[string]] =
var table = initTable[string,seq[string]]()
let conditions = splitWhitespaceExceptParens(date)
for c in conditions:
@@ -66,47 +66,49 @@ proc parseDateToTable(date:string): Table[string,seq[string]] =
table[condType].add(c)
return table
-
-proc getEvents(cal: seq[string], date: DateTime): seq[string] =
- var events: seq[string] = newSeq[string]()
+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:
- dateConditions.strip
- event.strip
- let conditions = parseDateToTable(dateConditions)
- var match = true
- # key = Condition Type (e.g. month), cs = Seq of conditions
- for k,cs in conditions:
- var keyMatched = false
- for c in cs:
- if checkCond(c, date) == true:
- keyMatched = true
- break
- if not keyMatched:
- match = false
- break
- if match:
- events.add(event)
+ 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
- return events
proc printEvents(cal: seq[string], date: DateTime, format: string) =
- let events = getEvents(cal, date)
+ let events = getEventsForDate(cal, date)
if events.len > 0:
- for e in events:
+ for e in events.keys:
echo date.format(format), " ", e
+ for attachment in events[e]:
+ echo attachment.indent(date.format(format).len + 2)
-proc printCalendar(calendar: seq[string], config: StringTableRef) =
- let past = 0 - parseInt(config["past"])
- let future = parseInt(config["future"])
+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 openCalendar(file: string): seq[string] =
+proc getCalendarFileLines(file: string): seq[string] =
let f = open(file)
for line in f.lines:
if line == "": continue
@@ -114,12 +116,12 @@ proc openCalendar(file: string): seq[string] =
result.add(line)
f.close()
-proc processCalendars(config: StringTableRef) =
+proc processCalendars(calendarPath: string, past: int, future: int) =
var calendar = newSeq[string]()
- for file in os.walkDirRec(config["calendarPath"]):
+ for file in os.walkDirRec(calendarPath):
if file.toLower().endsWith(".soon"):
- calendar = calendar.concat(openCalendar(file))
- printCalendar(calendar, config)
+ calendar = calendar.concat(getCalendarFileLines(file))
+ printAgenda(calendar, past, future)
proc parseArgs(config: StringTableRef): seq[string] =
for kind, key, val in getopt():
@@ -143,7 +145,9 @@ proc main() =
var conf = config.loadConfig()
let commands = parseArgs(conf)
validateConfig(conf)
- processCalendars(conf)
+ let past = 0 - parseInt(conf["past"])
+ let future = parseInt(conf["future"])
+ processCalendars(conf["calendarPath"], past, future)
when isMainModule:
main()