Skip to content

Commit 14d355c

Browse files
authored
Add --include-overdue arg to include items before the specified due date (#98)
Closes #71 by adding an `--include-overdue` argument that includes reminders due _before_ the date specified with `--due-date` This makes it easy to recreate the "Today" view by running `reminders show-all --due-date today --include-overdue` I also added some examples of date-based usage to the README as none currently existed.
1 parent 18c9774 commit 14d355c

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ $ reminders show Soon
7272
3: Something really important (priority: high)
7373
```
7474

75+
#### Show reminders due on or by a date
76+
77+
```
78+
$ reminders show-all --due-date today
79+
1: Contribute to open source (in 3 hours)
80+
$ reminders show-all --due-date today --include-overdue
81+
0: Ship reminders-cli (2 days ago)
82+
1: Contribute to open source (in 3 hours)
83+
$ reminders show-all --due-date 2025-02-16
84+
1: Contribute to open source (in 3 hours)
85+
$ reminders show Soon --due-date today --include-overdue
86+
0: Ship reminders-cli (2 days ago)
87+
1: Contribute to open source (in 3 hours)
88+
```
89+
7590
#### See help for more examples
7691

7792
```

Sources/RemindersLibrary/CLI.swift

+10-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ private struct ShowAll: ParsableCommand {
2626
@Flag(help: "Include completed items in output")
2727
var includeCompleted = false
2828

29+
@Flag(help: "When using --due-date, also include items due before the due date")
30+
var includeOverdue = false
31+
2932
@Option(
3033
name: .shortAndLong,
3134
help: "Show only reminders due on this date")
@@ -52,7 +55,8 @@ private struct ShowAll: ParsableCommand {
5255
}
5356

5457
reminders.showAllReminders(
55-
dueOn: self.dueDate, displayOptions: displayOptions, outputFormat: format)
58+
dueOn: self.dueDate, includeOverdue: self.includeOverdue,
59+
displayOptions: displayOptions, outputFormat: format)
5660
}
5761
}
5862

@@ -71,6 +75,9 @@ private struct Show: ParsableCommand {
7175
@Flag(help: "Include completed items in output")
7276
var includeCompleted = false
7377

78+
@Flag(help: "When using --due-date, also include items due before the due date")
79+
var includeOverdue = false
80+
7481
@Option(
7582
name: .shortAndLong,
7683
help: "Show the reminders in a specific order, one of: \(Sort.commaSeparatedCases)")
@@ -107,8 +114,8 @@ private struct Show: ParsableCommand {
107114
}
108115

109116
reminders.showListItems(
110-
withName: self.listName, dueOn: self.dueDate, displayOptions: displayOptions,
111-
outputFormat: format, sort: sort, sortOrder: sortOrder)
117+
withName: self.listName, dueOn: self.dueDate, includeOverdue: self.includeOverdue,
118+
displayOptions: displayOptions, outputFormat: format, sort: sort, sortOrder: sortOrder)
112119
}
113120
}
114121

Sources/RemindersLibrary/Reminders.swift

+13-6
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ public final class Reminders {
100100
}
101101
}
102102

103-
func showAllReminders(dueOn dueDate: DateComponents?,
104-
displayOptions: DisplayOptions, outputFormat: OutputFormat) {
103+
func showAllReminders(dueOn dueDate: DateComponents?, includeOverdue: Bool,
104+
displayOptions: DisplayOptions, outputFormat: OutputFormat
105+
) {
105106
let semaphore = DispatchSemaphore(value: 0)
106107
let calendar = Calendar.current
107108

@@ -120,7 +121,10 @@ public final class Reminders {
120121

121122
let sameDay = calendar.compare(
122123
reminderDueDate, to: dueDate, toGranularity: .day) == .orderedSame
123-
if sameDay {
124+
let earlierDay = calendar.compare(
125+
reminderDueDate, to: dueDate, toGranularity: .day) == .orderedAscending
126+
127+
if sameDay || (includeOverdue && earlierDay) {
124128
matchingReminders.append((reminder, i, listName))
125129
}
126130
}
@@ -140,8 +144,8 @@ public final class Reminders {
140144
semaphore.wait()
141145
}
142146

143-
func showListItems(withName name: String, dueOn dueDate: DateComponents?, displayOptions: DisplayOptions,
144-
outputFormat: OutputFormat, sort: Sort, sortOrder: CustomSortOrder)
147+
func showListItems(withName name: String, dueOn dueDate: DateComponents?, includeOverdue: Bool,
148+
displayOptions: DisplayOptions, outputFormat: OutputFormat, sort: Sort, sortOrder: CustomSortOrder)
145149
{
146150
let semaphore = DispatchSemaphore(value: 0)
147151
let calendar = Calendar.current
@@ -162,7 +166,10 @@ public final class Reminders {
162166

163167
let sameDay = calendar.compare(
164168
reminderDueDate, to: dueDate, toGranularity: .day) == .orderedSame
165-
if sameDay {
169+
let earlierDay = calendar.compare(
170+
reminderDueDate, to: dueDate, toGranularity: .day) == .orderedAscending
171+
172+
if sameDay || (includeOverdue && earlierDay) {
166173
matchingReminders.append((reminder, index))
167174
}
168175
}

0 commit comments

Comments
 (0)