summaryrefslogtreecommitdiff
path: root/internal/askcli/commands_registry.go
blob: 4d8d9ba089969cfa983a2da9390302354ab01ddc (plain)
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
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
}

type commandTable struct {
	entries []commandEntry
	lookup  map[string]int
}

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}
}

func (t commandTable) get(name string) (*commandEntry, bool) {
	idx, ok := t.lookup[name]
	if !ok {
		return nil, false
	}
	return &t.entries[idx], true
}

func (t commandTable) rootCompletionEntries() []commandEntry {
	var entries []commandEntry
	for _, entry := range t.entries {
		if entry.includeInCompletion {
			entries = append(entries, entry)
		}
	}
	return entries
}

func (t commandTable) singleSelectorNames() []string {
	var names []string
	for _, entry := range t.entries {
		if entry.singleSelector {
			names = append(names, entry.name)
		}
	}
	return names
}

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,
	},
	{
		name:                "all",
		description:         "List all tasks",
		handler:             wrapSimpleCommand((*Dispatcher).handleAll),
		includeInCompletion: true,
	},
	{
		name:                "ready",
		description:         "List READY tasks",
		handler:             wrapSimpleCommand((*Dispatcher).handleReady),
		includeInCompletion: true,
	},
	{
		name:                "info",
		description:         "Show task details",
		handler:             wrapSimpleCommand((*Dispatcher).handleInfo),
		includeInCompletion: true,
		singleSelector:      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,
	},
})

func init() {
	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",
		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,
	})
}