aboutsummaryrefslogtreecommitdiff
path: root/src/soon.nim
blob: cede38ceea963434fa5a1133049a089d230638d2 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import std/[os, strutils, strtabs, sequtils, parseopt]
import soon/[config, agenda, todos]

const VERSION = "0.1"

proc printHelp(badCmd: string) =
  if badCmd != "":
    echo badCmd & " is not a valid option\n"
  echo """
Usage: soon [options]

Options:
-a                 print agenda (default behavior if no options)
-c                 print reference calendar
-t                 print todos
-e                 open default calendar file in $EDITOR
-i                 open TUI in interactive mode to schedule/archive events
-w, -m, -y         print agenda for upcoming week, month, or year
-j                 print the modified julian day
-d                 print today's date
-p, --past=DAYS    days in past to print agenda (default: 1)
-f, --future=DAYS  days in future to print agenda (default: 14)
-h, --help         show this help
-v, --version      print version

Check the docs at https://codeberg.org/historia/soon
  """
  quit()

proc printVersion() =
  echo "soon " & VERSION
  quit()

proc createDefaultCalendarFile(file: string) =
  if not fileExists(file):
    writeFile(file, "# This is your first calendar file. Put events and todos in here.")

proc getCalendarFileLines(file: string): seq[string] =
  let f = open(file)
  for line in f.lines:
    if line == "": continue
    if line.strip[0] == '#': continue
    result.add(line)
  f.close()

proc processCalendars(calendarPath: string, past: int, future: int) =
  var calendar = newSeq[string]()
  for file in os.walkDirRec(calendarPath):
    if file.toLower().endsWith(".soon"):
      calendar = calendar.concat(getCalendarFileLines(file))
  agenda.printAgenda(calendar, past, future)
  todos.printTodos(calendar)

proc parseArgs(config: StringTableRef): seq[string] =
  for kind, key, val in getopt():
    case kind
    of cmdEnd: break
    of cmdLongOption, cmdShortOption:
      case key
        of "past",    "p": config["past"] = val
        of "future",  "f": config["future"] = val
        of "help",    "h": printHelp("")
        of "version", "v": printVersion()
        else:
          result.add(key)
    of cmdArgument:
      printHelp(key)

proc validateConfig(config: StringTableRef) =
  createDefaultCalendarFile(config["path"] / config["defaultFile"])

proc main() =
  var conf = config.loadConfig()
  let commands = parseArgs(conf)
  validateConfig(conf)
  processCalendars(conf["calendarPath"], 0 - parseInt(conf["past"]), parseInt(conf["future"]))

when isMainModule:
  main()