Skip to content

Commit a7a062f

Browse files
authored
Improve JSON output and add display options to ShowAll (#69)
- Add `displayOptions` to `ShowAll` command - Set correct `location` and `locationTitle` (new). Fix formatting of `completionDate`.
1 parent 91c1d93 commit a7a062f

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

Sources/RemindersLibrary/CLI.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ private struct ShowLists: ParsableCommand {
1919
private struct ShowAll: ParsableCommand {
2020
static let configuration = CommandConfiguration(
2121
abstract: "Print all reminders")
22+
23+
@Flag(help: "Show completed items only")
24+
var onlyCompleted = false
25+
26+
@Flag(help: "Include completed items in output")
27+
var includeCompleted = false
2228

2329
@Option(
2430
name: .shortAndLong,
@@ -30,8 +36,23 @@ private struct ShowAll: ParsableCommand {
3036
help: "format, either of 'plain' or 'json'")
3137
var format: OutputFormat = .plain
3238

39+
func validate() throws {
40+
if self.onlyCompleted && self.includeCompleted {
41+
throw ValidationError(
42+
"Cannot specify both --show-completed and --only-completed")
43+
}
44+
}
45+
3346
func run() {
34-
reminders.showAllReminders(dueOn: self.dueDate, outputFormat: format)
47+
var displayOptions = DisplayOptions.incomplete
48+
if self.onlyCompleted {
49+
displayOptions = .complete
50+
} else if self.includeCompleted {
51+
displayOptions = .all
52+
}
53+
54+
reminders.showAllReminders(
55+
dueOn: self.dueDate, displayOptions: displayOptions, outputFormat: format)
3556
}
3657
}
3758

Sources/RemindersLibrary/EKReminder+Encodable.swift

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extension EKReminder: Encodable {
77
case notes
88
case url
99
case location
10+
case locationTitle
1011
case completionDate
1112
case isCompleted
1213
case priority
@@ -23,24 +24,37 @@ extension EKReminder: Encodable {
2324
try container.encode(self.priority, forKey: .priority)
2425
try container.encode(self.calendar.title, forKey: .list)
2526
try container.encodeIfPresent(self.notes, forKey: .notes)
27+
28+
// url field is nil
29+
// https://developer.apple.com/forums/thread/128140
2630
try container.encodeIfPresent(self.url, forKey: .url)
27-
try container.encodeIfPresent(self.location, forKey: .location)
28-
try container.encodeIfPresent(self.completionDate, forKey: .completionDate)
31+
try container.encodeIfPresent(format(self.completionDate), forKey: .completionDate)
2932

30-
if let startDateComponents = self.startDateComponents {
31-
if #available(macOS 12.0, *) {
32-
try container.encode(startDateComponents.date?.ISO8601Format(), forKey: .startDate)
33-
} else {
34-
try container.encode(startDateComponents.date?.description(with: .current), forKey: .startDate)
33+
for alarm in self.alarms ?? [] {
34+
if let location = alarm.structuredLocation {
35+
try container.encodeIfPresent(location.title, forKey: .locationTitle)
36+
if let geoLocation = location.geoLocation {
37+
let geo = "\(geoLocation.coordinate.latitude), \(geoLocation.coordinate.longitude)"
38+
try container.encode(geo, forKey: .location)
39+
}
40+
break
3541
}
3642
}
3743

44+
if let startDateComponents = self.startDateComponents {
45+
try container.encodeIfPresent(format(startDateComponents.date), forKey: .startDate)
46+
}
47+
3848
if let dueDateComponents = self.dueDateComponents {
39-
if #available(macOS 12.0, *) {
40-
try container.encode(dueDateComponents.date?.ISO8601Format(), forKey: .dueDate)
41-
} else {
42-
try container.encode(dueDateComponents.date?.description(with: .current), forKey: .dueDate)
43-
}
49+
try container.encodeIfPresent(format(dueDateComponents.date), forKey: .dueDate)
50+
}
51+
}
52+
53+
private func format(_ date: Date?) -> String? {
54+
if #available(macOS 12.0, *) {
55+
return date?.ISO8601Format()
56+
} else {
57+
return date?.description(with: .current)
4458
}
4559
}
4660
}

Sources/RemindersLibrary/Reminders.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,12 @@ public final class Reminders {
9090
}
9191
}
9292

93-
func showAllReminders(dueOn dueDate: DateComponents?, outputFormat: OutputFormat) {
93+
func showAllReminders(dueOn dueDate: DateComponents?,
94+
displayOptions: DisplayOptions, outputFormat: OutputFormat) {
9495
let semaphore = DispatchSemaphore(value: 0)
9596
let calendar = Calendar.current
9697

97-
self.reminders(on: self.getCalendars(), displayOptions: .incomplete) { reminders in
98+
self.reminders(on: self.getCalendars(), displayOptions: displayOptions) { reminders in
9899
var matchingReminders = [(EKReminder, Int, String)]()
99100
for (i, reminder) in reminders.enumerated() {
100101
let listName = reminder.calendar.title

0 commit comments

Comments
 (0)