Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## 0.1.15

Removals. The panel now opens only when asked.

- Remove: `source`, `autoplay` and `remember`. The panel no longer opens on
load, and `/media` requires a path. That path was the only one that could
surprise a user, and the marker replaced it for the agent case.
- Remove: the `close` RPC method. `escape` closes the panel locally and always
did; a script that opened a clip can open another one instead.
- Docs: one "Ways to open" description instead of four drifting copies.

## 0.1.14

- Fix: install docs implied both config files were required. They are not:
Expand Down
49 changes: 18 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ Add options by editing the `cli.json` entry:
}
```

With `source` set and `autoplay` left on, the panel opens by itself once a
session is on screen.

## Example

Make a clip to try it with, so no media of your own is needed:
Expand All @@ -80,25 +77,18 @@ Make a clip to try it with, so no media of your own is needed:
ffmpeg -f lavfi -i "testsrc=size=320x240:rate=24:duration=4" -pix_fmt yuv420p /tmp/demo.mp4
```

Point `cli.json` at it, as in the install example, with `"source": "/tmp/demo.mp4"`,
then start OpenCode and open a session. The panel opens on its own:
Then run `/media /tmp/demo.mp4` in a session:

```text
demo.mp4 · 12 fps · 7/48 · space pause · f fullscreen · esc close
```

48 frames, because the request is 12 frames per second over 4 seconds; the source
48 frames, because frames are extracted at `fps` (12) over 4 seconds; the source
clip's own frame rate does not change that. The line above the frames is the whole
interface: the file name, the frame rate in use, the current frame, and the keys.
`space` pauses, `f` takes the full window, and `esc` closes the panel.

Without a `source`, ask for the file instead:

```text
/media /tmp/demo.mp4
```
interface: file name, frame rate in use, current frame, keys. `space` pauses, `f`
takes the full window, `esc` closes.

The panel belongs to a session, so opening a file from the home screen reports
The panel belongs to a session, so asking for a file from the home screen reports
"Open a session first" and opens nothing.

## Use
Expand Down Expand Up @@ -141,7 +131,8 @@ The body is an envelope: the method's own arguments go under `input`. A body of
service authenticates with HTTP Basic: username `opencode` and the password in
`service.json`, over the channel's port (`49374` for `latest`, `0xc0de`).

Every open, including `/media` and autoplay, travels through this RPC: the TUI
Every open, including `/media` and the message marker, travels through this
RPC: the TUI
part calls it, the server part emits, and the TUI renders the event. The plugin
never shells out.
```
Expand All @@ -156,18 +147,15 @@ exactly like typing the command before the panel exists.

## Options

| Option | Default | Meaning |
| ---------------- | ----------- | ----------------------------------------------- |
| `source` | none | File path or URL opened on start. |
| `autoplay` | `true` | Open the panel on start when a source is known. |
| `remember` | `false` | Reopen the last clip played on start. |
| `fps` | `12` | Frames played per second, 1 to 30. |
| `width` | `44` | Panel width in cells, 4 pixels each. |
| `loop` | `true` | Restart after the last frame. |
| `maxFrames` | `300` | Cap on extracted frames, 1 to 2000. |
| `ffmpeg` | `ffmpeg` | Binary name or absolute path. |
| `cacheDirectory` | system temp | Root directory for extracted frames. |
| `presentation` | `panel` | `panel` or `fullscreen`. |
| Option | Default | Meaning |
| ---------------- | ----------- | ------------------------------------ |
| `fps` | `12` | Frames played per second, 1 to 30. |
| `width` | `44` | Panel width in cells, 4 pixels each. |
| `loop` | `true` | Restart after the last frame. |
| `maxFrames` | `300` | Cap on extracted frames, 1 to 2000. |
| `ffmpeg` | `ffmpeg` | Binary name or absolute path. |
| `cacheDirectory` | system temp | Root directory for extracted frames. |
| `presentation` | `panel` | `panel` or `fullscreen`. |

Frame height follows the source aspect ratio.

Expand All @@ -179,8 +167,8 @@ Frame height follows the source aspect ratio.
3. A timer advances the frame index, and the panel sets its `<image>` source to
the next entry.

Opening is one path. `/media`, autoplay and a message marker all ask the
plugin, the plugin calls its own RPC (`context.client.rpc(Media).open`), the
Opening is one path. `/media`, a message marker and an external RPC call all ask
the plugin, the plugin calls its own RPC (`context.client.rpc(Media).open`), the
server part emits, and the TUI's subscription renders. Nothing shells out, and
if the server part is not loaded the call rejects and the local opener runs
instead.
Expand Down Expand Up @@ -215,7 +203,6 @@ directory under `cacheDirectory` to fetch it again.
| Toast: `Could not run ffmpeg` | ffmpeg is not on the TUI's `PATH`; set `ffmpeg` to a full path. |
| Toast: `Open a session first` | The panel only exists inside a session. |
| Toast: `ffmpeg failed on ...` | The file is not media ffmpeg can decode. |
| No autoplay | No `source` set and `remember` off, or no session is open yet. |
| Panel opens, frame blank | The terminal has no image support. |
| Panel shows one frame, frozen | The plugin was hot-swapped into a running session by a config |
| counter, `escape` ignored | edit; restart the TUI so it mounts fresh. |
Expand Down
4 changes: 0 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ export default Plugin.define({
await registration.events.emit("open", { source });
return { queued: true };
},
close: async () => {
await registration.events.emit("close", {});
return { queued: true };
},
});
},
});
16 changes: 0 additions & 16 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
export type PanelPresentation = "panel" | "fullscreen";

export interface MediaOptions {
/** File path or URL played on start. Without it the panel opens from a command. */
readonly source: string | undefined;
/** Frames played per second. */
readonly fps: number;
/** Panel width in terminal cells. Pixel width is four times this. */
Expand All @@ -18,10 +16,6 @@ export interface MediaOptions {
readonly loop: boolean;
/** Upper bound on extracted frames, so a long clip cannot exhaust memory. */
readonly maxFrames: number;
/** Open the panel on start when a source is known. */
readonly autoplay: boolean;
/** Remember the last clip played and reopen it on start. */
readonly remember: boolean;
/** ffmpeg binary name or absolute path. */
readonly ffmpeg: string;
/** Root directory for extracted frames. Defaults to the system temp directory. */
Expand All @@ -31,13 +25,10 @@ export interface MediaOptions {
}

export const DEFAULT_MEDIA_OPTIONS: MediaOptions = {
source: undefined,
fps: 12,
width: 44,
loop: true,
maxFrames: 300,
autoplay: true,
remember: false,
ffmpeg: "ffmpeg",
cacheDirectory: undefined,
presentation: "panel",
Expand Down Expand Up @@ -70,23 +61,16 @@ const booleanOr = (value: unknown, fallback: boolean): boolean =>

export function normalizeOptions(raw: Readonly<Record<string, unknown>> | undefined): MediaOptions {
const input = raw ?? {};
const source =
typeof input.source === "string" && input.source.trim() !== ""
? input.source.trim()
: undefined;
const cacheDirectory =
typeof input.cacheDirectory === "string" && input.cacheDirectory.trim() !== ""
? input.cacheDirectory.trim()
: undefined;

return {
source,
fps: numberOr(input.fps, DEFAULT_MEDIA_OPTIONS.fps, 1, 30),
width: numberOr(input.width, DEFAULT_MEDIA_OPTIONS.width, 8, 120),
loop: booleanOr(input.loop, DEFAULT_MEDIA_OPTIONS.loop),
maxFrames: numberOr(input.maxFrames, DEFAULT_MEDIA_OPTIONS.maxFrames, 1, 2000),
autoplay: booleanOr(input.autoplay, DEFAULT_MEDIA_OPTIONS.autoplay),
remember: booleanOr(input.remember, DEFAULT_MEDIA_OPTIONS.remember),
ffmpeg: textOr(input.ffmpeg, DEFAULT_MEDIA_OPTIONS.ffmpeg),
cacheDirectory,
presentation: input.presentation === "fullscreen" ? "fullscreen" : "panel",
Expand Down
15 changes: 0 additions & 15 deletions src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ const definition = Rpc.define({
additionalProperties: false,
},
},
close: {
input: {
type: "object",
additionalProperties: false,
},
output: {
type: "object",
properties: { queued: { type: "boolean" } },
required: ["queued"],
additionalProperties: false,
},
},
},
events: {
open: {
Expand All @@ -53,9 +41,6 @@ const definition = Rpc.define({
additionalProperties: false,
},
},
close: {
schema: { type: "object", additionalProperties: false },
},
},
});

Expand Down
47 changes: 3 additions & 44 deletions src/tui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Media } from "./rpc";

/** Opening name for this plugin's panel. Shared selection value, not a registry key. */
const PANEL = "media.panel";
const STORAGE = "media";

// File names and ffmpeg messages reach the renderer as text. Replace control
// characters so neither can inject a terminal escape sequence.
Expand Down Expand Up @@ -91,7 +90,6 @@ export default Plugin.define({
id: PANEL,
setup(context) {
const options = normalizeOptions(context.options);
const [saved, save] = context.storage.store(STORAGE, { initial: { source: "" } });
const [clip, setClip] = createSignal<Clip>();
const [index, setIndex] = createSignal(0);
const [paused, setPaused] = createSignal(false);
Expand Down Expand Up @@ -162,17 +160,6 @@ export default Plugin.define({
return false;
}

if (options.remember) {
void save((draft) => {
draft.source = source;
}).catch(() => {
context.ui.toast.show({
variant: "warning",
title: "Media panel",
message: "Could not save the last clip.",
});
});
}
return true;
}

Expand All @@ -187,9 +174,8 @@ export default Plugin.define({
palette: true,
slash: { name: "media", aliases: ["anim"], arguments: true },
run: (input) => {
const given = input?.trim() ?? "";
const source = given === "" ? options.source : given;
if (source === undefined) {
const source = input?.trim() ?? "";
if (source === "") {
void context.ui.dialog.alert({
title: "Media panel",
message: "Pass a file path or URL, for example: /media /home/user/clip.mp4",
Expand Down Expand Up @@ -233,7 +219,7 @@ export default Plugin.define({
// and this subscriber is what opens it.
// Every open goes through the server: `open` below calls the RPC, the
// server emits, and this subscriber renders. One path for the slash
// command, autoplay and any external caller, and no shell or CLI in it.
// command, the message marker and any external caller, and no shell in it.
// If the server part is not loaded the call rejects and the local opener
// runs instead, so a TUI-only install keeps working.
const remote = context.client.rpc(Media);
Expand All @@ -250,9 +236,6 @@ export default Plugin.define({
}
Promise.resolve(call({ source })).catch(() => show(source));
}
const stopClose = remote.events.on("close", () => {
context.ui.panel.close();
});

// A marker in a finished message opens a clip without any shell command:
// the client already receives session text, so the plugin is the one that
Expand All @@ -266,34 +249,10 @@ export default Plugin.define({
if (match?.[1]) open(match[1]);
});

const remembered = options.remember ? saved.source.trim() : "";
const initial = options.source ?? (remembered === "" ? undefined : remembered);
let autoplay: ReturnType<typeof setInterval> | undefined;

if (options.autoplay && initial !== undefined) {
let attempts = 0;
autoplay = setInterval(() => {
attempts += 1;
if (context.ui.router.current().type === "session") {
if (autoplay) clearInterval(autoplay);
autoplay = undefined;
open(initial);
return;
}
// Give up after two minutes: no session was opened.
if (attempts > 240 && autoplay) {
clearInterval(autoplay);
autoplay = undefined;
}
}, 500);
}

return () => {
stopTicker();
stopOpen();
stopClose();
stopMarker();
if (autoplay) clearInterval(autoplay);
unregister();
};
},
Expand Down
15 changes: 0 additions & 15 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ describe("normalizeOptions", () => {
expect(normalizeOptions(undefined)).toEqual(DEFAULT_MEDIA_OPTIONS);
});

it("keeps a trimmed source and ignores an empty one", () => {
expect(normalizeOptions({ source: " /home/user/clip.mp4 " }).source).toBe(
"/home/user/clip.mp4",
);
expect(normalizeOptions({ source: " " }).source).toBeUndefined();
expect(normalizeOptions({ source: 7 }).source).toBeUndefined();
});

it("clamps numbers into the renderable range", () => {
expect(normalizeOptions({ fps: 0 }).fps).toBe(1);
expect(normalizeOptions({ fps: 999 }).fps).toBe(30);
Expand All @@ -35,13 +27,6 @@ describe("normalizeOptions", () => {
expect(normalizeOptions({ presentation: "window" }).presentation).toBe("panel");
});

it("treats autoplay and remember as opt-out and opt-in respectively", () => {
expect(normalizeOptions({ autoplay: false }).autoplay).toBe(false);
expect(normalizeOptions({ autoplay: "yes" }).autoplay).toBe(true);
expect(normalizeOptions({ remember: true }).remember).toBe(true);
expect(normalizeOptions({}).remember).toBe(false);
});

it("keeps a custom ffmpeg path and cache directory", () => {
const options = normalizeOptions({
ffmpeg: "/opt/homebrew/bin/ffmpeg",
Expand Down