prowm/main.ts
2025-02-06 02:21:37 +01:00

128 lines
3.6 KiB
TypeScript

import { Board } from "./board.ts";
import { Renderer } from "./renderer.ts";
import { Resolver } from "./resolver.ts";
const help_message = `
prown - simwle prowek mawawger
commands:
general:
help
tasks:
task-add #ID TITLE [INDEX]
task-edit #ID TITLE
task-delete #ID
task-move src:#ID dest:#ID [INDEX]
columns:
column-add TITLE [IDX]
column-edit IDX TITLE
column-delete IDX
column-move IDX DELTA
`.trim();
const board = new Board("Skill issues");
while (true) {
const raster = board.rasterize();
const resolver = new Resolver(raster);
const text = new Renderer(raster).render();
console.log(text);
const line = prompt("> ");
if (!line) {
break;
}
const [cmd, ...args] = line
.split("")
.reduce<[string[], string]>(
([acc, strCh], tok) =>
strCh === ""
? tok === "'" || tok === '"'
? [acc, tok]
: tok === " "
? [[tok.trim(), ...acc], strCh]
: [[acc[0] + tok, ...acc.slice(1)], strCh]
: tok === strCh
? [acc, ""]
: [[acc[0] + tok, ...acc.slice(1)], strCh],
[[""], ""],
)[0]
.toReversed();
switch (cmd) {
case "help":
console.log(help_message);
break;
case "add":
case "task-add": {
const path = parsePath(args[0], resolver);
const content = args[1];
const idx = args[2] && parseInt(args[2]) || 0;
board.addTask(path, idx, content);
break;
}
case "edit":
case "task-edit": {
const path = parsePath(args[0], resolver);
const content = args[1];
board.editTask(path, content);
break;
}
case "delete":
case "task-delete": {
const path = parsePath(args[0], resolver);
board.deleteTask(path);
break;
}
case "move":
case "task-move": {
const srcPath = parsePath(args[0], resolver);
const destPath = parsePath(args[1], resolver);
const idx = args[2] && parseInt(args[2]) || 0;
board.moveTask(srcPath, destPath, idx);
break;
}
case "cadd":
case "column-add": {
const title = args[0];
const idx = args[1] && parseInt(args[1]) || 0;
board.addColumn(idx, title);
break;
}
case "cedit":
case "column-edit": {
const idx = parseInt(args[0]);
const title = args[1];
board.editColumn(idx, title);
break;
}
case "cdelete":
case "column-delete": {
const idx = parseInt(args[0]);
board.deleteColumn(idx);
break;
}
case "cmove":
case "column-move": {
const idx = parseInt(args[0]);
const delta = parseInt(args[1]);
board.moveColumn(idx, delta);
break;
}
default:
console.log(`unknown command '${cmd}'`);
}
}
function parsePath(text: string, resolver: Resolver): number[] {
if (text.startsWith(".")) {
return [parseInt(text.slice(1))];
}
if (text.startsWith("#")) {
return resolver.taskIdPath(parseInt(text.slice(1)));
}
return resolver.taskIdPath(parseInt(text));
}