// `umtool tighten [--json] [--no-measure] [--all] [--apply [,…]]`: // the Cut tab's "Propose tight windows" (lib/report/tightPropose.mjs) from a // terminal -- every clip of the cut tightened to its sentence and each edge // into its pause, before and after. Prints only; `--apply` writes the named // rows through the window writer (the token, the cite that follows the edge, // an edit note on a generated manifest), re-proposed first. // // Plain ESM: the CLI runs it under bare node. import { REPORTS_ROOT, resolveProject } from "../projects/core.mjs"; import { kindMakesTakes } from "../projects/kinds.mjs"; import { applyTightWindows, proposeTightWindows } from "./tightPropose.mjs"; export const TIGHTEN_USAGE = [ "usage: umtool tighten [--json] [--all] [--no-measure]", " umtool tighten --apply [,…]", "", "Each clip of the cut to its sentence -- the sentences its cut (its quote) touches, else the whole", "sentences inside its window -- and each edge into its pause (the build's measured silences and the", "gaps between the recording's timed words, at most 1 s away, inside the cached window). Prints the", "clips that would change, before -> after with the seconds (--all: every clip). --no-measure skips", "the silences (the words alone). --apply writes those rows through the window writer; a locked edge", "is proposed like any other (a lock is against resolve-windows) and the lock is kept.", ].join("\n"); class CliError extends Error {} const hms = (t) => { const m = Math.floor(t / 60); return `${m}:${(t - m * 60).toFixed(2).padStart(5, "0")}`; }; /** One row as a line. */ export function tightLine(r) { const b = `${hms(r.before.start)}–${hms(r.before.end)} (${r.before.seconds.toFixed(2)} s)`; if (!r.after) return `${r.id.padEnd(8)} ${b} — ${r.why}`; const a = `${hms(r.after.start)}–${hms(r.after.end)} (${r.after.seconds.toFixed(2)} s)`; const d = r.after.seconds - r.before.seconds; const marks = [r.locked ? "locked" : null, r.fetched ? null : "not fetched", r.clear.cut ? "clears the cut" : null].filter(Boolean); return `${r.id.padEnd(8)} ${b} → ${a} ${r.changed ? `${d > 0 ? "+" : "−"}${Math.abs(d).toFixed(2)} s` : "already tight"}${marks.length ? ` [${marks.join(", ")}]` : ""}` + (r.changed ? `\n ${r.how}${r.said.length ? ` — ${r.said.join("; ")}` : ""}` : ""); } /** * @param {string[]} args the words after `tighten` * @param {{ root?: string, out?: (s: string) => void, err?: (s: string) => void }} [opts] * @returns {Promise} */ export async function tightenCommand(args, { root = REPORTS_ROOT, out = (s) => console.log(s), err = (s) => console.error(s) } = {}) { try { const flags = new Set(); const words = []; let apply = null; for (let i = 0; i < args.length; i += 1) { const a = args[i]; if (a === "--apply") { apply = String(args[i + 1] ?? "").split(",").map((s) => s.trim()).filter(Boolean); i += 1; if (!apply.length) throw new CliError("--apply names clips: --apply c01,c02"); } else if (a.startsWith("--")) { if (!["--json", "--all", "--no-measure", "--help"].includes(a)) throw new CliError(`unknown flag ${a}`); flags.add(a); } else words.push(a); } if (!words[0] || flags.has("--help")) { (flags.has("--help") ? out : err)(TIGHTEN_USAGE); return flags.has("--help") ? 0 : 2; } const r = await resolveProject(words[0], root); if (r.ambiguous) throw new CliError(`"${words[0]}" is the name of ${r.ambiguous.length} projects — pass its id`); if (!r.project) throw new CliError(`no project matches "${words[0]}"`); const project = r.project; if (!kindMakesTakes(project.kind)) throw new CliError(`${project.id} is a ${project.kind}, not a report video`); if (apply) { const done = await applyTightWindows(project, apply); if (flags.has("--json")) out(JSON.stringify({ applied: done }, null, 2)); else for (const d of done) out(`${d.ok ? "applied" : "NOT applied"} ${d.id}: ${d.said}`); return done.every((d) => d.ok) ? 0 : 1; } const got = await proposeTightWindows(project, { measure: !flags.has("--no-measure") }); if (flags.has("--json")) { out(JSON.stringify(got, null, 2)); return 0; } out(`${got.counts.proposed} of ${got.counts.clips} clips would change · ${got.counts.same} already tight${got.counts.none ? ` · ${got.counts.none} with nothing to go by` : ""}`); for (const row of got.rows) if (flags.has("--all") || row.changed) out(tightLine(row)); if (got.counts.proposed) out(`\napply: umtool tighten ${project.id} --apply ${got.rows.filter((x) => x.changed).map((x) => x.id).join(",")}`); return 0; } catch (e) { err(e instanceof Error ? e.message : String(e)); return e instanceof CliError ? 2 : 1; } }