diff options
| author | historia <gravel.justness270@slmail.me> | 2024-08-07 04:10:34 -0400 |
|---|---|---|
| committer | historia <gravel.justness270@slmail.me> | 2024-08-07 04:10:34 -0400 |
| commit | 6d3740e68631530dd612d5dc56cc83b990003e25 (patch) | |
| tree | 9dd0070a8c28a3e572ae17a92c453e0beb25326e | |
| parent | 1320375ebc0e87956807c0c543abe9f9a9f1dd6e (diff) | |
| download | soon-6d3740e68631530dd612d5dc56cc83b990003e25.tar.gz | |
CLI option parsing and recursively search calendar path
| -rw-r--r-- | README.md | 28 | ||||
| -rw-r--r-- | src/soon.nim | 97 | ||||
| -rw-r--r-- | tests/tconditions.nim | 2 | ||||
| -rw-r--r-- | tests/ttypechecker.nim | 2 |
4 files changed, 70 insertions, 59 deletions
@@ -16,27 +16,25 @@ In terms of features and complexity, Soon lies between [When](https://www.lighta # Arguments ``` -Usage: soon [options] [commands] [file] +Usage: soon [options] Options: --p, --past DAYS days in past to print agenda (default: 1) --f, --future DAYS days in future to print agenda (default: 14) +-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 -Commands: -a print agenda -c print 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 - Combine commands to get output in a certain order. -`soon ca` prints a calendar followed by an agenda. -`soon at` prints an agenda followed by todos. +`soon -ca` prints a calendar followed by an agenda. +`soon -at` prints an agenda followed by todos. Check the docs at https://codeberg.org/historia/soon ``` diff --git a/src/soon.nim b/src/soon.nim index 3040f84..0850bd6 100644 --- a/src/soon.nim +++ b/src/soon.nim @@ -1,4 +1,4 @@ -import std/[times, os, strutils, strbasics, strscans, sets, strtabs, tables], parseopt +import std/[times, os, strutils, strbasics, sequtils, strscans, sets, strtabs, tables], parseopt import soon/[conditions, config, typechecker] const VERSION = "0.1" @@ -13,19 +13,23 @@ proc printHelp(badCmd: string) = if badCmd != "": echo badCmd & " is not a valid option\n" echo """ -Usage: - soon [options] [commands] - -OPTIONS - -h, --help show list of CLI options - -v, --version show soon version - --future=DAYS set future - --past=DAYS set past - -COMMANDS - a print upcoming agenda (default command) - e open default calendar file in $EDITOR - j print the modified julian day +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() @@ -33,6 +37,10 @@ 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.") + # Splits a string on whitespace, but maintains whitespace in date groups proc splitWhitespaceExceptParens(str:string): seq[string] = var output: seq[string] = newSeq[string]() @@ -90,47 +98,52 @@ proc printEvents(cal: seq[string], date: DateTime, format: string) = for e in events: echo date.format(format), " ", e -proc openCal(file: string): seq[string] = +proc printCalendar(calendar: seq[string], config: StringTableRef) = + let past = 0 - parseInt(config["past"]) + let future = parseInt(config["future"]) + 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] = let f = open(file) - var cal: seq[string] for line in f.lines: if line == "": continue if line.strip[0] == '#': continue - cal.add(line) + result.add(line) f.close() - return cal -proc printCalendar() = - let cal = openCal(defaultCalendarFile) - var past = -3 - var future = 7 - let now = now() +proc processCalendars(config: StringTableRef) = + var calendar = newSeq[string]() + for file in os.walkDirRec(config["calendarPath"]): + if file.toLower().endsWith(".soon"): + calendar = calendar.concat(openCalendar(file)) + printCalendar(calendar, config) - for i in past..future: - let curDay = now + i.days - printEvents(cal, curDay, "ddd yyyy MMM dd") - -proc parseArgs(config: StringTableRef): StringTableRef = +proc parseArgs(config: StringTableRef): seq[string] = for kind, key, val in getopt(): case kind + of cmdEnd: break of cmdLongOption, cmdShortOption: case key - of "help", "h": - printHelp("") - of "version", "v": - printVersion() - else: - printHelp(key) - else: - discard + 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 config = loadConfig() - config = parseArgs(config) - var noArg: bool = true - - if noArg: - printCalendar() + var conf = config.loadConfig() + let commands = parseArgs(conf) + validateConfig(conf) + processCalendars(conf) when isMainModule: main() diff --git a/tests/tconditions.nim b/tests/tconditions.nim index 4732546..a0298e4 100644 --- a/tests/tconditions.nim +++ b/tests/tconditions.nim @@ -1,4 +1,4 @@ -import then/conditions +import soon/conditions import std/[unittest] import std/[times] diff --git a/tests/ttypechecker.nim b/tests/ttypechecker.nim index ee81fc4..5e76c2f 100644 --- a/tests/ttypechecker.nim +++ b/tests/ttypechecker.nim @@ -1,4 +1,4 @@ -import then/typechecker +import soon/typechecker import std/[unittest] test "Testing typechecker": |
