diff options
Diffstat (limited to 'src/soon.nim')
| -rw-r--r-- | src/soon.nim | 51 |
1 files changed, 47 insertions, 4 deletions
diff --git a/src/soon.nim b/src/soon.nim index 2c5f109..61ac16d 100644 --- a/src/soon.nim +++ b/src/soon.nim @@ -1,4 +1,4 @@ -import std/[os, strutils, strtabs, sequtils, parseopt] +import std/[os, strutils, strtabs, sequtils, osproc, parseopt] import soon/[config, agenda, todos] const VERSION = "0.1" @@ -10,14 +10,15 @@ proc printHelp(badCmd: string) = Usage: soon [options] Options: --a print agenda (default behavior if no options) +-a print agenda -c print reference calendar -t print todos +-d print deadlines -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 +-i, --date 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 @@ -44,9 +45,15 @@ proc getFileLines(file: string): seq[string] = f.close() proc readAllSoonFiles(path: string): seq[string] = + var soonFileExists = false for file in os.walkDirRec(path): if file.toLower().endsWith(".soon"): + soonFileExists = true result = result.concat(getFileLines(file)) + if not soonFileExists: + echo "No .soon files exist at " & path + echo "Check your paths in ~/.config/soon/soon.conf" + quit() proc processCalendars(path: string, past: int, future: int) = var calendar = readAllSoonFiles(path) @@ -67,15 +74,51 @@ proc parseArgs(config: StringTableRef): seq[string] = result.add(key) of cmdArgument: printHelp(key) + if result.len == 0: + result.add("a") + result.add("t") + result.add("d") + echo "I'm running soon -atd, but I should've checked the user config and run that!" + +proc printReference() = + discard execCmd("cal -3") + +proc sanitize(str: string): string = + result = "" + for ch in str: + if ch.isAlphaNumeric() or ch in {'/', '~', '.'}: + result.add(ch) + +proc openEditor(path: string, file: string) = + var editor = getEnv("EDITOR").sanitize + if editor.isEmptyOrWhitespace: editor = "vim" + let sanePath = path.sanitize + let saneFile = file.sanitize + discard execCmd("$1 $2/$3" % [editor, sanePath, saneFile]) proc validateConfig(config: StringTableRef) = createDefaultCalendarFile(config["path"] / config["defaultFile"]) +proc processCommands(commands: seq[string], conf: StringTableRef) = + var calendar = readAllSoonFiles(conf["calendarPath"]) + var past = 0 - parseInt(conf["past"]) + var future = parseInt(conf["future"]) + for command in commands: + case command + of "a": agenda.printAgenda(calendar, past, future) + of "w": agenda.printAgenda(calendar, 0, 7) + of "m": agenda.printAgenda(calendar, 0, 28) + of "y": agenda.printAgenda(calendar, 0, 365) + of "t": todos.printTodos(calendar) + of "d": todos.printDeadlineTodos(calendar) + of "c": printReference() + of "e": openEditor(conf["calendarPath"], conf["defaultFile"]) + proc main() = var conf = config.loadConfig() let commands = parseArgs(conf) validateConfig(conf) - processCalendars(conf["calendarPath"], 0 - parseInt(conf["past"]), parseInt(conf["future"])) + processCommands(commands, conf) when isMainModule: main() |
