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
260
261
262
263
264
265
266
267
268
|
use fmt;
use strings;
use getopt;
use os;
use io;
use path;
use os::exec;
use strconv;
use ascii;
use format::tsv;
type error = !(!str | io::error | path::error | exec::error | strconv::error);
type func = fn(cfg: config, tasks: []task, args: arguments) (void | task | error);
type command = struct {
names: []str,
func: *func,
};
type arguments = *getopt::command;
const PADDING: size = 30z;
const commands: [_]command = [
command {
names = ["f", "filter"],
func = &filter,
},
command {
names = ["s", "show"],
func = &show,
},
command {
names = ["a", "add"],
func = &add,
},
command {
names = ["w", "write"],
func = &write,
},
command {
names = ["d", "done"],
func = &do,
},
command {
names = ["t", "tsv"],
func = &tsv,
},
];
fn execcommand(cfg: config, name: str, tasks: []task, args: arguments) (void | error) = {
for (const c .. commands) {
for (const n .. c.names) {
if (n == name) {
c.func(cfg, tasks, args)?;
};
};
};
};
fn add(cfg: config, tasks: []task, a: arguments) (void | task | error) = {
const args = a.args;
if (len(args) != 1z) {
getopt::printhelp(os::stderr, "write", a.help)?;
return;
};
let buf = path::init(cfg.tasksdir, args[0])?;
const c = exec::cmd("vim", path::string(&buf))?;
exec::exec(&c);
};
fn write(cfg: config, tasks: []task, a: arguments) (void | task | error) = {
const args = a.args;
if (len(args) != 1z) {
getopt::printhelp(os::stderr, "write", a.help)?;
return;
};
const id = strconv::stoz(args[0])?;
const t = if (len(tasks) > id) {
yield tasks[id];
} else {
return "No such task";
};
const c = exec::cmd("vim", t.path)?;
exec::exec(&c);
};
fn show(cfg: config, tasks: []task, a: arguments) (void | task | error) = {
const args = a.args;
// if no args, print all
if (len(args) == 0) {
for (const t .. tasks) {
fmt::println(t.content)!;
};
return;
};
for (const arg .. args) {
const id = strconv::stoz(arg)?;
const t = if (len(tasks) > id) {
yield tasks[id];
} else {
return "No such task";
};
fmt::println(t.content)!;
};
};
fn printtasktsv(cfg: config, t: task, id: size) (void | error) = {
const sid = strings::dup(strconv::ztos(id));
defer free(sid);
const spriority = strconv::ztos(t.priority);
const buf = path::init(t.path)?;
const stags = strings::join(",", t.tags...);
defer free(stags);
const p = path::trimprefix(&buf, cfg.tasksdir)?;
tsv::writerecord(os::stdout, [sid, t.name, spriority, stags, p])!;
};
fn printtask(cfg: config, t: task, id: size) (void | error) = {
let name = strings::dup(t.name);
defer free(name);
if (len(t.name) > PADDING - 4) {
name = strings::concat(strings::sub(t.name, 0z, PADDING - 4),
"...");
};
const pad = PADDING - len(name) + len(strconv::utos(t.priority));
const namepad = 10 - len(strconv::ztos(id)) + len(name);
const buf = path::init(t.path)?;
const stags = strings::join(",", t.tags...);
defer free(stags);
const tagspad = 15 - len(strconv::utos(t.priority)) + len(stags);
const p = path::trimprefix(&buf, cfg.tasksdir)?;
const pathpad = 20 - len(stags) + len(p);
fmt::printfln("{}{%}{%}{%}{%}", id, name, &fmt::mods {
pad = ' ',
width = namepad,
...
},
t.priority, &fmt::mods {
pad = ' ',
width = pad,
...
},
stags, &fmt::mods {
pad = ' ',
width = tagspad,
...
},
p, &fmt::mods {
pad = ' ',
width = pathpad,
...
},
)!;
};
fn do(cfg: config, tasks: []task, a: arguments) (void | task | error) = {
if (len(a.args) != 1z) {
getopt::printhelp(os::stderr, "done", a.help)?;
os::exit(os::status::FAILURE);
};
const id = strconv::stoz(a.args[0])?;
if (id >= len(tasks)) {
return "No such task";
};
const t = tasks[id];
os::remove(t.path)?;
fmt::printfln("Task {}: \"{}\" done (deleted)", id, t.name)!;
};
fn filter(cfg: config, tasks: []task, a: arguments) (void | task | error) = {
const headpad = PADDING - len("name") + len("priority");
const namepad = 10 - len("id") + len("name");
const tagspad = 15 - len("priority") + len("tags");
const pathpad = 20 - len("priority") + len("path");
fmt::printfln("id{%}{%}{%}{%}",
"name", &fmt::mods {
pad = ' ',
width = namepad,
...
},"priority", &fmt::mods {
pad = ' ',
width = headpad,
...
},"tags", &fmt::mods {
pad = ' ',
width = tagspad,
...
},"path", &fmt::mods {
pad = ' ',
width = pathpad,
...
},
)!;
const args = a.args;
for (let i = 0z; i < len(tasks); i += 1) {
const t = tasks[i];
if (len(args) == 0z) {
printtask(cfg, t, i)?;
};
for (const s .. args) {
const lname = ascii::strlower(t.name);
defer free(lname);
const ls = ascii::strlower(s);
defer free(ls);
if (strings::contains(lname, ls)) {
printtask(cfg, t, i)?;
};
};
};
};
fn tsv(cfg: config, tasks: []task, a: arguments) (void | task | error) = {
tsv::writerecord(os::stdout, ["id" ,"name", "priority", "tags", "path"])!;
for (let i = 0z; i < len(tasks); i += 1) {
const t = tasks[i];
printtasktsv(cfg, t, i)?;
};
};
fn listall(cfg: config, tasks: []task) void = {
const headpad = PADDING - len("name") + len("priority");
const namepad = 10 - len("id") + len("name");
const tagspad = 15 - len("priority") + len("tags");
const pathpad = 20 - len("tags") + len("path");
fmt::printfln("id{%}{%}{%}{%}",
"name", &fmt::mods {
pad = ' ',
width = namepad,
...
},"priority", &fmt::mods {
pad = ' ',
width = headpad,
...
},"tags", &fmt::mods {
pad = ' ',
width = tagspad,
...
},"path", &fmt::mods {
pad = ' ',
width = pathpad,
...
},
)!;
for (let i = 0z; i < len(tasks); i += 1) {
printtask(cfg, tasks[i], i)!;
};
};
fn strerror(e: error) str = {
match (e) {
case let e: !str =>
return e;
case let e: io::error =>
return io::strerror(e);
case let e: path::error =>
return path::strerror(e);
case let e: exec::error =>
return exec::strerror(e);
case let e: strconv::error =>
return strconv::strerror(e);
};
};
|