Skip to content

Commit

Permalink
Add show-all command (#47)
Browse files Browse the repository at this point in the history
This shows all reminders across all lists, formatting them with the list
name, number, and then normal info:

```
$ reminders show-all
To Do: 1: Something
```

This is mostly useful when combined with `--due-date`, so you can show
all reminders due today, etc.
  • Loading branch information
keith authored Dec 15, 2022
1 parent 13df73d commit 348f5da
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
15 changes: 15 additions & 0 deletions Sources/RemindersLibrary/CLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ private struct ShowLists: ParsableCommand {
}
}

private struct ShowAll: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Print all reminders")

@Option(
name: .shortAndLong,
help: "Show only reminders due on this date")
var dueDate: DateComponents?

func run() {
reminders.showAllReminders(dueOn: self.dueDate)
}
}

private struct Show: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Print the items on the given list")
Expand Down Expand Up @@ -182,6 +196,7 @@ public struct CLI: ParsableCommand {
Show.self,
ShowLists.self,
NewList.self,
ShowAll.self,
]
)

Expand Down
51 changes: 41 additions & 10 deletions Sources/RemindersLibrary/Reminders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ private extension EKReminder {
}
}

private func format(_ reminder: EKReminder, at index: Int) -> String {
private func format(_ reminder: EKReminder, at index: Int, listName: String? = nil) -> String {
let dateString = formattedDueDate(from: reminder).map { " (\($0))" } ?? ""
let priorityString = Priority(reminder.mappedPriority).map { " (priority: \($0))" } ?? ""
return "\(index): \(reminder.title ?? "<unknown>")\(dateString)\(priorityString)"
let listString = listName.map { "\($0): " } ?? ""
return "\(listString)\(index): \(reminder.title ?? "<unknown>")\(dateString)\(priorityString)"
}

public enum DisplayOptions: String, Decodable {
Expand Down Expand Up @@ -78,11 +79,40 @@ public final class Reminders {
}
}

func showAllReminders(dueOn dueDate: DateComponents?) {
let semaphore = DispatchSemaphore(value: 0)
let calendar = Calendar.current

self.reminders(on: self.getCalendars(), displayOptions: .incomplete) { reminders in
for (i, reminder) in reminders.enumerated() {
let listName = reminder.calendar.title
guard let dueDate = dueDate?.date else {
print(format(reminder, at: i, listName: listName))
continue
}

guard let reminderDueDate = reminder.dueDateComponents?.date else {
continue
}

let sameDay = calendar.compare(
reminderDueDate, to: dueDate, toGranularity: .day) == .orderedSame
if sameDay {
print(format(reminder, at: i, listName: listName))
}
}

semaphore.signal()
}

semaphore.wait()
}

func showListItems(withName name: String, dueOn dueDate: DateComponents?, displayOptions: DisplayOptions) {
let semaphore = DispatchSemaphore(value: 0)
let calendar = Calendar.current

self.reminders(onCalendar: self.calendar(withName: name), displayOptions: displayOptions) { reminders in
self.reminders(on: [self.calendar(withName: name)], displayOptions: displayOptions) { reminders in
for (i, reminder) in reminders.enumerated() {
guard let dueDate = dueDate?.date else {
print(format(reminder, at: i))
Expand Down Expand Up @@ -151,7 +181,7 @@ public final class Reminders {
let calendar = self.calendar(withName: name)
let semaphore = DispatchSemaphore(value: 0)

self.reminders(onCalendar: calendar, displayOptions: .incomplete) { reminders in
self.reminders(on: [calendar], displayOptions: .incomplete) { reminders in
guard let reminder = reminders[safe: index] else {
print("No reminder at index \(index) on \(name)")
exit(1)
Expand All @@ -176,7 +206,7 @@ public final class Reminders {
let calendar = self.calendar(withName: name)
let semaphore = DispatchSemaphore(value: 0)

self.reminders(onCalendar: calendar, displayOptions: .incomplete) { reminders in
self.reminders(on: [calendar], displayOptions: .incomplete) { reminders in
guard let reminder = reminders[safe: index] else {
print("No reminder at index \(index) on \(name)")
exit(1)
Expand All @@ -201,7 +231,7 @@ public final class Reminders {
let calendar = self.calendar(withName: name)
let semaphore = DispatchSemaphore(value: 0)

self.reminders(onCalendar: calendar, displayOptions: .incomplete) { reminders in
self.reminders(on: [calendar], displayOptions: .incomplete) { reminders in
guard let reminder = reminders[safe: index] else {
print("No reminder at index \(index) on \(name)")
exit(1)
Expand Down Expand Up @@ -240,11 +270,12 @@ public final class Reminders {

// MARK: - Private functions

private func reminders(onCalendar calendar: EKCalendar,
displayOptions: DisplayOptions,
completion: @escaping (_ reminders: [EKReminder]) -> Void)
private func reminders(
on calendars: [EKCalendar],
displayOptions: DisplayOptions,
completion: @escaping (_ reminders: [EKReminder]) -> Void)
{
let predicate = Store.predicateForReminders(in: [calendar])
let predicate = Store.predicateForReminders(in: calendars)
Store.fetchReminders(matching: predicate) { reminders in
let reminders = reminders?
.filter { self.shouldDisplay(reminder: $0, displayOptions: displayOptions) }
Expand Down

0 comments on commit 348f5da

Please sign in to comment.