ossclip 0.1.17 → 0.1.19
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/README.md +2 -0
- package/editor-dist/assets/index-B44I-xE4.js +163 -0
- package/editor-dist/assets/index-ChRVBVLj.css +1 -0
- package/editor-dist/index.html +2 -2
- package/package.json +4 -4
- package/src/caption-report.ts +217 -0
- package/src/edit.ts +38 -0
- package/src/interactive/ask-input.ts +191 -0
- package/src/interactive/picker.ts +286 -0
- package/src/interactive/produce-wizard.ts +6 -22
- package/src/interactive/suggest-inputs.ts +164 -0
- package/src/overrides-write.ts +77 -0
- package/src/produce.ts +61 -36
- package/src/program.ts +32 -0
- package/editor-dist/assets/index-C8IPo60X.css +0 -1
- package/editor-dist/assets/index-CdwzWN3j.js +0 -162
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.ossclip-scroll-list{scrollbar-width:thin;scrollbar-color:#3a3a44 transparent}.ossclip-scroll-list::-webkit-scrollbar{width:8px}.ossclip-scroll-list::-webkit-scrollbar-track{background:transparent}.ossclip-scroll-list::-webkit-scrollbar-thumb{background:#3a3a44;border-radius:4px}.ossclip-picker-row{color:#c9c9d4;background:transparent;border:1px solid transparent}.ossclip-picker-row.is-workdir{color:#ededf2;background:#1a1a21;border-color:#2a2a33}.ossclip-picker-row:disabled{cursor:default;opacity:.5}.ossclip-picker-row:hover:not(:disabled){background:#1f1f28;border-color:#3a3a48}.ossclip-picker-row:focus{outline:none;background:#23232e;border-color:#8ab4f8}.ossclip-picker-row:active:not(:disabled){background:#2a2a36}
|
package/editor-dist/index.html
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
6
|
<title>ossclip editor</title>
|
|
7
|
-
<script type="module" crossorigin src="/assets/index-
|
|
8
|
-
<link rel="stylesheet" crossorigin href="/assets/index-
|
|
7
|
+
<script type="module" crossorigin src="/assets/index-B44I-xE4.js"></script>
|
|
8
|
+
<link rel="stylesheet" crossorigin href="/assets/index-ChRVBVLj.css">
|
|
9
9
|
</head>
|
|
10
10
|
<body>
|
|
11
11
|
<div id="root"></div>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ossclip",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.19",
|
|
4
4
|
"description": "Local-first CLI video producer: cuts silence and fillers, word-timed captions, face-aware framing, and LLM-planned code-rendered graphics — transcription and rendering never leave your machine",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -36,9 +36,9 @@
|
|
|
36
36
|
"commander": "^12.1.0",
|
|
37
37
|
"tsx": "^4.19.0",
|
|
38
38
|
"zod": "^3.25.76",
|
|
39
|
-
"@ossclip/core": "0.1.
|
|
40
|
-
"@ossclip/renderer": "0.1.
|
|
41
|
-
"@ossclip/scenes": "0.1.
|
|
39
|
+
"@ossclip/core": "0.1.19",
|
|
40
|
+
"@ossclip/renderer": "0.1.19",
|
|
41
|
+
"@ossclip/scenes": "0.1.19"
|
|
42
42
|
},
|
|
43
43
|
"homepage": "https://github.com/AhsanAyaz/ossclip#readme",
|
|
44
44
|
"bugs": {
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyCaptionEdits,
|
|
3
|
+
captionEditsToKeep,
|
|
4
|
+
isLegacyCaptionKey,
|
|
5
|
+
migrateCaptionKeys,
|
|
6
|
+
MIGRATION_SEARCH_RADIUS,
|
|
7
|
+
} from "@ossclip/core";
|
|
8
|
+
import type {
|
|
9
|
+
AppliedCaptionEdits,
|
|
10
|
+
CaptionEdit,
|
|
11
|
+
CaptionKeyMigration,
|
|
12
|
+
CaptionLine,
|
|
13
|
+
OverrideDoc,
|
|
14
|
+
} from "@ossclip/core";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* What `produce` does with the user's retyped caption words, and what it says
|
|
18
|
+
* about the ones that did not land (§137).
|
|
19
|
+
*
|
|
20
|
+
* Pure, and in its own module rather than inline in `produce.ts`, for the
|
|
21
|
+
* house reason: this is a decision about the user's data (which edits are
|
|
22
|
+
* re-anchored, which are applied, and why the others were not) and a
|
|
23
|
+
* `produce()` run needs ffmpeg, a transcript and a workdir before it reaches
|
|
24
|
+
* any of it. `produce.ts` keeps the `console.log` and the file writes.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** One drop, as a console line. Three cases, and the caller must not merge them. */
|
|
28
|
+
export function captionDropLine(drop: AppliedCaptionEdits["dropped"][number]): string {
|
|
29
|
+
// `found: null` used to be interpolated straight into the sentence, so a
|
|
30
|
+
// word the cut removed reported `the transcript now has "null"` — the one
|
|
31
|
+
// case §137 exists for, described as a JSON literal. The three cases carry
|
|
32
|
+
// genuinely different advice, so they get genuinely different sentences.
|
|
33
|
+
if (drop.reason === "duplicate-anchor") {
|
|
34
|
+
// NOT a stale edit: the edit almost certainly applied, to the FIRST word
|
|
35
|
+
// carrying this anchor. Two words share one source instant by design
|
|
36
|
+
// (captions.ts:44-50 — backfilled seam preimages and cut-clamped words),
|
|
37
|
+
// so this is a note about reach, not a failure.
|
|
38
|
+
return (
|
|
39
|
+
` ⚠ caption edit "${drop.expected}" (${drop.key}): a second word shares that ` +
|
|
40
|
+
`source moment and was left as it is — only the first was retyped`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
if (drop.found === null) {
|
|
44
|
+
// A key that is a POSITION, not a source anchor, never had a moment to
|
|
45
|
+
// lose — it is a pre-§137 doc the migration could not upgrade. Saying "the
|
|
46
|
+
// cut removed it" there sends the user to redo work that is sitting intact
|
|
47
|
+
// on screen (§137 Task 6 review, Important 2). Reachable whenever an edit
|
|
48
|
+
// reaches `applyCaptionEdits` without going through `migrateCaptionKeys`.
|
|
49
|
+
if (isLegacyCaptionKey(drop.key)) {
|
|
50
|
+
return (
|
|
51
|
+
` ⚠ caption edit "${drop.expected}" (position ${drop.key}) not applied: it is keyed ` +
|
|
52
|
+
`by word POSITION, from a project saved before source anchors, and nothing ` +
|
|
53
|
+
`re-anchored it — open the project in the editor, or retype it there`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return (
|
|
57
|
+
` ⚠ caption edit "${drop.expected}" (${drop.key}) dropped: no word starts at that ` +
|
|
58
|
+
`source moment any more — the cut removed the word it was typed over. ` +
|
|
59
|
+
`Retype it in the editor if you still want it.`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
return (
|
|
63
|
+
` ⚠ caption edit "${drop.expected}" (${drop.key}) dropped: the transcript now says ` +
|
|
64
|
+
`"${drop.found}" there`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* How many stored edits actually landed.
|
|
70
|
+
*
|
|
71
|
+
* NOT `keys.length - dropped.length` (§137): `dropped` is not one entry per
|
|
72
|
+
* key. A `duplicate-anchor` entry is pushed for every EXTRA word carrying an
|
|
73
|
+
* anchor, so a single key can appear in `dropped` two or three times — and it
|
|
74
|
+
* may have applied anyway. The old subtraction therefore undercounted, and
|
|
75
|
+
* with enough duplicates went NEGATIVE, which the `> 0` guard then hid
|
|
76
|
+
* entirely: the run printed nothing at all about edits that had applied.
|
|
77
|
+
*
|
|
78
|
+
* The rule comes straight from `applyCaptionEdits`' own contract: a key is
|
|
79
|
+
* marked `seen` by the first word carrying it, and that word either applied
|
|
80
|
+
* the edit or was reported with `reason` ABSENT. So an edit landed exactly
|
|
81
|
+
* when nothing was reported for its key without a `reason`.
|
|
82
|
+
*/
|
|
83
|
+
export function appliedCaptionEditCount(
|
|
84
|
+
edits: Record<string, CaptionEdit>,
|
|
85
|
+
dropped: AppliedCaptionEdits["dropped"],
|
|
86
|
+
): number {
|
|
87
|
+
const failed = new Set(dropped.filter((d) => d.reason === undefined).map((d) => d.key));
|
|
88
|
+
return Object.keys(edits).filter((key) => !failed.has(key)).length;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* One console line for a legacy edit the migration would not place (§137).
|
|
93
|
+
*
|
|
94
|
+
* One sentence per CAUSE, like the editor's own notice: three of the four
|
|
95
|
+
* leave the word sitting right there in the transcript, and a single message
|
|
96
|
+
* blaming the cut would send the user hunting for it.
|
|
97
|
+
*/
|
|
98
|
+
export function captionMigrationLine(u: CaptionKeyMigration["unresolved"][number]): string {
|
|
99
|
+
const head = ` ⚠ caption edit "${u.was}" (${u.key}) could not be re-anchored`;
|
|
100
|
+
switch (u.reason) {
|
|
101
|
+
case "out-of-range":
|
|
102
|
+
// The word is ON SCREEN. Blaming the cut here (which the shared
|
|
103
|
+
// `not-found` sentence did until the final review) sends the user to
|
|
104
|
+
// retype something that is sitting intact in the transcript — and the
|
|
105
|
+
// edit is kept in the doc, so the next run against a different cut may
|
|
106
|
+
// place it without them doing anything at all.
|
|
107
|
+
// No promise that retyping fixes it, either: the word this found may
|
|
108
|
+
// itself be unanchorable (a pre-§137 render-props.json with nothing to
|
|
109
|
+
// backfill from), and "re-anchors it for good" would be a guarantee this
|
|
110
|
+
// line cannot make.
|
|
111
|
+
return `${head}: the word is still here, but more than ${MIGRATION_SEARCH_RADIUS} words from where the edit was stored, so it was left alone rather than applied to the wrong one — it is kept in overrides.json, so retype it in the editor if that is the word you meant`;
|
|
112
|
+
case "ambiguous":
|
|
113
|
+
return `${head}: more than one word says it here, so it was left alone rather than applied to the wrong one — retype the one you meant in the editor`;
|
|
114
|
+
case "unanchorable":
|
|
115
|
+
return `${head}: the word is here but carries no source timing to key on`;
|
|
116
|
+
case "collision":
|
|
117
|
+
return `${head}: two stored edits point at the same word — neither was applied, retype the one you meant in the editor`;
|
|
118
|
+
case "superseded":
|
|
119
|
+
return `${head}: a newer edit already covers that word — the newer one was kept`;
|
|
120
|
+
default:
|
|
121
|
+
return `${head}: no word says it any more — the cut or a re-plan removed it. Retype it in the editor if you still want it.`;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* How many edits came out under a key they did not go in under — the ones the
|
|
127
|
+
* migration actually moved.
|
|
128
|
+
*
|
|
129
|
+
* NOT `Object.keys(migration.edits).length`, which is what the count in the log
|
|
130
|
+
* line was first written as: a MIXED doc (`{"0": …, "w6000": …}` over one word)
|
|
131
|
+
* keeps its already-source-keyed edit and retires the legacy one, so that
|
|
132
|
+
* count announced "1 caption edit re-anchored" about a key nothing had
|
|
133
|
+
* touched. A number the user can check against their own file has to be true
|
|
134
|
+
* for the same reason the drop lines do.
|
|
135
|
+
*/
|
|
136
|
+
export function reanchoredKeyCount(
|
|
137
|
+
before: Record<string, CaptionEdit>,
|
|
138
|
+
migration: CaptionKeyMigration,
|
|
139
|
+
): number {
|
|
140
|
+
return Object.keys(migration.edits).filter((key) => !(key in before)).length;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface CaptionReconciliation {
|
|
144
|
+
/** The doc with its caption keys upgraded — what produce writes back. */
|
|
145
|
+
doc: OverrideDoc;
|
|
146
|
+
/** The caption lines with every edit that could be applied, applied. */
|
|
147
|
+
lines: CaptionLine[];
|
|
148
|
+
/**
|
|
149
|
+
* Whether the migration actually MOVED an edit onto a source anchor — the
|
|
150
|
+
* write-back gate, and the only thing that earns spending the `.bak`.
|
|
151
|
+
*
|
|
152
|
+
* It used to be `captionKeysMigrated`: true whenever anything was
|
|
153
|
+
* unresolved, including when nothing at all was placed. That is a write with
|
|
154
|
+
* no repair in it, and on the field workdir (`cutResult.changed` false,
|
|
155
|
+
* because the cut already carries `src`) it was a NEW write on a run that
|
|
156
|
+
* previously touched nothing — spending `overrides.json.bak`, the user's
|
|
157
|
+
* only surviving pre-cut save and the sole route back to the split half they
|
|
158
|
+
* deleted, on a copy of the already-damaged document (final review, Critical
|
|
159
|
+
* 2). A run that placed nothing has nothing to write and no business
|
|
160
|
+
* touching the `.bak`. Renamed as well as re-defined, because "keys changed"
|
|
161
|
+
* is no longer even true of it: a `superseded` retirement changes the keys
|
|
162
|
+
* and deliberately does not fire this.
|
|
163
|
+
*/
|
|
164
|
+
reanchored: boolean;
|
|
165
|
+
/** Everything produce should print about this, in order. */
|
|
166
|
+
log: string[];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Migrate, then apply, then account for the difference — the whole caption
|
|
171
|
+
* half of a produce run, in one pure pass.
|
|
172
|
+
*
|
|
173
|
+
* MIGRATE FIRST, and against these same lines: `applyCaptionEdits` addresses
|
|
174
|
+
* words by source anchor, so a pre-§137 positional key matches nothing at all
|
|
175
|
+
* and every retype in an old project would be silently absent from the render
|
|
176
|
+
* (§137 Task 6 review, Critical 1). Nothing needs backfilling here — produce
|
|
177
|
+
* builds these lines itself, with a real `srcStart` on every word — which is
|
|
178
|
+
* exactly why this migration belongs in produce even though the same call in
|
|
179
|
+
* the edit server would be inert.
|
|
180
|
+
*
|
|
181
|
+
* WHAT IS APPLIED AND WHAT IS KEPT ARE DIFFERENT SETS, deliberately (final
|
|
182
|
+
* review, Critical 1). Only the edits the migration PLACED are applied — an
|
|
183
|
+
* unresolved key addresses no word, so handing it to `applyCaptionEdits` would
|
|
184
|
+
* buy nothing but a second, worse-worded report of the same edit. The doc
|
|
185
|
+
* written back keeps them anyway (`captionEditsToKeep`), because a run that
|
|
186
|
+
* cannot place an edit today is not a licence to delete it.
|
|
187
|
+
*
|
|
188
|
+
* The caller must hand a doc that has been through `OverrideDocSchema`
|
|
189
|
+
* (`produce.ts` parses it at the top of the run). On raw `JSON.parse` output a
|
|
190
|
+
* literal `"__proto__"` key would be assigned THROUGH rather than kept, and
|
|
191
|
+
* the migration would report nothing lost while losing it.
|
|
192
|
+
*/
|
|
193
|
+
export function reconcileCaptionEdits(
|
|
194
|
+
doc: OverrideDoc,
|
|
195
|
+
baseLines: readonly CaptionLine[],
|
|
196
|
+
): CaptionReconciliation {
|
|
197
|
+
const log: string[] = [];
|
|
198
|
+
const migration = migrateCaptionKeys(doc.captions, baseLines);
|
|
199
|
+
// The MOVED count is both the log gate and the write gate. As a log gate:
|
|
200
|
+
// announcing "0 caption edit(s) re-anchored" above the lines saying why is
|
|
201
|
+
// noise on the one run where the user is reading carefully. As a write gate:
|
|
202
|
+
// see `CaptionReconciliation.reanchored` — a run that placed nothing must
|
|
203
|
+
// not spend the `.bak`.
|
|
204
|
+
const reanchored = reanchoredKeyCount(doc.captions, migration);
|
|
205
|
+
if (reanchored > 0) {
|
|
206
|
+
log.push(
|
|
207
|
+
`▸ ${reanchored} caption edit(s) re-anchored from word positions to source time (§137)`,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
for (const u of migration.unresolved) log.push(captionMigrationLine(u));
|
|
211
|
+
const migrated = { ...doc, captions: captionEditsToKeep(doc.captions, migration) };
|
|
212
|
+
const { lines, dropped } = applyCaptionEdits(baseLines, migration.edits);
|
|
213
|
+
const live = appliedCaptionEditCount(migration.edits, dropped);
|
|
214
|
+
if (live > 0) log.push(`▸ ${live} caption word(s) retyped by the editor`);
|
|
215
|
+
for (const d of dropped) log.push(captionDropLine(d));
|
|
216
|
+
return { doc: migrated, lines, reanchored: reanchored > 0, log };
|
|
217
|
+
}
|
package/src/edit.ts
CHANGED
|
@@ -259,9 +259,29 @@ export async function startEditServer(
|
|
|
259
259
|
return send(200, { noWorkdir: true, recent: await readRecentProjects(opts.recentDir) });
|
|
260
260
|
}
|
|
261
261
|
const renderProps = JSON.parse(await readFile(propsPath(), "utf8"));
|
|
262
|
+
// Parsed, never cast — and that parse is also what makes the doc
|
|
263
|
+
// safe to migrate downstream: a literal `"__proto__"` caption key
|
|
264
|
+
// survives `JSON.parse` as an own property, and any later pass that
|
|
265
|
+
// rebuilds the record would assign through it instead of keeping it.
|
|
262
266
|
const overrides = existsSync(overridesPath())
|
|
263
267
|
? OverrideDocSchema.parse(JSON.parse(await readFile(overridesPath(), "utf8")))
|
|
264
268
|
: emptyOverrideDoc();
|
|
269
|
+
// §137 DECISION (Task 6): the pre-§137 caption-key migration does
|
|
270
|
+
// NOT run here. It resolves a positional key by finding the word it
|
|
271
|
+
// named and taking that word's source anchor — and these render
|
|
272
|
+
// props are served exactly as they sit on disk, where a pre-§137
|
|
273
|
+
// file's caption words have no `srcStart` at all. Every word would
|
|
274
|
+
// answer "no anchor", every edit would land in `unresolved`, and the
|
|
275
|
+
// migration would report total loss while doing nothing: a call that
|
|
276
|
+
// passes its own tests and is inert in production.
|
|
277
|
+
// Anchoring them here instead would mean a second copy of the "no
|
|
278
|
+
// usable map, no repair" rule (`anchorCaptionLines`, apps/editor) in
|
|
279
|
+
// this package — the CLI cannot import the editor's source, which it
|
|
280
|
+
// only ever ships as a built `editor-dist/` — and that rule is
|
|
281
|
+
// exactly the one §137 refuses to have two of. So the EDITOR owns
|
|
282
|
+
// the repair, at the one point that holds anchored lines and the doc
|
|
283
|
+
// at the same time (App.tsx's load path), and it loses no reach:
|
|
284
|
+
// this endpoint has exactly one consumer.
|
|
265
285
|
return send(200, {
|
|
266
286
|
renderProps,
|
|
267
287
|
overrides,
|
|
@@ -450,6 +470,24 @@ export async function startEditServer(
|
|
|
450
470
|
for await (const c of req) chunks.push(c as Buffer);
|
|
451
471
|
const parsed = OverrideDocSchema.safeParse(JSON.parse(Buffer.concat(chunks).toString()));
|
|
452
472
|
if (!parsed.success) return send(400, { error: parsed.error.message });
|
|
473
|
+
// NO `.bak` HERE, and that is a decision, not an omission (final
|
|
474
|
+
// review, Important 5). This write is safe without one only because
|
|
475
|
+
// it ROUND-TRIPS: whatever the editor loaded, it saves back, plus
|
|
476
|
+
// the change the user just made. §137 briefly broke that property —
|
|
477
|
+
// `migrateLoadedDoc` stripped the caption edits the migration could
|
|
478
|
+
// not place before `edits.load` (which also clears undo), so the
|
|
479
|
+
// first save after opening a legacy project deleted them
|
|
480
|
+
// permanently. The fix is in `migrateLoadedDoc`, which now keeps
|
|
481
|
+
// them, rather than here.
|
|
482
|
+
//
|
|
483
|
+
// Adding produce's `.bak` to this handler was the other option and
|
|
484
|
+
// is actively worse: `overrides.json.bak` is single-generation and
|
|
485
|
+
// SHARED with produce's write, so a routine ⌘S would spend the one
|
|
486
|
+
// the user's pre-cut save is sitting in — which on the §137 field
|
|
487
|
+
// workdir is the only artefact their deleted split half can ever be
|
|
488
|
+
// recovered from (`legacySplitId`). That is the review's own
|
|
489
|
+
// Critical 2 reintroduced through the editor.
|
|
490
|
+
//
|
|
453
491
|
// Atomic: the producer may read this file at any moment, and a
|
|
454
492
|
// half-written document would be worse than a stale one.
|
|
455
493
|
const tmp = `${overridesPath()}.tmp`;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { existsSync, statSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { livePickerDeps, pickPath, pickerAvailable, type PickMode } from "./picker";
|
|
4
|
+
import { assertInteractive, select, text, unwrap } from "./prompts";
|
|
5
|
+
import { rankSuggestions, scanLikelyDirs, type Suggestion } from "./suggest-inputs";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* "Which video?" — the first prompt of the wizard, and until §136 the one
|
|
9
|
+
* step a non-technical user could not get past: it demanded a typed path.
|
|
10
|
+
* Now it is a menu of the newest videos on the machine, a native picker, and
|
|
11
|
+
* typing, in that order.
|
|
12
|
+
*
|
|
13
|
+
* Every branch converges on `validateInputPath` rather than trusting its
|
|
14
|
+
* source. A picker result is not more trustworthy than typed text: the file
|
|
15
|
+
* can be deleted between the dialog closing and the path arriving, and
|
|
16
|
+
* "Browse for a folder" happily returns a folder with no video in it.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export const BROWSE_FILE = "__browse_file__";
|
|
20
|
+
export const BROWSE_FOLDER = "__browse_folder__";
|
|
21
|
+
export const TYPE_PATH = "__type_path__";
|
|
22
|
+
|
|
23
|
+
export type InputSource = "suggestion" | "picker" | "typed";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Not a zod schema, deliberately: the house rule parses user values with zod
|
|
27
|
+
* because a typo must not silently coerce, and there is nothing to coerce
|
|
28
|
+
* here — this is a filesystem predicate (does it exist, is it a file or a
|
|
29
|
+
* folder), which zod cannot answer. The wording is carried over verbatim
|
|
30
|
+
* from the prompt this replaced.
|
|
31
|
+
*
|
|
32
|
+
* There is deliberately no extension whitelist, on any branch: typing a path
|
|
33
|
+
* was never extension-checked either, and ossclip accepts whatever ffmpeg can
|
|
34
|
+
* read — a list would reject legitimate containers.
|
|
35
|
+
*/
|
|
36
|
+
export function validateInputPath(v: string | undefined): string | undefined {
|
|
37
|
+
if (!v) return "a path is required";
|
|
38
|
+
if (!existsSync(v)) return `no such path: ${v}`;
|
|
39
|
+
const st = statSync(v);
|
|
40
|
+
if (!st.isFile() && !st.isDirectory()) return `${v} is neither a video file nor a folder`;
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function inputChoices(
|
|
45
|
+
suggestions: Suggestion[],
|
|
46
|
+
canBrowse: boolean,
|
|
47
|
+
): { value: string; label: string; hint?: string }[] {
|
|
48
|
+
const rows = suggestions.map((s) => ({ value: s.path, label: s.label, hint: s.hint }));
|
|
49
|
+
// Offering a "Browse…" row on a machine with no dialog to open is worse
|
|
50
|
+
// than not offering it — `pickerAvailable` has already probed, so the row
|
|
51
|
+
// simply does not exist when it cannot work.
|
|
52
|
+
if (canBrowse) {
|
|
53
|
+
rows.push(
|
|
54
|
+
{ value: BROWSE_FILE, label: "Browse…", hint: "opens a file picker" },
|
|
55
|
+
{ value: BROWSE_FOLDER, label: "Browse for a folder of clips", hint: "concatenated by name" },
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
rows.push({ value: TYPE_PATH, label: "Type a path", hint: "if you already know it" });
|
|
59
|
+
return rows;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let lastInputSource: InputSource | "argv" = "argv";
|
|
63
|
+
|
|
64
|
+
/** Which branch produced the input, for the produce_completed event (§136). */
|
|
65
|
+
export function noteInputSource(s: InputSource): void {
|
|
66
|
+
lastInputSource = s;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function inputSourceUsed(): InputSource | "argv" {
|
|
70
|
+
return lastInputSource;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Module state outlives a single wizard, and "argv" is a real answer — the
|
|
75
|
+
* value the telemetry reads when `ossclip <path>` prefilled the input and
|
|
76
|
+
* `askInput` never ran. So a second run in the same process (a batch or REPL
|
|
77
|
+
* caller, and every test file, where module state persists across `it`s)
|
|
78
|
+
* would report the PREVIOUS run's branch with nothing to signal the staleness.
|
|
79
|
+
* Reset at the start of a run — or of a test — rather than trusting one
|
|
80
|
+
* invocation per process.
|
|
81
|
+
*/
|
|
82
|
+
export function resetInputSource(): void {
|
|
83
|
+
lastInputSource = "argv";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The seam. Same shape and same reason as `PickerDeps` (picker.ts) and
|
|
88
|
+
* `TtyDeps` (tty.ts): the branch logic is what has rules worth pinning — a
|
|
89
|
+
* cancelled dialog re-asks, every branch validates, each sets its own source —
|
|
90
|
+
* and none of that is reachable through a real `select` blocking on a human.
|
|
91
|
+
* `pickPath` is tested the same way through its own injected deps.
|
|
92
|
+
*
|
|
93
|
+
* Structural signatures rather than `typeof select`: clack's are generic, and
|
|
94
|
+
* a fake cannot satisfy an unresolved type parameter. Narrowing here is what
|
|
95
|
+
* makes the fakes writable, exactly as `PickerDeps` declares `hasBin` rather
|
|
96
|
+
* than `typeof binOnPath`.
|
|
97
|
+
*/
|
|
98
|
+
export interface AskInputDeps {
|
|
99
|
+
/** Probed once per run, not per loop — no dialog appears mid-prompt. */
|
|
100
|
+
canBrowse: boolean;
|
|
101
|
+
suggest: () => Promise<Suggestion[]>;
|
|
102
|
+
pick: (mode: PickMode) => Promise<string | undefined>;
|
|
103
|
+
select: (opts: {
|
|
104
|
+
message: string;
|
|
105
|
+
options: { value: string; label: string; hint?: string }[];
|
|
106
|
+
}) => Promise<string | symbol>;
|
|
107
|
+
text: (opts: {
|
|
108
|
+
message: string;
|
|
109
|
+
placeholder?: string;
|
|
110
|
+
validate?: (v: string | undefined) => string | undefined;
|
|
111
|
+
}) => Promise<string | symbol>;
|
|
112
|
+
/** Injected because the guard itself takes an injected check (tty.ts:58). */
|
|
113
|
+
assertInteractive: () => void;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export const liveAskInputDeps = (): AskInputDeps => ({
|
|
117
|
+
canBrowse: pickerAvailable(livePickerDeps()),
|
|
118
|
+
suggest: async () => rankSuggestions(await scanLikelyDirs(), Date.now(), homedir()),
|
|
119
|
+
pick: (mode) => pickPath(mode),
|
|
120
|
+
select,
|
|
121
|
+
text,
|
|
122
|
+
assertInteractive: () => assertInteractive("input prompt"),
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const typePath = async (deps: AskInputDeps): Promise<string> =>
|
|
126
|
+
unwrap(
|
|
127
|
+
await deps.text({
|
|
128
|
+
// Finding 1 (final-review fix wave): `ossclip produce <folder>` shipped
|
|
129
|
+
// (folder-input-brief.md) but this prompt still rejected a directory —
|
|
130
|
+
// the wizard was the only way in that couldn't do what the CLI could.
|
|
131
|
+
// A folder is concatenated by name (codepoint order, like `ls`); --sort
|
|
132
|
+
// mtime reorders it but stays a typed flag, not a wizard question (see
|
|
133
|
+
// the file-level comment in produce-wizard.ts for why).
|
|
134
|
+
message: "Video file, or a folder of clips to concatenate (by name; --sort mtime is a typed flag)",
|
|
135
|
+
placeholder: "./raw/take1.mp4",
|
|
136
|
+
validate: validateInputPath,
|
|
137
|
+
}),
|
|
138
|
+
) as string;
|
|
139
|
+
|
|
140
|
+
export async function askInput(deps: AskInputDeps = liveAskInputDeps()): Promise<string> {
|
|
141
|
+
deps.assertInteractive();
|
|
142
|
+
const canBrowse = deps.canBrowse;
|
|
143
|
+
const suggestions = await deps.suggest();
|
|
144
|
+
|
|
145
|
+
// Nothing found and nowhere to browse: a one-row menu is pure noise, so go
|
|
146
|
+
// straight to the prompt this whole unit replaced.
|
|
147
|
+
if (suggestions.length === 0 && !canBrowse) {
|
|
148
|
+
noteInputSource("typed");
|
|
149
|
+
return typePath(deps);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Loops rather than returns, because cancelling the OS dialog must land
|
|
153
|
+
// back on the menu — Escape in a Finder window means "not that one", not
|
|
154
|
+
// "abandon the run". Ctrl-C at the menu still exits via `unwrap`.
|
|
155
|
+
for (;;) {
|
|
156
|
+
const choice = unwrap(
|
|
157
|
+
await deps.select({ message: "Which video?", options: inputChoices(suggestions, canBrowse) }),
|
|
158
|
+
) as string;
|
|
159
|
+
|
|
160
|
+
if (choice === TYPE_PATH) {
|
|
161
|
+
noteInputSource("typed");
|
|
162
|
+
return typePath(deps);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (choice === BROWSE_FILE || choice === BROWSE_FOLDER) {
|
|
166
|
+
const picked = await deps.pick(choice === BROWSE_FOLDER ? "folder" : "file");
|
|
167
|
+
// An empty result conflates "cancelled" with "the backend failed
|
|
168
|
+
// silently" — no backend distinguishes them reliably (picker.ts), and
|
|
169
|
+
// `pickPath` has already printed the fallback notice in the failure
|
|
170
|
+
// case. Re-asking is the right next step either way.
|
|
171
|
+
if (picked === undefined) continue;
|
|
172
|
+
const problem = validateInputPath(picked);
|
|
173
|
+
if (problem !== undefined) {
|
|
174
|
+
console.log(`▸ ${problem}`);
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
noteInputSource("picker");
|
|
178
|
+
return picked;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// A suggestion. Still validated: the listing is a snapshot, and a file
|
|
182
|
+
// can be moved between the scan and the keypress.
|
|
183
|
+
const problem = validateInputPath(choice);
|
|
184
|
+
if (problem !== undefined) {
|
|
185
|
+
console.log(`▸ ${problem}`);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
noteInputSource("suggestion");
|
|
189
|
+
return choice;
|
|
190
|
+
}
|
|
191
|
+
}
|