From ba38c9e073db90b530f2ba4460fb124a930ea055 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Fri, 15 Nov 2024 13:29:02 +0100 Subject: Support tags --- cmd.ha | 37 +++++++++++++++++++++++++++++-------- hatask.ha | 10 +++++----- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/cmd.ha b/cmd.ha index 1e520e6..cc81304 100644 --- a/cmd.ha +++ b/cmd.ha @@ -104,8 +104,10 @@ fn printtasktsv(cfg: config, t: task, id: size) (void | error) = { defer free(sid); const spriority = strconv::ztos(t.priority); const buf = path::init(t.path)?; + const stags = strings::join(",", t.tags...); + defer free(stags); const p = path::trimprefix(&buf, cfg.tasksdir)?; - tsv::writerecord(os::stdout, [sid, t.name, spriority, p])!; + tsv::writerecord(os::stdout, [sid, t.name, spriority, stags, p])!; }; fn printtask(cfg: config, t: task, id: size) (void | error) = { @@ -115,12 +117,16 @@ fn printtask(cfg: config, t: task, id: size) (void | error) = { 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); const buf = path::init(t.path)?; + const stags = strings::join(",", t.tags...); + defer free(stags); + const tagspad = 15 - len(strconv::utos(t.priority)) + len(stags); const p = path::trimprefix(&buf, cfg.tasksdir)?; - const pathpad = 15 - len(strconv::utos(t.priority)) + len(p); - fmt::printfln("{}{%}{%}{%}", id, name, &fmt::mods { + const pathpad = 20 - len(stags) + len(p); + fmt::printfln("{}{%}{%}{%}{%}", id, name, &fmt::mods { pad = ' ', width = namepad, ... @@ -130,6 +136,11 @@ fn printtask(cfg: config, t: task, id: size) (void | error) = { width = pad, ... }, + stags, &fmt::mods { + pad = ' ', + width = tagspad, + ... + }, p, &fmt::mods { pad = ' ', width = pathpad, @@ -155,8 +166,9 @@ fn do(cfg: config, tasks: []task, a: arguments) (void | task | error) = { fn filter(cfg: config, tasks: []task, a: arguments) (void | task | error) = { const headpad = PADDING - len("name") + len("priority"); const namepad = 10 - len("id") + len("name"); - const pathpad = 15 - len("priority") + len("path"); - fmt::printfln("id{%}{%}{%}", + const tagspad = 15 - len("priority") + len("tags"); + const pathpad = 20 - len("priority") + len("path"); + fmt::printfln("id{%}{%}{%}{%}", "name", &fmt::mods { pad = ' ', width = namepad, @@ -165,6 +177,10 @@ fn filter(cfg: config, tasks: []task, a: arguments) (void | task | error) = { pad = ' ', width = headpad, ... + },"tags", &fmt::mods { + pad = ' ', + width = tagspad, + ... },"path", &fmt::mods { pad = ' ', width = pathpad, @@ -190,7 +206,7 @@ fn filter(cfg: config, tasks: []task, a: arguments) (void | task | error) = { }; fn tsv(cfg: config, tasks: []task, a: arguments) (void | task | error) = { - tsv::writerecord(os::stdout, ["id" ,"name", "priority", "path"])!; + tsv::writerecord(os::stdout, ["id" ,"name", "priority", "tags", "path"])!; for (let i = 0z; i < len(tasks); i += 1) { const t = tasks[i]; printtasktsv(cfg, t, i)?; @@ -200,8 +216,9 @@ fn tsv(cfg: config, tasks: []task, a: arguments) (void | task | error) = { fn listall(cfg: config, tasks: []task) void = { const headpad = PADDING - len("name") + len("priority"); const namepad = 10 - len("id") + len("name"); - const pathpad = 15 - len("priority") + len("path"); - fmt::printfln("id{%}{%}{%}", + const tagspad = 15 - len("priority") + len("tags"); + const pathpad = 20 - len("tags") + len("path"); + fmt::printfln("id{%}{%}{%}{%}", "name", &fmt::mods { pad = ' ', width = namepad, @@ -210,6 +227,10 @@ fn listall(cfg: config, tasks: []task) void = { pad = ' ', width = headpad, ... + },"tags", &fmt::mods { + pad = ' ', + width = tagspad, + ... },"path", &fmt::mods { pad = ' ', width = pathpad, diff --git a/hatask.ha b/hatask.ha index ef8b51c..534a150 100644 --- a/hatask.ha +++ b/hatask.ha @@ -19,7 +19,7 @@ type task = struct { name: str, path: str, context: (str | void), - tags: ([]str | void), + tags: []str, priority: uint, content: str, }; @@ -76,7 +76,7 @@ fn readtask(taskpath: str) (task | rtaskerror) = { defer ini::finish(&sc); let t = task { context = void, - tags = void, + tags = [], ... }; for (let entry: ini::entry => ini::next(&sc)?) { @@ -85,6 +85,8 @@ fn readtask(taskpath: str) (task | rtaskerror) = { 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; }; @@ -104,9 +106,7 @@ fn freetask(t: *task) void = { if (t.context is str) { free(t.context as str); }; - if (t.tags is []str) { - strings::freeall(t.tags as []str); - }; + strings::freeall(t.tags); }; fn finishall(tasks: []task) void = { -- cgit v1.2.3