1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
package askcli
import (
"context"
"io"
)
type commandHandler func(d *Dispatcher, ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error)
type simpleCommand func(d *Dispatcher, ctx context.Context, args []string, stdout, stderr io.Writer) (int, error)
func wrapSimpleCommand(handler simpleCommand) commandHandler {
return func(d *Dispatcher, ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
return handler(d, ctx, args, stdout, stderr)
}
}
type commandEntry struct {
name string
description string
handler commandHandler
includeInCompletion bool
singleSelector bool
readOnly bool
}
type commandTable struct {
entries []commandEntry
lookup map[string]int
}
// newCommandTable builds a command table and returns a pointer to it. A
// pointer is used because add mutates the table (it grows the entries slice
// and updates the lookup map), and all methods therefore share the same
// pointer-receiver convention to keep the receiver type consistent.
func newCommandTable(entries []commandEntry) *commandTable {
lookup := make(map[string]int, len(entries))
for i := range entries {
entry := entries[i]
lookup[entry.name] = i
}
return &commandTable{entries: entries, lookup: lookup}
}
// get returns the entry registered under name. Uses a pointer receiver to
// match add, which must mutate the table.
func (t *commandTable) get(name string) (*commandEntry, bool) {
idx, ok := t.lookup[name]
if !ok {
return nil, false
}
return &t.entries[idx], true
}
// rootCompletionEntries returns the entries that should appear in top-level
// shell completion. Pointer receiver for consistency with the rest of the type.
func (t *commandTable) rootCompletionEntries() []commandEntry {
var entries []commandEntry
for _, entry := range t.entries {
if entry.includeInCompletion {
entries = append(entries, entry)
}
}
return entries
}
// singleSelectorNames returns the names of commands that take a single task
// selector. Pointer receiver for consistency with the rest of the type.
func (t *commandTable) singleSelectorNames() []string {
var names []string
for _, entry := range t.entries {
if entry.singleSelector {
names = append(names, entry.name)
}
}
return names
}
// add appends an entry and records its index in the lookup map. This mutates
// the receiver, which is why the whole type uses pointer receivers.
func (t *commandTable) add(entry commandEntry) {
t.entries = append(t.entries, entry)
t.lookup[entry.name] = len(t.entries) - 1
}
var commandRegistry = newCommandTable([]commandEntry{
{
name: "add",
description: "Create a new task",
handler: wrapSimpleCommand((*Dispatcher).handleAdd),
includeInCompletion: true,
},
{
name: "list",
description: "List active tasks",
handler: wrapSimpleCommand((*Dispatcher).handleList),
includeInCompletion: true,
readOnly: true,
},
{
name: "all",
description: "List all tasks",
handler: wrapSimpleCommand((*Dispatcher).handleAll),
includeInCompletion: true,
readOnly: true,
},
{
name: "ready",
description: "List READY tasks",
handler: wrapSimpleCommand((*Dispatcher).handleReady),
includeInCompletion: true,
readOnly: true,
},
{
name: "completed",
description: "List completed tasks",
handler: wrapSimpleCommand((*Dispatcher).handleCompleted),
includeInCompletion: true,
readOnly: true,
},
{
name: "info",
description: "Show task details",
handler: wrapSimpleCommand((*Dispatcher).handleInfo),
includeInCompletion: true,
singleSelector: true,
readOnly: true,
},
{
name: "annotate",
description: "Add an annotation",
handler: wrapSimpleCommand((*Dispatcher).handleAnnotate),
includeInCompletion: true,
singleSelector: true,
},
{
name: "start",
description: "Start a task",
handler: wrapSimpleCommand((*Dispatcher).handleStart),
includeInCompletion: true,
singleSelector: true,
},
{
name: "stop",
description: "Stop a task",
handler: wrapSimpleCommand((*Dispatcher).handleStop),
includeInCompletion: true,
singleSelector: true,
},
{
name: "done",
description: "Mark a task complete",
handler: wrapSimpleCommand((*Dispatcher).handleDone),
includeInCompletion: true,
singleSelector: true,
},
{
name: "priority",
description: "Set priority",
handler: wrapSimpleCommand((*Dispatcher).handlePriority),
includeInCompletion: true,
singleSelector: true,
},
{
name: "tag",
description: "Add or remove a tag",
handler: wrapSimpleCommand((*Dispatcher).handleTag),
includeInCompletion: true,
singleSelector: true,
},
{
name: "modify",
description: "Modify task fields",
handler: wrapSimpleCommand((*Dispatcher).handleModify),
includeInCompletion: true,
singleSelector: true,
},
{
name: "denotate",
description: "Remove an annotation",
handler: wrapSimpleCommand((*Dispatcher).handleDenotate),
includeInCompletion: true,
singleSelector: true,
},
{
name: "delete",
description: "Delete a task",
handler: (*Dispatcher).handleDelete,
includeInCompletion: true,
singleSelector: true,
},
{
name: "dep",
description: "Manage dependencies",
handler: wrapSimpleCommand((*Dispatcher).handleDep),
includeInCompletion: true,
},
{
name: "urgency",
description: "List tasks sorted by urgency",
handler: wrapSimpleCommand((*Dispatcher).handleUrgency),
includeInCompletion: true,
readOnly: true,
},
})
func init() {
commandRegistry.add(commandEntry{
name: "projects",
description: "List projects with pending, not-yet-started tasks",
handler: wrapSimpleCommand((*Dispatcher).handleProjects),
includeInCompletion: true,
readOnly: true,
})
commandRegistry.add(commandEntry{
name: "watch",
description: "Repeatedly run a subcommand and redraw when output changes",
handler: wrapSimpleCommand((*Dispatcher).handleWatch),
includeInCompletion: true,
})
commandRegistry.add(commandEntry{
name: "edit",
description: "Edit a task description in $EDITOR (or create one)",
handler: wrapSimpleCommand((*Dispatcher).handleEdit),
includeInCompletion: true,
singleSelector: true,
})
commandRegistry.add(commandEntry{
name: "fish",
description: "Emit Fish shell completion script",
handler: wrapSimpleCommand((*Dispatcher).handleFish),
includeInCompletion: true,
})
commandRegistry.add(commandEntry{
name: "help",
description: "Show help",
readOnly: true,
handler: func(d *Dispatcher, ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
_ = ctx
_ = args
_ = stdin
_ = stderr
return d.help(stdout)
},
includeInCompletion: true,
})
commandRegistry.add(commandEntry{
name: "complete-uuids",
description: "Emit task selector list",
handler: wrapSimpleCommand((*Dispatcher).handleCompleteUUIDs),
includeInCompletion: false,
})
commandRegistry.add(commandEntry{
name: "complete-aliases",
description: "Emit short task selector list for Fish completion",
handler: wrapSimpleCommand((*Dispatcher).handleCompleteAliases),
includeInCompletion: false,
})
}
|