3 Commits
0.4.1 ... 0.7.0

Author SHA1 Message Date
d3fc1cdf9c Fix incorrect ISO time format.
An incorrect time format that was used in version 0.6 and prior.
Version 0.7 between 1.0 support this format on read only and will write
out the correct  format (so they can be used to convert older timelines).
2016-11-01 09:57:36 -05:00
237f5026f2 Added merge command. 2016-10-23 17:31:27 -05:00
3cf76ef382 Add --this-week, --last-week options 2016-10-21 15:42:48 -05:00
3 changed files with 61 additions and 18 deletions

71
ptk.nim
View File

@ -4,7 +4,7 @@
## Simple time keeping CLI ## Simple time keeping CLI
import algorithm, docopt, json, langutils, logging, os, nre, sequtils, import algorithm, docopt, json, langutils, logging, os, nre, sequtils,
strutils, tempfile, terminal, times, timeutils, uuids sets, strutils, tempfile, terminal, times, timeutils, uuids
import ptkutil import ptkutil
@ -19,12 +19,14 @@ let NO_MARK: Mark = (
time: fromSeconds(0).getLocalTime, time: fromSeconds(0).getLocalTime,
summary: "", notes: "", tags: @[]) summary: "", notes: "", tags: @[])
const ISO_TIME_FORMAT = "yyyy:MM:dd'T'HH:mm:ss" const ISO_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"
const TIME_FORMATS = @[ const TIME_FORMATS = @[
"H:mm", "HH:mm", "H:mm:ss", "HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd HH:mm:ss",
"yyyy:MM:dd'T'HH:mm:ss", "yyyy:MM:dd'T'HH:mm", "yyyy-MM-dd'T'HH:mm", "yyyy-MM-dd HH:mm",
"yyyy:MM:dd HH:mm:ss", "yyyy:MM:dd HH:mm"] "MM-dd'T'HH:mm:ss", "MM-dd HH:mm:ss",
"MM-dd'T'HH:mm", "MM-dd HH:mm",
"HH:mm:ss", "H:mm:ss", "H:mm", "HH:mm" ]
#proc `$`*(mark: Mark): string = #proc `$`*(mark: Mark): string =
#return (($mark.uuid)[ #return (($mark.uuid)[
@ -40,12 +42,6 @@ proc parseTime(timeStr: string): TimeInfo =
raise newException(Exception, "unable to interpret as a date: " & timeStr) raise newException(Exception, "unable to interpret as a date: " & timeStr)
proc startOfDay(ti: TimeInfo): TimeInfo =
result = ti
result.hour = 0
result.minute = 0
result.second = 0
template `%`(mark: Mark): JsonNode = template `%`(mark: Mark): JsonNode =
%* { %* {
"id": $(mark.id), "id": $(mark.id),
@ -68,9 +64,17 @@ proc loadTimeline(filename: string): Timeline =
var timeline: Timeline = (name: timelineJson["name"].getStr(), marks: @[]) var timeline: Timeline = (name: timelineJson["name"].getStr(), marks: @[])
for markJson in timelineJson["marks"]: for markJson in timelineJson["marks"]:
# TODO: an incorrect time format that was used in version 0.6 and prior.
# Version 0.7 between 1.0 support this format on read only and will write
# out the correct format (so they can be used to convert older timelines).
var time: TimeInfo
try: time = parse(markJson["time"].getStr(), ISO_TIME_FORMAT)
except: time = parse(markJson["time"].getStr(), "yyyy:MM:dd'T'HH:mm:ss")
timeline.marks.add(( timeline.marks.add((
id: parseUUID(markJson["id"].getStr()), id: parseUUID(markJson["id"].getStr()),
time: parse(markJson["time"].getStr(), ISO_TIME_FORMAT), time: time, #parse(markJson["time"].getStr(), ISO_TIME_FORMAT),
summary: markJson["summary"].getStr(), summary: markJson["summary"].getStr(),
notes: markJson["notes"].getStr(), notes: markJson["notes"].getStr(),
tags: markJson["tags"].getElems(@[]).map(proc (t: JsonNode): string = t.getStr()))) tags: markJson["tags"].getElems(@[]).map(proc (t: JsonNode): string = t.getStr())))
@ -268,6 +272,18 @@ proc filterMarkIndices(timeline: Timeline, args: Table[string, Value]): seq[int]
let e = b + 1.days let e = b + 1.days
result = result.filterIt(marks[it].time >= b and marks[it].time < e) result = result.filterIt(marks[it].time >= b and marks[it].time < e)
if args["--this-week"]:
let now = getLocalTime(getTime())
let b = now.startOfWeek(dSun)
let e = b + 7.days
result = result.filterIt(marks[it].time >= b and marks[it].time < e)
if args["--last-week"]:
let now = getLocalTime(getTime())
let e = now.startOfWeek(dSun)
let b = e - 7.days
result = result.filterIt(marks[it].time >= b and marks[it].time < e)
if args["--tags"]: if args["--tags"]:
let tags = (args["--tags"] ?: "").split({',', ';'}) let tags = (args["--tags"] ?: "").split({',', ';'})
result = result.filter(proc (i: int): bool = result = result.filter(proc (i: int): bool =
@ -290,6 +306,7 @@ Usage:
ptk add [options] ptk add [options]
ptk add [options] <summary> ptk add [options] <summary>
ptk amend [options] <id> [<summary>] ptk amend [options] <id> [<summary>]
ptk merge <timeline> [<timeline>...]
ptk stop [options] ptk stop [options]
ptk continue ptk continue
ptk delete <id> ptk delete <id>
@ -314,7 +331,9 @@ Options:
-m --matching <pattern> Restric the selection to marks matching <pattern>. -m --matching <pattern> Restric the selection to marks matching <pattern>.
-n --notes <notes> For add and amend, set the notes for a time mark. -n --notes <notes> For add and amend, set the notes for a time mark.
-t --time <time> For add and amend, use this time instead of the current time. -t --time <time> For add and amend, use this time instead of the current time.
-T --today Restrict the seelction to marks during today. -T --today Restrict the selection to marks during today.
-w --this-week Restrict the selection to marks during this week.
-W --last-week Restrict the selection to marks during the last week.
-v --verbose Include notes in timeline entry output. -v --verbose Include notes in timeline entry output.
""" """
@ -324,7 +343,7 @@ Options:
let now = getLocalTime(getTime()) let now = getLocalTime(getTime())
# Parse arguments # Parse arguments
let args = docopt(doc, version = "ptk 0.4.1") let args = docopt(doc, version = "ptk 0.7.0")
if args["--echo-args"]: echo $args if args["--echo-args"]: echo $args
@ -370,6 +389,30 @@ Options:
if args["init"]: if args["init"]:
doInit(foldl(timelineLocations, if len(a) > 0: a else: b)) doInit(foldl(timelineLocations, if len(a) > 0: a else: b))
elif args["merge"]:
let filesToMerge = args["<timeline>"]
let timelines = filesToMerge.mapIt(loadTimeline(it))
let names = timelines.mapIt(it.name).toSet
let mergedName = sequtils.toSeq(names.items).foldl(a & " + " & b)
var merged: Timeline = (
name: mergedName,
marks: @[])
for timeline in timelines:
for mark in timeline.marks:
var existingMarkIdx = merged.marks.findById($mark.id)
if existingMarkIdx >= 0:
if merged.marks[existingMarkIdx].summary != mark.summary:
merged.marks[existingMarkIdx].summary &= " | " & mark.summary
if merged.marks[existingMarkIdx].notes != mark.notes:
merged.marks[existingMarkIdx].notes &= "\r\n--------\r\b" & mark.notes
else: merged.marks.add(mark)
writeLine(stdout, pretty(%merged))
else: else:
if not fileExists(timelineLocation): if not fileExists(timelineLocation):

View File

@ -1,6 +1,6 @@
# Package # Package
version = "0.4.1" version = "0.7.0"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Personal Time Keeper" description = "Personal Time Keeper"
license = "MIT" license = "MIT"
@ -8,5 +8,5 @@ bin = @["ptk"]
# Dependencies # Dependencies
requires @["nim >= 0.15.0", "docopt >= 0.6.4", "uuids", "langutils", "tempfile", "timeutils"] requires @["nim >= 0.15.0", "docopt >= 0.6.4", "uuids", "langutils", "tempfile", "timeutils >= 0.2.0"]

View File

@ -1,3 +1,3 @@
template first*(s: seq): auto = s[0] template first*(a: openarray): auto = a[0]
template last*(s: seq): auto = s[len(s)-1] template last*(a: openarray): auto = a[len(a)-1]