@harborppm/cli 0.119.0 → 0.121.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/args.mjs CHANGED
@@ -32,8 +32,42 @@ export function parseArgs(argv, { flags = [], options = [], repeat = [] } = {})
32
32
  }
33
33
  values[key] = repeat.includes(key) ? [...(values[key] ?? []), value] : value;
34
34
  } else {
35
- throw new Error(`Unknown option --${key}.`);
35
+ const near = nearest(key, [...flags, ...options, ...repeat]);
36
+ throw new Error(`Unknown option --${key}.${near ? ` Did you mean --${near}?` : ""}`);
36
37
  }
37
38
  }
38
39
  return { positionals, values };
39
40
  }
41
+
42
+ /** How many single-letter edits (and swaps of neighbours) turn one word into another. */
43
+ function editDistance(a, b) {
44
+ const rows = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]);
45
+ for (let j = 1; j <= b.length; j++) rows[0][j] = j;
46
+ for (let i = 1; i <= a.length; i++) {
47
+ for (let j = 1; j <= b.length; j++) {
48
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
49
+ rows[i][j] = Math.min(rows[i - 1][j] + 1, rows[i][j - 1] + 1, rows[i - 1][j - 1] + cost);
50
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1])
51
+ rows[i][j] = Math.min(rows[i][j], rows[i - 2][j - 2] + 1);
52
+ }
53
+ }
54
+ return rows[a.length][b.length];
55
+ }
56
+
57
+ /**
58
+ * The option a mistyped one was most likely meant to be: within two edits
59
+ * (one for a short word), or the one it begins, when only one fits.
60
+ */
61
+ export function nearest(typed, known) {
62
+ const word = typed.toLowerCase();
63
+ if (!word) return null;
64
+ const starts = known.filter((k) => k.startsWith(word));
65
+ if (starts.length === 1) return starts[0];
66
+ const reach = word.length <= 4 ? 1 : 2;
67
+ const scored = known
68
+ .map((k) => ({ k, d: editDistance(word, k) }))
69
+ .filter((x) => x.d <= reach)
70
+ .sort((x, y) => x.d - y.d);
71
+ if (!scored.length || (scored[1] && scored[1].d === scored[0].d)) return null;
72
+ return scored[0].k;
73
+ }
package/lib/cli.mjs CHANGED
@@ -210,8 +210,11 @@ export const HELP = `harbor: Harbor from the terminal
210
210
  the date the owner's queue
211
211
  suggests (harbor help add)
212
212
  harbor start TASK | submit TASK | done TASK [--confirm] [--over-limit]
213
- move a task to In play, In review
214
- or Done
213
+ [--comment TEXT] move a task to In play, In review
214
+ or Done; several keys at once
215
+ (harbor done RR-4 RR-7) move each
216
+ in turn; --comment leaves a note
217
+ on each as it moves
215
218
  harbor edit TASK [--title T] [--assignee me|none|NAME]
216
219
  [--also "NAME, NAME"|none] [--due DATE|pilot|none]
217
220
  [--starts DATE|none]
@@ -418,11 +421,14 @@ export const HELP = `harbor: Harbor from the terminal
418
421
  takes --status all. Lists say your board's own name for a column it
419
422
  renamed; --json keeps Harbor's word in status and the board's in column.
420
423
  DATE: YYYY-MM-DD, today, tomorrow, or +N for N days from today.
424
+ On a terminal the answers are coloured: keys, what's late, health. Piped
425
+ or with --json they're plain; --no-color or NO_COLOR=1 keeps them plain.
421
426
  Changes show in Harbor's History as yours, "via Harbor CLI". These commands
422
427
  never delete; the full-screen app can, as the website can, and asks first,
423
428
  and so can harbor fog --sweep's x (drop it), one task at a time.
424
429
  Practice: a new harbor starts with First voyage, a project of practice
425
- tasks that tick themselves off as you try things; the brief, harbor week,
430
+ tasks that tick themselves off as you try things, from here too; harbor add
431
+ puts a task there while it's your only project. The brief, harbor week,
426
432
  bridge and forecast leave it out. To look around a made-up harbor instead,
427
433
  open harborppm.com/demo or run ssh try.harborppm.com.
428
434
  `;
@@ -586,15 +592,25 @@ const REPEAT_WORD = { weekly: "every week", biweekly: "every two weeks", monthly
586
592
 
587
593
  const WEEKDAYS = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
588
594
 
589
- /** The one open project a task can go in (First voyage aside), or why --project is needed. */
590
- async function onlyProject(client, harbor, say = "--project NAME") {
595
+ /** The one open project a task can go in (First voyage only when it is all there is), or why --project is needed. */
596
+ async function onlyProject(
597
+ client,
598
+ harbor,
599
+ say = "--project NAME",
600
+ { practice: orPractice = false } = {},
601
+ ) {
591
602
  const r = await client.call("list_projects", {
592
603
  harbor,
593
604
  health: ["on-track", "at-risk", "off-track", "paused"],
594
605
  limit: 5,
595
606
  });
596
- const open = (r.projects ?? []).filter((p) => p.id !== "voyage");
607
+ const all = r.projects ?? [];
608
+ const open = all.filter((p) => p.id !== "voyage");
597
609
  if (open.length === 1) return open[0].name;
610
+ // A new harbor has First voyage alone, and its first task says to add one:
611
+ // the task goes there, as it does from the website's New.
612
+ const practice = all.find((p) => p.id === "voyage");
613
+ if (open.length === 0 && practice && orPractice) return practice.name;
598
614
  if (open.length === 0)
599
615
  throw new HarborError(
600
616
  'This harbor has no open project yet; harbor project "NAME" --program P makes one.',
@@ -2055,6 +2071,15 @@ async function harborNamed(client, key) {
2055
2071
  }
2056
2072
 
2057
2073
  const TASK_KEY = /^[A-Za-z][A-Za-z0-9]{0,9}-\d+$/;
2074
+ /** How many keys harbor start, submit and done take in one go. */
2075
+ const MANY_KEYS = 20;
2076
+
2077
+ /** First voyage's practice tasks this change ticked off, said once under the answer. */
2078
+ function practiceLine(r) {
2079
+ const ticked = Array.isArray(r?.practice) ? r.practice : [];
2080
+ if (!ticked.length) return "";
2081
+ return `First voyage: ticked off ${ticked.map((t) => `“${t}”`).join(" and ")}.`;
2082
+ }
2058
2083
 
2059
2084
  /** True when every word of `words` is somewhere in `name`, any case. */
2060
2085
  const nameHas = (name, words) =>
@@ -2576,7 +2601,11 @@ export async function run(argv, io = {}) {
2576
2601
  );
2577
2602
  // The prompt's counts are behind now; its next draw fetches new ones.
2578
2603
  if (name === "create_task" || name === "update_task") await stalePromptCache(env);
2579
- out(asJson ? jsonText(result) : plain(show(result)));
2604
+ out(
2605
+ asJson
2606
+ ? jsonText(result)
2607
+ : plain([show(result), practiceLine(result)].filter(Boolean).join("\n")),
2608
+ );
2580
2609
  return 0;
2581
2610
  };
2582
2611
  let asJson = false;
@@ -3158,7 +3187,9 @@ export async function run(argv, io = {}) {
3158
3187
  : await readTitles(client, harbor, titles, values);
3159
3188
  // No --project and no #Project: the harbor's one open project, if that's all there is.
3160
3189
  if (!values.project && !reads.every((r) => r.project))
3161
- values.project = await onlyProject(client, harbor, "--project NAME or #NAME in the title");
3190
+ values.project = await onlyProject(client, harbor, "--project NAME or #NAME in the title", {
3191
+ practice: true,
3192
+ });
3162
3193
  const projectOf = (r) => values.project ?? r.project?.value;
3163
3194
  // One task as --json lists it, the same for a dry run and the real thing.
3164
3195
  const taskEntry = (t, title) => ({
@@ -3337,6 +3368,7 @@ export async function run(argv, io = {}) {
3337
3368
  ...(r.pilot ? { pilot: r.pilot } : {}),
3338
3369
  ...(r.milestoneReached ? { milestoneReached: r.milestoneReached } : {}),
3339
3370
  ...(r.milestoneReopened ? { milestoneReopened: r.milestoneReopened } : {}),
3371
+ ...(r.practice?.length ? { practice: r.practice } : {}),
3340
3372
  },
3341
3373
  );
3342
3374
  const where = [...new Set(tasks.map((t) => t.project).filter(Boolean))];
@@ -3378,6 +3410,7 @@ export async function run(argv, io = {}) {
3378
3410
  ...(r.milestoneReopened
3379
3411
  ? [`${r.milestoneReopened} is open again: it has an open task now.`]
3380
3412
  : []),
3413
+ ...(practiceLine(r) ? [practiceLine(r)] : []),
3381
3414
  ].join("\n"),
3382
3415
  )
3383
3416
  .join("\n"),
@@ -3392,7 +3425,7 @@ export async function run(argv, io = {}) {
3392
3425
  const { positionals, values, harbor } = parse(
3393
3426
  command === "review"
3394
3427
  ? spec(["confirm", "over-limit", "mine", "done"], ["owner", "portfolio"])
3395
- : spec(["confirm", "over-limit"]),
3428
+ : spec(["confirm", "over-limit"], ["comment"]),
3396
3429
  );
3397
3430
  if (command === "review") {
3398
3431
  // The review only reads: a task key here once moved the task, so it says where that went.
@@ -3431,32 +3464,80 @@ export async function run(argv, io = {}) {
3431
3464
  );
3432
3465
  }
3433
3466
  const status = { start: "doing", submit: "review", done: "done" }[command];
3434
- return call(
3435
- "update_task",
3436
- {
3437
- harbor,
3438
- task: one(positionals, "task key, like RR-42"),
3439
- status,
3440
- confirmed: values.confirm || undefined,
3441
- overLimit: values["over-limit"] || undefined,
3442
- },
3443
- (r) =>
3444
- [
3445
- `${r.updated.title}: ${r.reopened ? "reopened, " : ""}${{ doing: "started", review: "in review", done: "done" }[status]}.`,
3446
- r.queuedNext ? `Next one: ${taskLine(r.queuedNext, opts)}` : "",
3447
- r.milestoneReached
3448
- ? `That was the last of its tasks: ${r.milestoneReached} is reached.`
3449
- : "",
3450
- r.milestoneReopened
3451
- ? `${r.milestoneReopened} is open again: one of its tasks is open.`
3452
- : "",
3453
- r.projectFinished
3454
- ? `That was the last open task in ${r.projectFinished}. Mark the project Done in the app (harbor) or on the website.`
3455
- : "",
3456
- ]
3457
- .filter(Boolean)
3458
- .join("\n"),
3459
- );
3467
+ const moved = (r) =>
3468
+ [
3469
+ `${r.updated.title}: ${r.reopened ? "reopened, " : ""}${{ doing: "started", review: "in review", done: "done" }[status]}.`,
3470
+ r.commented ? "Your comment is on it." : "",
3471
+ r.queuedNext ? `Next one: ${taskLine(r.queuedNext, opts)}` : "",
3472
+ r.milestoneReached
3473
+ ? `That was the last of its tasks: ${r.milestoneReached} is reached.`
3474
+ : "",
3475
+ r.milestoneReopened
3476
+ ? `${r.milestoneReopened} is open again: one of its tasks is open.`
3477
+ : "",
3478
+ r.projectFinished
3479
+ ? `That was the last open task in ${r.projectFinished}. Mark the project Done in the app (harbor) or on the website.`
3480
+ : "",
3481
+ ]
3482
+ .filter(Boolean)
3483
+ .join("\n");
3484
+ const move = (task) => ({
3485
+ harbor,
3486
+ task,
3487
+ status,
3488
+ confirmed: values.confirm || undefined,
3489
+ overLimit: values["over-limit"] || undefined,
3490
+ });
3491
+ // --comment says why, on the task's own thread, once the move has landed:
3492
+ // a move that's refused leaves no comment behind.
3493
+ const note = values.comment === undefined ? undefined : values.comment.trim();
3494
+ if (note === "") throw new HarborError("--comment needs some words.");
3495
+ const moveOne = async (task) => {
3496
+ const r = await client.call("update_task", move(task));
3497
+ if (!note) return r;
3498
+ const c = await client.call("add_comment", { harbor, task, body: note });
3499
+ return { ...r, commented: c.commented, mentioned: c.mentioned };
3500
+ };
3501
+ // Several keys: each in turn, as if typed one after another. Only keys,
3502
+ // so a title typed without quotes is never read as several tasks.
3503
+ const keys = [...new Map(positionals.map((k) => [k.toUpperCase(), k])).values()];
3504
+ if (positionals.length > 1 && positionals.every((k) => TASK_KEY.test(k))) {
3505
+ if (keys.length > MANY_KEYS)
3506
+ throw new HarborError(`That's ${keys.length} tasks; ${MANY_KEYS} at a time at most.`);
3507
+ const results = [];
3508
+ for (const key of keys) {
3509
+ try {
3510
+ const r = await moveOne(key);
3511
+ results.push({ task: key.toUpperCase(), ...r });
3512
+ } catch (err) {
3513
+ if (!(err instanceof HarborError)) throw err;
3514
+ results.push({ task: key.toUpperCase(), error: hint(err.message) });
3515
+ }
3516
+ }
3517
+ await stalePromptCache(env);
3518
+ const failed = results.filter((r) => r.error).length;
3519
+ if (asJson) out(jsonText({ results, moved: results.length - failed, failed }));
3520
+ else {
3521
+ const lines = results.map((r) =>
3522
+ r.error
3523
+ ? `${r.task}: ${r.error}`
3524
+ : [moved(r), practiceLine(r)].filter(Boolean).join("\n"),
3525
+ );
3526
+ if (failed && results.length > failed)
3527
+ lines.push(`${results.length - failed} of ${results.length} moved.`);
3528
+ out(plain(lines.join("\n")));
3529
+ }
3530
+ return failed ? 1 : 0;
3531
+ }
3532
+ const task = one(positionals, "task key, like RR-42");
3533
+ if (!note) return call("update_task", move(task), moved);
3534
+ const r = await moveOne(task).catch((err) => {
3535
+ if (err instanceof HarborError) throw new HarborError(hint(err.message));
3536
+ throw err;
3537
+ });
3538
+ await stalePromptCache(env);
3539
+ out(asJson ? jsonText(r) : plain([moved(r), practiceLine(r)].filter(Boolean).join("\n")));
3540
+ return 0;
3460
3541
  }
3461
3542
  case "edit": {
3462
3543
  const { positionals, values, harbor } = parse(
package/lib/format.mjs CHANGED
@@ -1630,7 +1630,9 @@ export function milestonesText(r, { done = false } = {}) {
1630
1630
  ? [`None ${r.program ? `in ${r.program} ` : ""}reached yet.`]
1631
1631
  : [];
1632
1632
  const hidden = r.reachedHidden
1633
- ? `${r.reachedHidden} reached ${r.reachedHidden === 1 ? "one" : "ones"} not shown (--done shows them).`
1633
+ ? r.reachedHidden === 1
1634
+ ? "1 reached milestone not shown (--done shows it)."
1635
+ : `${r.reachedHidden} reached milestones not shown (--done shows them).`
1634
1636
  : "";
1635
1637
  if (!list.length) {
1636
1638
  const none = r.program ? `${r.program} has no open milestones.` : "No open milestones.";
@@ -1820,7 +1822,12 @@ export function wbsText(r) {
1820
1822
  : `${l.percent}% (${l.done}/${l.tasks})`,
1821
1823
  l.owner || null,
1822
1824
  dates,
1823
- l.late ? `${l.late} late` : null,
1825
+ // A task on its own is late or it isn't; anything with work under it says how many.
1826
+ l.late
1827
+ ? (l.kind === "task" || l.kind === "subtask") && !l.subtasks
1828
+ ? "late"
1829
+ : `${l.late} late`
1830
+ : null,
1824
1831
  l.runsPastEnd ? "runs past its end" : null,
1825
1832
  ].filter(Boolean);
1826
1833
  out.push(` ${lead} ${l.name}${l.key ? ` ${l.key}` : ""} (${bits.join(" · ")})`);
package/lib/paint.mjs ADDED
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Colour for the commands' answers, on a terminal only. Harbor's lines keep
3
+ * one shape (`KEY Title (status · late, due … · owner · project)`), so the
4
+ * colour is laid over the finished text rather than threaded through every
5
+ * formatter: a task's key stands out, the details in brackets step back, and
6
+ * the few words that ask for attention (late, off track, at risk) are the
7
+ * only ones in a warm colour. Piped, redirected, with --json, with NO_COLOR
8
+ * set or with --no-color, the text is exactly what it was.
9
+ *
10
+ * The colours are the terminal's own sixteen, so they follow whatever theme
11
+ * the person chose, light or dark; nothing here picks a shade for them.
12
+ */
13
+
14
+ const ESC = "\x1b[";
15
+ const RESET = `${ESC}0m`;
16
+ const wrap = (code) => (s) => (s ? `${ESC}${code}m${s}${RESET}` : s);
17
+ const dim = wrap("2");
18
+ const bold = wrap("1");
19
+ const cyan = wrap("36");
20
+ const red = wrap("31");
21
+ const yellow = wrap("33");
22
+ const green = wrap("32");
23
+
24
+ /** Commands whose answer is data for another program, or draws itself. */
25
+ const PLAIN_COMMANDS = new Set(["export", "prompt", "calendar", "version"]);
26
+
27
+ /**
28
+ * Whether this run's answers are coloured. `--no-color` and NO_COLOR (any
29
+ * value but empty) always win; FORCE_COLOR turns it on where there's no
30
+ * terminal to ask, as other tools take it.
31
+ * @param {{ argv: string[], env?: Record<string, string | undefined>, stream?: { isTTY?: boolean } }} opts
32
+ */
33
+ export function wantsColour({ argv, env = process.env, stream = process.stdout }) {
34
+ if (argv.includes("--no-color") || argv.includes("--no-colour")) return false;
35
+ if (argv.includes("--json") || argv.includes("--csv")) return false;
36
+ const command = argv.find((a) => !a.startsWith("-"));
37
+ if (command && PLAIN_COMMANDS.has(command)) return false;
38
+ if (argv.includes("--version") || argv.includes("-v")) return false;
39
+ if (env.NO_COLOR) return false;
40
+ if (env.FORCE_COLOR && env.FORCE_COLOR !== "0") return true;
41
+ return Boolean(stream.isTTY) && env.TERM !== "dumb";
42
+ }
43
+
44
+ /** argv without the colour switches, which every command takes. */
45
+ export function withoutColourFlags(argv) {
46
+ return argv.filter((a) => a !== "--no-color" && a !== "--no-colour");
47
+ }
48
+
49
+ /** One detail between the dots: the few that ask for attention keep a colour. */
50
+ function paintBit(bit) {
51
+ const t = bit.trim();
52
+ if (/^(late\b|\d+ late$|was due\b|off track$|overdue\b|runs past its end$)/.test(t))
53
+ return red(bit);
54
+ if (/^(at risk$|changes asked for\b)/.test(t)) return yellow(bit);
55
+ if (/^on track$/.test(t)) return green(bit);
56
+ return dim(bit);
57
+ }
58
+
59
+ /** "a · b · c" with each part painted and the dots dimmed. */
60
+ function paintBits(text) {
61
+ return text
62
+ .split(" · ")
63
+ .map((bit) =>
64
+ // "5 open, 1 late": the late half is the news.
65
+ /, \d+ late$/.test(bit)
66
+ ? `${dim(bit.slice(0, bit.lastIndexOf(", ") + 2))}${red(bit.slice(bit.lastIndexOf(", ") + 2))}`
67
+ : paintBit(bit),
68
+ )
69
+ .join(dim(" · "));
70
+ }
71
+
72
+ /** A task key as Harbor writes one: RR-42, FV-1. */
73
+ const KEY = "[A-Z][A-Z0-9]{0,9}-\\d+";
74
+ const LEADING_KEY = new RegExp(`^(\\s*(?:(?:Added|Would add|Next one): )?)(${KEY})( )`);
75
+ /** The details at a line's end: two spaces, then one pair of brackets. */
76
+ const DETAILS = /^(.*\S)( {2})\(([^()]*(?:\([^()]*\)[^()]*)*)\)$/;
77
+ /** harbor help's lines: the command, a gap, what it does. */
78
+ const HELP_LINE = /^( {2}harbor(?: \S+)*?)( {2,})(\S.*)$/;
79
+ /** A line that only carries on the description above it. */
80
+ const HELP_MORE = /^ {20,}\S/;
81
+ /** A second line of details under a row: indented, parts between dots. */
82
+ const UNDER = /^( {4,})(\S.* · .*)$/;
83
+
84
+ /**
85
+ * One line with colour laid over it. A line that already carries an escape
86
+ * (the banner draws its own) is left alone.
87
+ * @param {string} line
88
+ * @param {{ help?: boolean }} [opts]
89
+ */
90
+ export function paintLine(line, opts = {}) {
91
+ if (!line || line.includes("\x1b")) return line;
92
+ if (opts.help) {
93
+ const help = HELP_LINE.exec(line);
94
+ if (help) return `${help[1]}${help[2]}${dim(help[3])}`;
95
+ if (HELP_MORE.test(line)) return dim(line);
96
+ return line;
97
+ }
98
+ let head = line;
99
+ let tail = "";
100
+ const details = DETAILS.exec(line);
101
+ if (details) {
102
+ head = details[1];
103
+ tail = `${details[2]}${dim("(")}${paintBits(details[3])}${dim(")")}`;
104
+ }
105
+ const key = LEADING_KEY.exec(head);
106
+ if (key) head = `${key[1]}${cyan(key[2])}${key[3]}${head.slice(key[0].length)}`;
107
+ else if (!tail) {
108
+ const under = UNDER.exec(line);
109
+ if (under) return `${under[1]}${paintBits(under[2])}`;
110
+ }
111
+ return `${head}${tail}`;
112
+ }
113
+
114
+ /**
115
+ * A whole answer, line by line. The first line of a list with a count
116
+ * ("Aya: 5 open, 1 late") is its heading, so it's bold.
117
+ * @param {string} text
118
+ * @param {{ help?: boolean }} [opts]
119
+ */
120
+ export function paint(text, opts = {}) {
121
+ const lines = String(text).split("\n");
122
+ return lines
123
+ .map((line, i) => {
124
+ if (
125
+ !opts.help &&
126
+ i === 0 &&
127
+ lines.length > 2 &&
128
+ lines[1] === "" &&
129
+ !line.includes("\x1b") &&
130
+ !/^\s/.test(line) &&
131
+ !DETAILS.test(line)
132
+ )
133
+ return bold(line);
134
+ return paintLine(line, opts);
135
+ })
136
+ .join("\n");
137
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harborppm/cli",
3
- "version": "0.119.0",
3
+ "version": "0.121.0",
4
4
  "description": "Harbor from the terminal: a full-screen app with everything, plus commands for scripts and agents.",
5
5
  "keywords": [
6
6
  "harbor",