From 86ef1210a686bce0db3ead2760bf4d401e23883a Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Fri, 15 Nov 2024 01:32:41 +0100 Subject: Create config struct and pass to commands --- cmd.ha | 18 +++++++++--------- config.ha | 3 +++ hatask.ha | 12 +++++++----- 3 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 config.ha diff --git a/cmd.ha b/cmd.ha index 290ff72..4c85b0a 100644 --- a/cmd.ha +++ b/cmd.ha @@ -11,7 +11,7 @@ use format::tsv; type error = !(!str | io::error | path::error | exec::error | strconv::error); -type func = fn(tasks: []task, args: arguments) (void | task | error); +type func = fn(cfg: config, tasks: []task, args: arguments) (void | task | error); type command = struct { names: []str, @@ -45,17 +45,17 @@ const commands: [_]command = [ }, ]; -fn execcommand(name: str, tasks: []task, args: arguments) (void | error) = { +fn execcommand(cfg: config, name: str, tasks: []task, args: arguments) (void | error) = { for (const c .. commands) { for (const n .. c.names) { if (n == name) { - c.func(tasks, args)?; + c.func(cfg, tasks, args)?; }; }; }; }; -fn write(tasks: []task, a: arguments) (void | task | error) = { +fn write(cfg: config, tasks: []task, a: arguments) (void | task | error) = { const args = a.args; if (len(args) != 1z) { getopt::printhelp(os::stderr, "write", a.help)?; @@ -72,7 +72,7 @@ fn write(tasks: []task, a: arguments) (void | task | error) = { exec::exec(&c); }; -fn show(tasks: []task, a: arguments) (void | task | error) = { +fn show(cfg: config, tasks: []task, a: arguments) (void | task | error) = { const args = a.args; const id = strconv::stoz(args[0])?; const t = if (len(tasks) > id) { @@ -112,7 +112,7 @@ fn printtask(t: task, id: size) (void | error) = { )!; }; -fn do(tasks: []task, a: arguments) (void | task | error) = { +fn do(cfg: config, tasks: []task, a: arguments) (void | task | error) = { if (len(a.args) != 1z) { getopt::printhelp(os::stderr, "done", a.help)?; os::exit(os::status::FAILURE); @@ -123,7 +123,7 @@ fn do(tasks: []task, a: arguments) (void | task | error) = { fmt::printfln("Task {}: \"{}\" done (deleted)", id, t.name)!; }; -fn filter(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"); fmt::printfln("id{%}{%}", @@ -155,7 +155,7 @@ fn filter(tasks: []task, a: arguments) (void | task | error) = { }; }; -fn tsv(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"])!; for (let i = 0z; i < len(tasks); i += 1) { const t = tasks[i]; @@ -163,7 +163,7 @@ fn tsv(tasks: []task, a: arguments) (void | task | error) = { }; }; -fn listall(tasks: []task) void = { +fn listall(cfg: config, tasks: []task) void = { const headpad = PADDING - len("name") + len("priority"); const namepad = 10 - len("id") + len("name"); fmt::printfln("id{%}{%}", diff --git a/config.ha b/config.ha new file mode 100644 index 0000000..e465294 --- /dev/null +++ b/config.ha @@ -0,0 +1,3 @@ +type config = struct { + tasksdir: str, +}; diff --git a/hatask.ha b/hatask.ha index d15007f..7807000 100644 --- a/hatask.ha +++ b/hatask.ha @@ -137,18 +137,20 @@ export fn main() void = { ); defer getopt::finish(&cmd); - let tasksdir: str = "tasks"; + const cfg: config = config { + tasksdir = "tasks", + }; for (let opt .. cmd.opts) { switch (opt.0) { case 'f' => - tasksdir = opt.1; + cfg.tasksdir = opt.1; case => abort(); }; }; - const tasks = match (listtasks(tasksdir)) { + const tasks = match (listtasks(cfg.tasksdir)) { case let e: fs::error => fmt::fatal(fs::strerror(e)); case let e: path::error => @@ -161,13 +163,13 @@ export fn main() void = { const com: (str, *getopt::command) = match (cmd.subcmd) { case void => - listall(tasks); + listall(cfg, tasks); return; case let subcmd: (str, *getopt::command) => yield (subcmd.0, subcmd.1); }; - match (execcommand(com.0, tasks, com.1)) { + match (execcommand(cfg, com.0, tasks, com.1)) { case let e: error => fmt::fatal(strerror(e)); case => -- cgit v1.2.3