aboutsummaryrefslogtreecommitdiff
path: root/cmd.ha
blob: dd4afc1d0f547a5ec52b3c96db24fbe4fd976074 (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
use fmt;
use strings;
use getopt;
use os;
use io;
use path;
use os::exec;
use strconv;
use ascii;

type error = !(!str | io::error | path::error | exec::error | strconv::error);

type func = fn(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 = ["w", "write"],
		func = &write,
	},
	command {
		names = ["d", "done"],
		func = &do,
	},
];

fn execcommand(name: str, tasks: []task, args: arguments) (void | error) = {
	for (const c .. commands) {
		for (const n .. c.names) {
			if (n == name) {
				c.func(tasks, args)?;
			};
		};
	};
};

fn write(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(tasks: []task, a: arguments) (void | task | error) = {
	const args = a.args;
	const id = strconv::stoz(args[0])?;
	const t = if (len(tasks) > id) {
		yield tasks[id];
	} else {
		return "No such task";
	};
	fmt::println(t.content)!;
};

fn printtask(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);
	fmt::printfln("{}{%}{%}", id, name, &fmt::mods {
			pad = ' ',
			width = namepad,
			...
		},
		t.priority, &fmt::mods {
			pad = ' ',
			width = pad,
			...
		},
	)!;
};

fn do(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])?;
	const t = tasks[id];
	os::remove(t.path)?;
	fmt::printfln("Task {}: \"{}\" done (deleted)", id, t.name)!;
};

fn filter(tasks: []task, a: arguments) (void | task | error) = {
	const headpad = PADDING - len("name") + len("priority");
	const namepad = 10 - len("id") + len("name");
	fmt::printfln("id{%}{%}",
		"name", &fmt::mods {
			pad = ' ',
			width = namepad,
			...
		},"priority", &fmt::mods {
			pad = ' ',
			width = headpad,
			...
		},
	)!;
	const args = a.args;
	for (let i = 0z; i < len(tasks); i += 1) {
		const t = tasks[i];
		if (len(args) == 0z) {
			printtask(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(t, i)?;
			};
		};
	};
};

fn listall(tasks: []task) void = {
	const headpad = PADDING - len("name") + len("priority");
	const namepad = 10 - len("id") + len("name");
	fmt::printfln("id{%}{%}",
		"name", &fmt::mods {
			pad = ' ',
			width = namepad,
			...
		},"priority", &fmt::mods {
			pad = ' ',
			width = headpad,
			...
		},
	)!;
	for (let i = 0z; i < len(tasks); i += 1) {
		printtask(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);
	};
};