diff options
Diffstat (limited to 'cmd.ha')
| -rw-r--r-- | cmd.ha | 106 |
1 files changed, 106 insertions, 0 deletions
@@ -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)?; + }; + }; + }; +}; |
