diff options
| -rw-r--r-- | src/soon/agenda.nim | 7 | ||||
| -rw-r--r-- | src/soon/columnize.nim | 20 | ||||
| -rw-r--r-- | src/soon/conditionchecker.nim | 2 | ||||
| -rw-r--r-- | src/soon/todos.nim | 78 | ||||
| -rw-r--r-- | tests/tcolumnize.nim | 11 |
5 files changed, 85 insertions, 33 deletions
diff --git a/src/soon/agenda.nim b/src/soon/agenda.nim index 49dcfe8..488778f 100644 --- a/src/soon/agenda.nim +++ b/src/soon/agenda.nim @@ -1,7 +1,7 @@ import std/[times, strutils, strbasics, strscans, tables, algorithm, re, oids] import conditionchecker, typechecker, parsetime -type Event = tuple +type Event = object name: string time: DateTime attachments: seq[string] @@ -60,9 +60,9 @@ proc getEventsForDate(cal: seq[string], date: DateTime): seq[Event] = var newOid: Oid if conditions.hasKey("time"): for t in conditions["time"]: - result.add((event, parsetime.toDateTime(t), @[], genOid())) + result.add(Event(name: event, time: parsetime.toDateTime(t), attachments: @[], id: genOid())) else: - result.add((event, allDay(), @[], genOid())) + result.add(Event(name: event, time: allDay(), attachments: @[], id: genOid())) readyForComments = true else: readyForComments = false @@ -81,6 +81,7 @@ proc getEventsForDate(cal: seq[string], date: DateTime): seq[Event] = proc compare(a, b: string): int = cmp(a.len, b.len) +# TODO: Sort me proc printDaysEvents(cal: seq[string], date: DateTime, format: string) = var events = getEventsForDate(cal, date) if events.len > 0: diff --git a/src/soon/columnize.nim b/src/soon/columnize.nim new file mode 100644 index 0000000..4ec87af --- /dev/null +++ b/src/soon/columnize.nim @@ -0,0 +1,20 @@ +import std/[strutils, enumerate] + +proc pad(table: seq[seq[string]], max: seq[int]): seq[seq[string]] = + for line in table: + var resLine = newSeq[string]() + for i, field in enumerate(line): + resLine.add(field & repeat(' ', max[i] - field.len)) + result.add(resLine) + +proc columnize(table: seq[seq[string]]): seq[seq[string]] = + var max = newSeq[int](table[0].len) + for line in table: + for i, field in enumerate(line): + if field.len > max[i]: + max[i] = field.len + result = pad(table, max) + +proc echo*(table: seq[seq[string]]) = + for line in columnize(table): + echo line.join(" ") diff --git a/src/soon/conditionchecker.nim b/src/soon/conditionchecker.nim index d4bd626..f3eea22 100644 --- a/src/soon/conditionchecker.nim +++ b/src/soon/conditionchecker.nim @@ -122,7 +122,7 @@ proc dateGroupStringToTable(s: string): Table[string, int] = return t # comparisonDate is used for strings with no year like (Aug 15) -proc dateGroupStringToDate(s: string, comparisonDate: DateTime): DateTime = +proc dateGroupStringToDate*(s: string, comparisonDate: DateTime): DateTime = var t = dateGroupStringToTable(s) if not t.hasKey("year"): t["year"] = comparisonDate.year diff --git a/src/soon/todos.nim b/src/soon/todos.nim index 8d5ccc3..97c6094 100644 --- a/src/soon/todos.nim +++ b/src/soon/todos.nim @@ -1,46 +1,66 @@ -import std/[times, strutils, tables, re] +import std/[times, strutils, tables, strtabs, re] +import conditionchecker, columnize -# Returns Table[todo, attachments] -proc getTodosFromCalendar(calendar: seq[string]): Table[string, seq[string]] = - var lastInsertion = "" - for l in calendar: - let line: string = l.strip - if line[0..1] == "- ": - result[line] = @[] - lastInsertion = line - elif line[0] == '+': - if result.hasKey(lastInsertion): - result[lastInsertion].add(l) +type + Todo = object + name: string + attachments: seq[string] + deadline: DateTime + +proc removeHyphen(s: string): string = + return replace(s, re"^(\s*\-\s*)") + +proc getTodosFromCalendar(calendar: seq[string]): seq[Todo] = + var readyForAttachments = false + for line in calendar: + let strippedLine = line.strip + if strippedLine[0..1] == "- ": + result.add(Todo(name: line, attachments: @[], deadline: now())) + readyForAttachments = true + elif strippedLine[0] == '+': + if readyForAttachments: + result[result.high].attachments.add(line) else: - lastInsertion = "" + readyForAttachments = false -proc splitRegularAndDeadlineTodos(todos: Table[string, seq[string]]): (Table[string, seq[string]], Table[string, seq[string]]) = - for todo in todos.keys: - var attachments = newSeq[string]() +proc splitRegularAndDeadlineTodos(todos: seq[Todo]): (seq[Todo], seq[Todo]) = + for todo in todos: + var deadline: DateTime var hasDeadline = false - for attach in todos[todo]: - if match(attach, re("^\\s*\\+\\s*Due:", flags = {reIgnoreCase})): + var matches: array[1, string] + for attach in todo.attachments: + if find(attach, re("^\\s*\\+\\s*Due:(.*)", flags = {reIgnoreCase}), matches) > -1: + deadline = conditionchecker.dateGroupStringToDate(matches[0], now()) hasDeadline = true - attachments.add(attach) + break if hasDeadline: - result[1][todo] = attachments + result[1].add(Todo(name: removeHyphen(todo.name), attachments: todo.attachments, deadline: deadline)) else: - result[0][todo] = attachments + result[0].add(todo) -proc printRegularTodos(todos: Table[string, seq[string]]) = +proc printRegularTodos(todos: seq[Todo]) = if todos.len > 0: echo "\nTodo List:" - for todo in todos.keys: - echo todo - for attachment in todos[todo]: + for todo in todos: + echo todo.name + for attachment in todo.attachments: echo attachment -proc printDeadlineTodos(todos: Table[string, seq[string]]) = +# TODO: Sort on deadlines +proc printDeadlineTodos(todos: seq[Todo]) = if todos.len > 0: echo "\nUpcoming Deadlines:" - for todo in todos.keys: - echo todo - echo "Deadline logic not yet implemented" + var lines = newSeq[seq[string]]() + for todo in todos: + var line = @[todo.deadline.format("ddd yyyy MMM dd")] + let dueInDays = (todo.deadline - now()).inDays + if dueInDays > 0: + line.add("(Due in " & dueInDays.intToStr & " days)") + else: + line.add("(OVERDUE)") + line.add(todo.name) + lines.add(line) + columnize.echo(lines) proc printTodos*(calendar: seq[string]) = let todos = getTodosFromCalendar(calendar) diff --git a/tests/tcolumnize.nim b/tests/tcolumnize.nim new file mode 100644 index 0000000..950b040 --- /dev/null +++ b/tests/tcolumnize.nim @@ -0,0 +1,11 @@ +import soon/columnize +import std/[unittest, strutils] + +test "Testing columize": + var table = newSeq[seq[string]]() + let line1 = @["Jan 1", "(OVERDUE)", "Do the thing"] + let line2 = @["Feb 22", "(Due in 24 days)", "Do that other thing"] + table.add(line1) + table.add(line2) + for line in columnize(table): + echo line.join(" ") |
