diff options
| author | historia <[not public]> | 2026-06-11 22:35:30 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-06-11 22:35:30 -0400 |
| commit | 75229ac7f76a7dbd1f9c0455535600ef0ee789a1 (patch) | |
| tree | 39619d95004b5aea115267bf5ad8a6f6013f30a9 /src/soon.nim | |
| parent | cb35bd463a53383c06e60be4d843bdf1e7d9d68b (diff) | |
| download | soon-75229ac7f76a7dbd1f9c0455535600ef0ee789a1.tar.gz | |
feat: added tui, fixed archive
Diffstat (limited to 'src/soon.nim')
| -rw-r--r-- | src/soon.nim | 162 |
1 files changed, 116 insertions, 46 deletions
diff --git a/src/soon.nim b/src/soon.nim index 62dbfcf..e6c545d 100644 --- a/src/soon.nim +++ b/src/soon.nim @@ -1,5 +1,5 @@ import std/[os, strutils, strtabs, sequtils, osproc, parseopt] -import soon/[config, agenda, todos] +import soon/[config, agenda, todos, tui, archive] const VERSION = "0.1" @@ -10,19 +10,20 @@ proc printHelp(badCmd: string) = Usage: soon [options] Options: --a print agenda --c print reference calendar (using 'cal') --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 --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 --v, --version print version +-a print agenda +-c print reference calendar (using 'cal') +-t print todos +-d print deadlines +-e open default calendar file in $EDITOR +-s open soon.conf 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 +-p, --past=DAYS days in past to print agenda (default: 1) +-f, --future=DAYS days in future to print agenda (default: 14) +--config <path> use alternate config file +-h, --help show this help +-v, --version print version Check the docs at https://codeberg.org/historia/soon """ @@ -55,12 +56,8 @@ proc readAllSoonFiles(path: string): seq[string] = echo "Check your paths in ~/.config/soon/soon.conf" quit() -proc processCalendars(path: string, past: int, future: int) = - var calendar = readAllSoonFiles(path) - agenda.printAgenda(calendar, past, future) - todos.printTodos(calendar) - -proc parseArgs(config: StringTableRef): seq[string] = +proc parseArgs(config: StringTableRef): tuple[commands: seq[string], interactive: bool] = + var skipNext = false for kind, key, val in getopt(): case kind of cmdEnd: break @@ -70,15 +67,37 @@ proc parseArgs(config: StringTableRef): seq[string] = of "future", "f": config["future"] = val of "help", "h": printHelp("") of "version", "v": printVersion() + of "i": + result.interactive = true + of "config": + if val == "": + skipNext = true else: - result.add(key) + result.commands.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!" + if skipNext: + skipNext = false + else: + printHelp(key) + + let printTodos = config.getOrDefault("printtodos", "true") == "true" + + if result.commands.len == 0: + let defaultCmd = config.getOrDefault("defaultCommand", "atd") + if result.interactive: + for c in defaultCmd: + if c == 't' or c == 'd': + if printTodos: + result.commands.add($c) + elif c != ' ': + result.commands.add($c) + else: + for c in defaultCmd: + if c == 't' or c == 'd': + if printTodos: + result.commands.add($c) + elif c != ' ': + result.commands.add($c) proc printReference() = discard execCmd("cal -3") @@ -89,36 +108,87 @@ proc sanitize(str: string): string = if ch.isAlphaNumeric() or ch in {'/', '~', '.'}: result.add(ch) -proc openEditor(path: string, file: string) = - var editor = getEnv("EDITOR").sanitize +proc openFileInEditor(conf: StringTableRef, filePath: string) = + var editor = conf.getOrDefault("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]) + let saneFile = filePath.sanitize + discard execCmd("$1 $2" % [editor, saneFile]) + +proc openEditor(conf: StringTableRef) = + let filePath = conf.getOrDefault("calendarPath", + conf.getOrDefault("path", "")) / conf.getOrDefault("defaultFile", "calendar.soon") + openFileInEditor(conf, filePath) + +proc openConfig(conf: StringTableRef) = + openFileInEditor(conf, conf.getOrDefault("configFile", + conf.getOrDefault("path", "") / "soon.conf")) proc validateConfig(config: StringTableRef) = createDefaultCalendarFile(config["path"] / config["defaultFile"]) -proc processCommands(commands: seq[string], conf: StringTableRef) = +proc processCommands(commands: seq[string], conf: StringTableRef, interactive: bool) = 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"]) + let arch = archive.loadArchive(conf.getOrDefault("archiveFile", + conf.getOrDefault("path", "") / "archive")) + + if interactive: + var showAgenda = false + var showTodos = false + var showDeadlines = false + for cmd in commands: + case cmd + of "a": showAgenda = true + of "w": + showAgenda = true + past = 0 + future = 7 + of "m": + showAgenda = true + past = 0 + future = 28 + of "y": + showAgenda = true + past = 0 + future = 365 + of "t": showTodos = true + of "d": showDeadlines = true + else: discard + if not showAgenda and not showTodos and not showDeadlines: + showAgenda = true + showTodos = true + showDeadlines = true + tui.start(calendar, conf, past, future, showAgenda, showTodos, showDeadlines) + else: + for command in commands: + case command + of "a": agenda.printAgenda(calendar, past, future, conf, arch) + of "w": agenda.printAgenda(calendar, 0, 7, conf, arch) + of "m": agenda.printAgenda(calendar, 0, 28, conf, arch) + of "y": agenda.printAgenda(calendar, 0, 365, conf, arch) + of "t": todos.printTodos(calendar, arch) + of "d": todos.printDeadlineTodos(calendar, arch) + of "c": printReference() + of "e": openEditor(conf) + of "s": openConfig(conf) + else: discard + +proc findConfigArg(): string = + let params = commandLineParams() + var i = 0 + while i < params.len: + if params[i] == "--config" and i + 1 < params.len: + return params[i + 1] + elif params[i].startsWith("--config="): + return params[i].split("=", maxsplit = 1)[1] + inc i proc main() = - var conf = config.loadConfig() - let commands = parseArgs(conf) + var conf = config.loadConfig(findConfigArg()) + let (commands, interactive) = parseArgs(conf) validateConfig(conf) - processCommands(commands, conf) + processCommands(commands, conf, interactive) when isMainModule: main() |
