Public: anyone can read this; the crew can change it.
The terminal's milestones screen shows each milestone's pace: issues closed per day as a sparkline and when it lands at this pace #8077
mason/forge:mason/8058-the-terminal-s-milestones-screen-shows-e → main 3 commits fork
2 files
changed +213
−10
src/tui/screens/limbs.ts +69 −10
| @@ -12,6 +12,10 @@ | ||
| 12 | 12 | * facts (`canSeeOwnerFacts`, as on the page) also gets, under the list, what |
| 13 | 13 | * the limb under the cursor cost and landed, and its costliest issues. |
| 14 | 14 | * |
| 15 | * Under each limb with something to say, one muted line (#8058): a sparkline of | |
| 16 | * its pace's `closedPerDay`, then `pace.line` as the page shows it. A narrow | |
| 17 | * terminal drops the sparkline before the sentence; an empty line draws nothing. | |
| 18 | * | |
| 15 | 19 | * `p` hands the limb under the cursor to the planner to re-plan as fewer, |
| 16 | 20 | * larger pieces (#761): `previewReplan` lists what it will be given, then |
| 17 | 21 | * `replanLimb` on confirm. The row reads "with the planner" until it |
| @@ -48,8 +52,12 @@ import { readPlan, type Limb, type Milestone, type Plan } from "../../core/miles | ||
| 48 | 52 | import type { Ctx, Screen } from "../app/app.ts"; |
| 49 | 53 | import { binding } from "../app/keys.ts"; |
| 50 | 54 | import { fill, len, split } from "../ui/layout.ts"; |
| 55 | import { row, scrollbar } from "../ui/widgets.ts"; | |
| 56 | import { trendText } from "../../flow/kpi_words.ts"; | |
| 51 | 57 | import { style } from "../term/style.ts"; |
| 52 | 58 | import { truncate } from "../term/width.ts"; |
| 59 | import type { Rect } from "../term/screen.ts"; | |
| 60 | import type { Tokens } from "../ui/theme.ts"; | |
| 53 | 61 | import type { Session } from "../session.ts"; |
| 54 | 62 | import { issueScreen } from "./issue.ts"; |
| 55 | 63 | import { local } from "./where.ts"; |
| @@ -57,8 +65,6 @@ import { | ||
| 57 | 65 | ListState, |
| 58 | 66 | ago, |
| 59 | 67 | confirmScreen, |
| 60 | drawList, | |
| 61 | hitList, | |
| 62 | 68 | live, |
| 63 | 69 | pickerScreen, |
| 64 | 70 | plural, |
| @@ -161,8 +167,8 @@ export function limbsScreen( | ||
| 161 | 167 | let loaded = false; |
| 162 | 168 | /** What each limb here cost and landed, by issue number; empty unless owner facts. */ |
| 163 | 169 | let spends = new Map<number, NonNullable<Milestone["spend"]>>(); |
| 164 | /** Each limb's pace line, by issue number: on pace for a date, no movement, or done. */ | |
| 165 | let paces = new Map<number, string>(); | |
| 170 | /** Each limb's pace by issue number: issues closed per day, and the sentence (never empty here). */ | |
| 171 | let paces = new Map<number, { days: readonly number[]; line: string }>(); | |
| 166 | 172 | /** The receipt of the last move made here, which `u` reverses once. */ |
| 167 | 173 | let last: string | undefined; |
| 168 | 174 | /** The limbs with the planner, by issue id (#761). */ |
| @@ -206,7 +212,8 @@ export function limbsScreen( | ||
| 206 | 212 | if (f.user?.kind === "user" && isWatching(f.db, f.user.id, m.issue.id)) |
| 207 | 213 | watching.add(m.issue.id); |
| 208 | 214 | if (m.spend !== undefined) spends.set(l.issue.number, m.spend); |
| 209 | if (m.pace.line !== "") paces.set(l.issue.number, m.pace.line); | |
| 215 | if (m.pace.line !== "") | |
| 216 | paces.set(l.issue.number, { days: m.pace.closedPerDay, line: m.pace.line }); | |
| 210 | 217 | } |
| 211 | 218 | loaded = true; |
| 212 | 219 | if (follow !== undefined) { |
| @@ -368,6 +375,48 @@ export function limbsScreen( | ||
| 368 | 375 | meta: `${watching.has(l.issue.id) ? "watching · " : ""}${l.weight.done}/${l.weight.total} done`, |
| 369 | 376 | })); |
| 370 | 377 | |
| 378 | /** Lines a limb takes: its row, and under it the pace line when it has one. */ | |
| 379 | const heightOf = (l: Limb): number => (paces.has(l.issue.number) ? 2 : 1); | |
| 380 | ||
| 381 | /** Draw the limbs, each with its muted pace line beneath; the cursor's row stays in view. */ | |
| 382 | const drawLimbs = (s: Parameters<Screen["render"]>[1], r: Rect, c: Tokens): void => { | |
| 383 | if (r.w <= 0 || r.h <= 0) return; | |
| 384 | const body = r.w - 1; | |
| 385 | state.clamp(limbs.length); | |
| 386 | if (state.index < state.offset) state.offset = state.index; | |
| 387 | const used = (from: number): number => | |
| 388 | limbs.slice(from, state.index + 1).reduce((n, l) => n + heightOf(l), 0); | |
| 389 | while (state.offset < state.index && used(state.offset) > r.h) state.offset++; | |
| 390 | state.height = Math.max(1, Math.floor(r.h / 2)); | |
| 391 | const items = rows(); | |
| 392 | let y = 0; | |
| 393 | let shown = 0; | |
| 394 | for (let i = state.offset; i < limbs.length && y < r.h; i++) { | |
| 395 | row(s, { x: r.x, y: r.y + y, w: body, h: 1 }, c, { | |
| 396 | ...items[i]!, | |
| 397 | selected: i === state.index, | |
| 398 | focused: i === state.index, | |
| 399 | }); | |
| 400 | y++; | |
| 401 | shown++; | |
| 402 | const pace = paces.get(limbs[i]!.issue.number); | |
| 403 | if (pace === undefined || y >= r.h) continue; | |
| 404 | // Narrow terminals lose the sparkline before the sentence; a spare cell keeps it off the scrollbar. | |
| 405 | const spark = trendText(pace.days); | |
| 406 | const text = spark.length + 3 + pace.line.length < body ? ` ${spark} ${pace.line}` : ` ${pace.line}`; | |
| 407 | s.text(r.x, r.y + y, truncate(text, body), style(c.mutedFg, c.bg), r.x + body); | |
| 408 | y++; | |
| 409 | } | |
| 410 | scrollbar(s, { x: r.x + body, y: r.y, w: 1, h: r.h }, c, { total: limbs.length, offset: state.offset }); | |
| 411 | lineAt = []; | |
| 412 | for (let i = state.offset, n = 0; i < state.offset + shown; i++) { | |
| 413 | for (let k = 0; k < heightOf(limbs[i]!); k++) lineAt[n++] = i; | |
| 414 | } | |
| 415 | }; | |
| 416 | ||
| 417 | /** Which limb each drawn line of the list belongs to, from its top. */ | |
| 418 | let lineAt: number[] = []; | |
| 419 | ||
| 371 | 420 | const screen: Screen = { |
| 372 | 421 | title: name, |
| 373 | 422 | get keys() { |
| @@ -391,9 +440,8 @@ export function limbsScreen( | ||
| 391 | 440 | render(area, s) { |
| 392 | 441 | const c = ctx.theme.c; |
| 393 | 442 | const spend = current() === undefined ? undefined : spends.get(current()!.issue.number); |
| 394 | const pace = current() === undefined ? undefined : paces.get(current()!.issue.number); | |
| 395 | 443 | const answered = current() === undefined ? undefined : replanned.get(current()!.issue.number); |
| 396 | const fixed = (pace === undefined ? 0 : 1) + (spend ? 2 : 0); | |
| 444 | const fixed = spend ? 2 : 0; | |
| 397 | 445 | // The foot never takes more than half of what the header leaves. |
| 398 | 446 | const block = |
| 399 | 447 | answered === undefined |
| @@ -412,9 +460,8 @@ export function limbsScreen( | ||
| 412 | 460 | ? "Nothing here waits for anything yet, so there is no milestone to show." |
| 413 | 461 | : `${plural(limbs.length, "limb")} in the order the crew takes ready work${may() ? "" : " · you can read this order, not change it"}`; |
| 414 | 462 | s.text(head!.x, head!.y, truncate(note, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); |
| 415 | drawList(s, list!, c, state, rows()); | |
| 463 | drawLimbs(s, list!, c); | |
| 416 | 464 | const lines = [ |
| 417 | ...(pace === undefined ? [] : [pace]), | |
| 418 | 465 | ...(spend === undefined ? [] : spendLines(spend)), |
| 419 | 466 | ...block.lines, |
| 420 | 467 | ]; |
| @@ -425,7 +472,19 @@ export function limbsScreen( | ||
| 425 | 472 | const cell = on ? style(c.fg, c.bg, 1) : style(c.mutedFg, c.bg); |
| 426 | 473 | s.text(foot!.x, foot!.y + n, truncate(on ? `> ${text}` : text, foot!.w), cell, foot!.x + foot!.w); |
| 427 | 474 | }); |
| 428 | hitList(ctx, list!, state, limbs.length, { open }); | |
| 475 | ctx.hit(list!, { | |
| 476 | click: (e) => { | |
| 477 | const index = lineAt[e.y]; | |
| 478 | if (index === undefined) return; | |
| 479 | if (index === state.index || e.double) { | |
| 480 | state.index = index; | |
| 481 | open(); | |
| 482 | return; | |
| 483 | } | |
| 484 | state.index = index; | |
| 485 | }, | |
| 486 | wheel: (n) => state.move(n, limbs.length), | |
| 487 | }); | |
| 429 | 488 | }, |
| 430 | 489 | key(e) { |
| 431 | 490 | const parts = splitsHere(); |
test/tui-limbs-pace.test.ts added +144
| @@ -0,0 +1,144 @@ | ||
| 1 | /** | |
| 2 | * The limbs screen draws each milestone's pace under its row (#8058): a | |
| 3 | * sparkline of `closedPerDay` and `pace.line`, word for word as the page | |
| 4 | * shows them, and nothing when the line is empty. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { linkIssue } from "../src/actions/dependencies.ts"; | |
| 11 | import { openIssue, setIssueState } from "../src/actions/issues.ts"; | |
| 12 | import { createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { createApp, type App } from "../src/app.ts"; | |
| 14 | import { getIssueById, type Issue } from "../src/core/issues.ts"; | |
| 15 | import { readPlan } from "../src/core/milestones.ts"; | |
| 16 | import { PACE_DAYS } from "../src/core/pace.ts"; | |
| 17 | import { getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 18 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 19 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 20 | import type { TuiIdentity } from "../src/tui/session.ts"; | |
| 21 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 22 | import { quietLogs } from "./helpers/globals.ts"; | |
| 23 | import { settleApp } from "./helpers/tui.ts"; | |
| 24 | ||
| 25 | quietLogs(); | |
| 26 | ||
| 27 | const BLOCKS = /[▁▂▃▄▅▆▇█]/g; | |
| 28 | ||
| 29 | describe("the limbs screen's pace lines", () => { | |
| 30 | let dataDir: string; | |
| 31 | let app: App; | |
| 32 | let alice: User; | |
| 33 | let repo: Repo; | |
| 34 | let moving: Issue; | |
| 35 | let bare: Issue; | |
| 36 | ||
| 37 | const fresh = () => getRepoById(app.db, repo.id)!; | |
| 38 | const open = (title: string): Issue => { | |
| 39 | const made = openIssue(app, alice, fresh(), { title }); | |
| 40 | if (!made.ok) throw new Error(String(made.error)); | |
| 41 | return made.value; | |
| 42 | }; | |
| 43 | const link = (waiter: Issue, on: Issue): void => { | |
| 44 | const now = (i: Issue) => getIssueById(app.db, i.id)!; | |
| 45 | const linked = linkIssue(app, alice, fresh(), now(waiter), now(on)); | |
| 46 | if (!linked.ok) throw new Error(String(linked.error)); | |
| 47 | }; | |
| 48 | ||
| 49 | function boot(cols = 120) { | |
| 50 | const terminal = headlessTerminal(cols, 32); | |
| 51 | const identity: TuiIdentity = { sshUsername: "t", remoteAddress: "127.0.0.1", user: alice }; | |
| 52 | const { app: tui } = createForgeApp(app, identity, { terminal, exitHooks: false }); | |
| 53 | tui.start(); | |
| 54 | const press = async (...keys: string[]) => { | |
| 55 | for (const key of keys) { | |
| 56 | const ctrl = key.startsWith("ctrl+"); | |
| 57 | tui.dispatch({ type: "key", key: ctrl ? key.slice(5) : key, ctrl, alt: false, shift: false }); | |
| 58 | } | |
| 59 | await settleApp(tui); | |
| 60 | }; | |
| 61 | const toMilestones = async () => { | |
| 62 | await settleApp(tui); | |
| 63 | await press("ctrl+k"); | |
| 64 | for (const ch of "Milestones in a repository") await press(ch); | |
| 65 | await press("enter"); | |
| 66 | for (const ch of "alice/pace") await press(ch); | |
| 67 | await press("enter"); | |
| 68 | }; | |
| 69 | return { text: () => tui.screen.toText(), toMilestones, stop: () => tui.stop() }; | |
| 70 | } | |
| 71 | ||
| 72 | beforeAll(async () => { | |
| 73 | dataDir = await mkdtemp(join(tmpdir(), "forge-tui-pace-")); | |
| 74 | app = createApp({ dataDir, port: 0 }); | |
| 75 | const made = await createUser(app.db, { | |
| 76 | origin: "seed", | |
| 77 | trusted: true, | |
| 78 | username: "alice", | |
| 79 | email: "[email protected]", | |
| 80 | password: "a-good-passphrase", | |
| 81 | adminIfFirst: true, | |
| 82 | }); | |
| 83 | if (!made.ok) throw new Error(made.error); | |
| 84 | alice = getUserByName(app.db, "alice")!; | |
| 85 | const r = await createRepo(app, alice, { name: "pace", visibility: "public" }); | |
| 86 | if (!r.ok) throw new Error("repo"); | |
| 87 | repo = r.value; | |
| 88 | moving = open("P0 Moving: closing fast"); | |
| 89 | bare = open("P1 Bare: nothing beneath"); | |
| 90 | const gate = open("Later gate: waits on both"); | |
| 91 | link(gate, moving); | |
| 92 | link(gate, bare); | |
| 93 | // Three leaves closed on three different days, one still open. | |
| 94 | [1, 3, 5].forEach((daysAgo, i) => { | |
| 95 | const leaf = open(`Moving leaf ${i}`); | |
| 96 | link(moving, leaf); | |
| 97 | expect(setIssueState(app, alice, fresh(), getIssueById(app.db, leaf.id)!, "closed").ok).toBe(true); | |
| 98 | const at = new Date(Date.now() - daysAgo * 86_400_000).toISOString(); | |
| 99 | app.db.query("UPDATE issue SET closed_at = ?1 WHERE id = ?2").run(at, leaf.id); | |
| 100 | }); | |
| 101 | link(moving, open("Moving leaf still open")); | |
| 102 | }, 60_000); | |
| 103 | ||
| 104 | afterAll(async () => { | |
| 105 | app.close(); | |
| 106 | await rm(dataDir, { recursive: true, force: true }); | |
| 107 | }); | |
| 108 | ||
| 109 | const plan = () => readPlan(app.db, fresh(), { spend: false }); | |
| 110 | ||
| 111 | test("a milestone with closings shows the page's sentence and a sparkline of PACE_DAYS cells", async () => { | |
| 112 | const pace = plan().milestone(moving.number)!.pace; | |
| 113 | expect(pace.closedPerDay.filter((n) => n > 0)).toHaveLength(3); | |
| 114 | expect(pace.line).not.toBe(""); | |
| 115 | const h = boot(); | |
| 116 | await h.toMilestones(); | |
| 117 | const row = h.text().split("\n").find((l) => l.includes(pace.line))!; | |
| 118 | expect(row).toBeDefined(); | |
| 119 | expect(row.match(BLOCKS)).toHaveLength(PACE_DAYS); | |
| 120 | h.stop(); | |
| 121 | }); | |
| 122 | ||
| 123 | test("a milestone whose pace line is empty gets no line", async () => { | |
| 124 | const pace = plan().milestone(bare.number)?.pace; | |
| 125 | const h = boot(); | |
| 126 | await h.toMilestones(); | |
| 127 | const lines = h.text().split("\n"); | |
| 128 | const at = lines.findIndex((l) => l.includes("Bare")); | |
| 129 | expect(at).toBeGreaterThan(-1); | |
| 130 | if (pace === undefined || pace.line === "") expect(lines[at + 1]).not.toMatch(BLOCKS); | |
| 131 | else expect(lines[at + 1]).toContain(pace.line); | |
| 132 | h.stop(); | |
| 133 | }); | |
| 134 | ||
| 135 | test("a narrow terminal drops the sparkline and keeps the sentence", async () => { | |
| 136 | const pace = plan().milestone(moving.number)!.pace; | |
| 137 | const h = boot(40); | |
| 138 | await h.toMilestones(); | |
| 139 | const text = h.text(); | |
| 140 | expect(text).toContain(pace.line); | |
| 141 | expect(text.match(BLOCKS)).toBeNull(); | |
| 142 | h.stop(); | |
| 143 | }); | |
| 144 | }); |
new above
offline