aboutsummaryrefslogtreecommitdiff
path: root/hatask.ha
blob: 39841653bc1bd8f4d0334f640a9e00dfbbd401c2 (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
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;

type task = struct {
	name: 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)?;
				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.content = strings::dup(strings::trim(memio::string(&content)?));
	return t;
};

fn freetask(t: *task) void = {
	free(t.name);
	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);
	};
};

export fn main() void = {
	const cmd = getopt::parse(os::args,
		"tasklist",
		"cmd",
	);
	defer getopt::finish(&cmd);

	const com: []str = if (len(cmd.args) > 0) {
		yield cmd.args;
	} else {
		yield ["filter"];
	};

	const tasks = match (listtasks()) {
	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);

	execcommand(com[0], tasks, com[1..]...)!;
};