aboutsummaryrefslogtreecommitdiff
path: root/hatask.ha
blob: ea8cf7db7779ff2cbcb5d698d704b9b1bcce0602 (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
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
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;
use fnmatch;
use errors;

type task = struct {
	name: str,
	path: str,
	context: (str | void),
	tags: []str,
	priority: uint,
	content: str,
};

def METADATASEP: str = "----";

type rtaskerror = !(fs::error | ini::error | strconv::error | io::error |
	utf8::invalid | path::error);

fn listtasks(root: str = "tasks", context: str = "*") ([]task | rtaskerror) = {
	const dirents = os::readdir(root)?;
	defer fs::dirents_free(dirents);
	let tasks: []task = [];
	for (const dirent .. dirents) {
		if (fs::isdir(dirent.ftype)) {
			if (context == "*" || fnmatch::fnmatch(context, dirent.name)) {
				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)?) {
		if (currentst != &content) {
			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 isc = ini::scan(&meta);
	defer ini::finish(&isc);
	let t = task {
		context = void,
		tags = [],
		...
	};
	for (let entry: ini::entry => ini::next(&isc)?) {
		switch (entry.1) {
		case "name" =>
			t.name = strings::dup(strings::trim(entry.2));
		case "priority" =>
			t.priority = strconv::stou(entry.2)?;
		case "tags" =>
			t.tags = strings::dupall(strings::split(entry.2, ","));
		case =>
			void;
		};
	};
	if (t.name == "") {
		t.name = strings::dup(path::basename(taskpath));
	};
	t.path = strings::dup(taskpath);
	t.content = strings::dup(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);
	};
	strings::freeall(t.tags);
};

fn finishall(tasks: []task) void = {
	for (const task .. tasks) {
		freetask(&task);
	};
};

fn sortpriority(a: const *opaque, b: const *opaque) int = {
	const a = a: *task;
	const b = b: *task;
	return (b.priority - a.priority): int;
};

fn sortname(a: const *opaque, b: const *opaque) int = {
	const a = a: *task;
	const b = b: *task;
	return strings::compare(a.name, b.name);
};

fn strrtaskerror(e: rtaskerror) str = {
	return match (e) {
	case let e: fs::error =>
		yield fs::strerror(e);
	case let e: ini::error =>
		yield ini::strerror(e);
	case let e: strconv::error =>
		yield strconv::strerror(e);
	case let e: io::error =>
		yield io::strerror(e);
	case let e: utf8::invalid =>
		yield utf8::strerror(e);
	case let e: path::error =>
		yield path::strerror(e);
	};
};

fn filtertags(cfg: config, tasks: *[]task) void = {
	if (len(cfg.tags) == 0z) {
		return;
	};
	for (let i = 0z; i < len(tasks); i += 1) {
		const t = tasks[i];
		let found = false;
		 for :tagloop (let tag .. t.tags) {
			for (let tagfilter .. cfg.tags) {
				if (tag == tagfilter) {
					found = true;
					break :tagloop;
				};
			};
		};
		if (!found) {
			freetask(&tasks[i]);
			delete(tasks[i]);
			i -= 1;
		};
	};
};

export fn main() void = {
	const cmd = getopt::parse(os::args,
		"tasklist",
		('f', "path", "tasks directory"),
		('c', "context", "context filter"),
		('t', "tags", "tags filter"),
		('p', "sort by priority"),
		('d', "activate debug mode"),
		("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 = match (readconfig()) {
	case let cfg: config =>
		yield cfg;
	case let e: fs::error =>
		match (e) {
		case let e: errors::noentry =>
			fmt::fatal("No config file found");
		case =>
			fmt::fatal(fs::strerror(e));
		};
	case let e: cerror =>
		fmt::fatal(strcerror(e));
	};
	defer cfinish(&cfg);

	let sortfn: *sort::cmpfunc = &sortname;
	for (let opt .. cmd.opts) {
		switch (opt.0) {
		case 'f' =>
			cfg.tasksdir = strings::dup(opt.1);
		case 'c' =>
			cfg.context = strings::dup(opt.1);
		case 't' =>
			cfg.tags = strings::dupall(strings::split(opt.1, ","));
		case 'p' =>
			sortfn = &sortpriority;
		case 'd' =>
			cfg.debug = true;
		case =>
			abort();
		};
	};

	if (cfg.debug) {
		fmt::errorln("config:")!;
		printconfig(cfg);
	};

	const tasks = match (listtasks(cfg.tasksdir, cfg.context)) {
	case let e: rtaskerror =>
		fmt::fatal(strrtaskerror(e));
	case let tasks: []task =>
		yield tasks;
	};
	defer finishall(tasks);
	sort::sort(tasks: []opaque, size(task), sortfn);
	filtertags(cfg, &tasks);

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