aboutsummaryrefslogtreecommitdiff
path: root/cmd.ha
diff options
context:
space:
mode:
authorJulian Hurst <ark@mansus.space>2024-11-15 01:14:07 +0100
committerJulian Hurst <ark@mansus.space>2024-11-15 01:14:07 +0100
commit4792b321eb3bf7b58fdddcc101a15bd83dad5d0f (patch)
treeda726a995bcc7af2bdfdf42f9571326e4ce7c2be /cmd.ha
parent6a79b5e7c37c846dcb4451d5a2ec33e771e857af (diff)
downloadhatask-4792b321eb3bf7b58fdddcc101a15bd83dad5d0f.tar.gz
Add write, show and filter commands
Diffstat (limited to 'cmd.ha')
-rw-r--r--cmd.ha106
1 files changed, 106 insertions, 0 deletions
diff --git a/cmd.ha b/cmd.ha
new file mode 100644
index 0000000..ff37c79
--- /dev/null
+++ b/cmd.ha
@@ -0,0 +1,106 @@
+use fmt;
+use strings;
+use getopt;
+use os;
+use io;
+use path;
+use os::exec;
+use strconv;
+
+type error = !(io::error | path::error | exec::error);
+
+type func = fn(tasks: []task, args: str...) (void | task | error);
+
+type command = struct {
+ names: []str,
+ func: *func,
+};
+
+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,
+ },
+];
+
+fn execcommand(name: str, tasks: []task, args: str...) (void | error) = {
+ for (const c .. commands) {
+ for (const n .. c.names) {
+ if (n == name) {
+ c.func(tasks, args...)?;
+ };
+ };
+ };
+};
+
+fn write(tasks: []task, args: str...) (void | task | error) = {
+ const cmdname = "task write";
+ const cmdhelp: []getopt::help = ["write a task", "<name>"];
+ if (len(args) == 0z) {
+ getopt::printhelp(os::stderr, cmdname, cmdhelp)?;
+ return;
+ };
+ const cmd = getopt::parse(args,
+ cmdname: getopt::help,
+ cmdhelp[0],
+ cmdhelp[1],
+ );
+ defer getopt::finish(&cmd);
+
+ if (len(args) != 1z) {
+ getopt::printhelp(os::stderr, cmdname, cmdhelp)?;
+ return;
+ };
+
+ let buf = path::init()?;
+ const p = path::push(&buf, "tasks", args[0])?;
+ const c = exec::cmd("vim", p)?;
+ exec::exec(&c);
+};
+
+fn show(tasks: []task, args: str...) (void | task | error) = {
+ for (const t .. tasks) {
+ for (const s .. args) {
+ if (t.name == s) {
+ fmt::println(t.content)!;
+ };
+ };
+ };
+};
+
+fn printtask(t: task) (void | error) = {
+ const pad = PADDING - len(t.name) + len(strconv::utos(t.priority));
+ fmt::printfln("{}{%}", t.name, t.priority, &fmt::mods {
+ pad = ' ',
+ width = pad, ...
+ })!;
+};
+
+fn filter(tasks: []task, args: str...) (void | task | error) = {
+ const headpad = PADDING - len("name") + len("priority");
+ fmt::printfln("name{%}", "priority", &fmt::mods {
+ pad = ' ',
+ width = headpad,
+ ...
+ })!;
+ for (const t .. tasks) {
+ if (len(args) == 0z) {
+ printtask(t)?;
+ };
+ for (const s .. args) {
+ if (strings::contains(t.name, s)) {
+ printtask(t)?;
+ };
+ };
+ };
+};