Skip to content

Commit 6e91b3b

Browse files
authored
Add completion for reminders list names (#46)
Fixes #37
1 parent ff413cc commit 6e91b3b

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

Sources/RemindersLibrary/CLI.swift

+16-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ private struct Show: ParsableCommand {
1717
abstract: "Print the items on the given list")
1818

1919
@Argument(
20-
help: "The list to print items from, see 'show-lists' for names")
20+
help: "The list to print items from, see 'show-lists' for names",
21+
completion: .custom(listNameCompletion))
2122
var listName: String
2223

2324
@Flag(help: "Show completed items only")
@@ -56,7 +57,8 @@ private struct Add: ParsableCommand {
5657
abstract: "Add a reminder to a list")
5758

5859
@Argument(
59-
help: "The list to add to, see 'show-lists' for names")
60+
help: "The list to add to, see 'show-lists' for names",
61+
completion: .custom(listNameCompletion))
6062
var listName: String
6163

6264
@Argument(
@@ -88,7 +90,8 @@ private struct Complete: ParsableCommand {
8890
abstract: "Complete a reminder")
8991

9092
@Argument(
91-
help: "The list to complete a reminder on, see 'show-lists' for names")
93+
help: "The list to complete a reminder on, see 'show-lists' for names",
94+
completion: .custom(listNameCompletion))
9295
var listName: String
9396

9497
@Argument(
@@ -105,7 +108,8 @@ private struct Delete: ParsableCommand {
105108
abstract: "Delete a reminder")
106109

107110
@Argument(
108-
help: "The list to delete a reminder on, see 'show-lists' for names")
111+
help: "The list to delete a reminder on, see 'show-lists' for names",
112+
completion: .custom(listNameCompletion))
109113
var listName: String
110114

111115
@Argument(
@@ -117,12 +121,19 @@ private struct Delete: ParsableCommand {
117121
}
118122
}
119123

124+
func listNameCompletion(_ arguments: [String]) -> [String] {
125+
// NOTE: A list name with ':' was separated in zsh completion, there might be more of these or
126+
// this might break other shells
127+
return reminders.getListNames().map { $0.replacingOccurrences(of: ":", with: "\\:") }
128+
}
129+
120130
private struct Edit: ParsableCommand {
121131
static let configuration = CommandConfiguration(
122132
abstract: "Edit the text of a reminder")
123133

124134
@Argument(
125-
help: "The list to edit a reminder on, see 'show-lists' for names")
135+
help: "The list to edit a reminder on, see 'show-lists' for names",
136+
completion: .custom(listNameCompletion))
126137
var listName: String
127138

128139
@Argument(

Sources/RemindersLibrary/Reminders.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ public final class Reminders {
6868
return grantedAccess
6969
}
7070

71+
func getListNames() -> [String] {
72+
return self.getCalendars().map { $0.title }
73+
}
74+
7175
func showLists() {
72-
let calendars = self.getCalendars()
73-
for calendar in calendars {
74-
print(calendar.title)
76+
for name in self.getListNames() {
77+
print(name)
7578
}
7679
}
7780

0 commit comments

Comments
 (0)