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
|
use fmt;
use dirs;
use bufio;
use memio;
use os;
use fs;
use path;
use io;
use strings;
use format::ini;
use strconv;
use encoding::utf8;
use getopt;
use sort;
type task = struct {
name: str,
path: str,
context: (str | void),
tags: ([]str | void),
priority: uint,
content: str,
};
def METADATASEP: str = "----";
type rtaskerror = !(fs::error | ini::error | strconv::error | io::error |
utf8::invalid);
fn listtasks(root: str = "tasks", context: str = "*") ([]task | rtaskerror |
path::error) = {
const dirents = os::readdir(root)?;
defer fs::dirents_free(dirents);
let tasks: []task = [];
for (const dirent .. dirents) {
if (context == "*" || dirent.name == context) {
if (fs::isdir(dirent.ftype)) {
let buf = path::init()?;
const p = path::push(&buf, root, dirent.name)?;
append(tasks, listtasks(p)?...);
} else {
let buf = path::init()?;
const p = path::push(&buf, root, dirent.name)?;
let t = readtask(p)?;
append(tasks, t);
};
};
};
return tasks;
};
fn readtask(taskpath: str) (task | rtaskerror) = {
const f = os::open(taskpath)?;
defer io::close(f)!;
const sc = bufio::newscanner(f);
defer bufio::finish(&sc);
const content = memio::dynamic();
defer io::close(&content)!;
const meta = memio::dynamic();
defer io::close(&meta)!;
let currentst = &meta;
for (let line => bufio::scan_line(&sc)?) {
line = strings::trim(line);
if (line == METADATASEP) {
currentst = &content;
continue;
};
memio::concat(currentst, line, "\n")?;
};
// seek to start of memio buffer
io::seek(&meta, 0, io::whence::SET)?;
const sc = ini::scan(&meta);
defer ini::finish(&sc);
let t = task {
context = void,
tags = void,
...
};
for (let entry: ini::entry => ini::next(&sc)?) {
switch (entry.1) {
case "name" =>
t.name = strings::dup(strings::trim(entry.2));
case "priority" =>
t.priority = strconv::stou(entry.2)?;
case =>
void;
};
};
if (t.name == "") {
t.name = strings::dup(path::basename(taskpath));
};
t.path = strings::dup(taskpath);
t.content = strings::dup(strings::trim(memio::string(&content)?));
return t;
};
fn freetask(t: *task) void = {
free(t.name);
free(t.path);
free(t.content);
if (t.context is str) {
free(t.context as str);
};
if (t.tags is []str) {
strings::freeall(t.tags as []str);
};
};
fn finishall(tasks: []task) void = {
for (const task .. tasks) {
freetask(&task);
};
};
fn sortname(a: const *opaque, b: const *opaque) int = {
const a = a: *task;
const b = b: *task;
return strings::compare(a.name, b.name);
};
export fn main() void = {
const cmd = getopt::parse(os::args,
"tasklist",
('f', "path", "tasks directory"),
("filter", ["filter tasks", "id"]: []getopt::help),
("f", ["filter tasks", "id"]: []getopt::help),
("show", ["show task details", "id"]: []getopt::help),
("s", ["show task details", "id"]: []getopt::help),
("add", ["add a task", "id"]: []getopt::help),
("a", ["add a task", "id"]: []getopt::help),
("write", ["write a task", "id"]: []getopt::help),
("w", ["write a task", "id"]: []getopt::help),
("done", ["delete a task", "id"]: []getopt::help),
("d", ["delete a task", "id"]: []getopt::help),
("tsv", ["print tsv of tasks"]: []getopt::help),
("t", ["print tsv of tasks"]: []getopt::help),
);
defer getopt::finish(&cmd);
const cfg: config = config {
tasksdir = "tasks",
};
for (let opt .. cmd.opts) {
switch (opt.0) {
case 'f' =>
cfg.tasksdir = opt.1;
case =>
abort();
};
};
const tasks = match (listtasks(cfg.tasksdir)) {
case let e: fs::error =>
fmt::fatal(fs::strerror(e));
case let e: path::error =>
fmt::fatal(path::strerror(e));
case let tasks: []task =>
yield tasks;
};
defer finishall(tasks);
sort::sort(tasks: []opaque, size(task), &sortname);
const com: (str, *getopt::command) = match (cmd.subcmd) {
case void =>
listall(cfg, tasks);
return;
case let subcmd: (str, *getopt::command) =>
yield (subcmd.0, subcmd.1);
};
match (execcommand(cfg, com.0, tasks, com.1)) {
case let e: error =>
fmt::fatal(strerror(e));
case =>
void;
};
};
|