aboutsummaryrefslogtreecommitdiff
path: root/src/soon/archive.nim
blob: fef59e2c953cd90420f4945e67397a271dc86e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import std/[os, sets, strutils, times]

type
  ArchiveData* = object
    events*: HashSet[string]
    todos*: HashSet[string]

proc loadArchive*(path: string): ArchiveData =
  if not fileExists(path):
    return
  for line in lines(path):
    let stripped = line.strip
    if stripped.len == 0:
      continue
    if stripped.startsWith('-'):
      result.todos.incl(stripped)
    else:
      result.events.incl(stripped)

proc archiveEvent*(path: string, date: DateTime, eventName: string) =
  let line = date.format("yyyy-MM-dd") & "|" & eventName
  let f = open(path, fmAppend)
  f.writeLine(line)
  f.close()

proc unarchiveEvent*(path: string, date: DateTime, eventName: string) =
  let target = date.format("yyyy-MM-dd") & "|" & eventName
  if not fileExists(path):
    return
  let content = readFile(path)
  let f = open(path, fmWrite)
  for line in content.splitLines():
    if line.strip != target:
      f.writeLine(line)
  f.close()

proc completeTodo*(path: string, todoName: string) =
  let line = "- " & todoName
  let f = open(path, fmAppend)
  f.writeLine(line)
  f.close()

proc uncompleteTodo*(path: string, todoName: string) =
  let target = "- " & todoName
  if not fileExists(path):
    return
  let content = readFile(path)
  let f = open(path, fmWrite)
  for line in content.splitLines():
    if line.strip != target:
      f.writeLine(line)
  f.close()

proc isEventArchived*(archive: ArchiveData, date: DateTime, eventName: string): bool =
  let key = date.format("yyyy-MM-dd") & "|" & eventName
  return archive.events.contains(key)

proc isTodoCompleted*(archive: ArchiveData, todoName: string): bool =
  let key = "- " & todoName
  return archive.todos.contains(key)