Official
terminal
Built-in integrated terminal (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/terminal@0.2.3
Permissions in 0.2.3
Take care. This plugin asks for permissions that can do anything your account can. The app asks you to hold Enable for two seconds or to type the plugin's name before it turns on.
Files
state.ts4.9 KB
// Workspace-scoped terminal selection is independent of the views and kernel effects.
// The state layer gives each activation its own session cache and creation guards.
import * as Context from "effect/Context";
import * as Layer from "effect/Layer";
import * as z from "zod";
import type { EffectClient } from "divergence/effect";
import type * as Fiber from "effect/Fiber";
import type { Terminal, ViewHandle, Workspace } from "divergence";
import type { Tabs } from "../tabs/contract.ts";
export interface State {
terminals: readonly Terminal[];
activeByWorkspace: Record<string, string | null>;
paths: Record<string, string>;
menu: string | null;
renaming: string | null;
creating: Set<string>;
// Why the last shell a workspace asked for could not be made; the fly-out shows it with Try again in place of
// starting another by itself.
failed: Record<string, string>;
// The terminals in the order the user dragged them to; empty until then.
order: string[];
ready: boolean;
flyOut?: ViewHandle;
rail?: ViewHandle;
// The divergence/tabs client while its provider runs, the tab it last
// announced, and the fiber following it.
tabs: EffectClient<typeof Tabs> | undefined;
activeTab: { kind: string; path: unknown } | null;
follow: Fiber.Fiber<void> | undefined;
}
export interface State {
terminals: readonly Terminal[];
activeByWorkspace: Record<string, string | null>;
paths: Record<string, string>;
menu: string | null;
renaming: string | null;
creating: Set<string>;
ready: boolean;
flyOut?: ViewHandle;
rail?: ViewHandle;
}
export class TerminalState extends Context.Service<TerminalState, State>()("terminal/State") {
static readonly layer = Layer.sync(TerminalState, () => ({
terminals: [],
activeByWorkspace: {},
paths: {},
menu: null,
renaming: null,
creating: new Set<string>(),
failed: {},
order: [],
ready: false,
tabs: undefined,
activeTab: null,
follow: undefined,
}));
}
export function forWorkspace(terminals: readonly Terminal[], workspaceId: string | null | undefined): Terminal[] {
return terminals.filter((terminal) => terminal.workspaceId === workspaceId);
}
export function activeTerminal(
state: Pick<State, "terminals" | "activeByWorkspace">,
workspaceId: string | undefined,
): Terminal | null {
const mine = forWorkspace(state.terminals, workspaceId);
return (
mine.find((terminal) => terminal.id === state.activeByWorkspace[String(workspaceId)]) ??
mine[mine.length - 1] ??
null
);
}
// The kernel's terminals in the user's order; ones the user has not placed
// keep the kernel's order after the rest.
export function arrange(terminals: readonly Terminal[], order: readonly string[]): Terminal[] {
const rank = new Map(order.map((id, index) => [id, index]));
return [...terminals].sort((a, b) => (rank.get(a.id) ?? order.length) - (rank.get(b.id) ?? order.length));
}
// A terminal dropped on another lands beside it, on the side the kernel
// reports. A kernel without `reorder` reports `into`: it takes the other's
// place.
export function moveTerminal(terminals: readonly Terminal[], id: string, target: string, zone: string): Terminal[] {
const from = terminals.findIndex((terminal) => terminal.id === id);
const to = terminals.findIndex((terminal) => terminal.id === target);
if (from < 0 || to < 0 || from === to) return [...terminals];
const rest = terminals.filter((terminal) => terminal.id !== id);
const at = rest.findIndex((terminal) => terminal.id === target);
const after = zone === "after" || (zone !== "before" && from < to);
rest.splice(after ? at + 1 : at, 0, terminals[from]!);
return rest;
}
export function nextTerminal(terminals: readonly Terminal[], terminal: Terminal): Terminal | null {
const mine = forWorkspace(terminals, terminal.workspaceId);
const index = mine.findIndex((session) => session.id === terminal.id);
return mine[index + 1] ?? mine[index - 1] ?? null;
}
/** The workspace's choice once `terminal` closes: unchanged when it was not the one in use, else its next neighbour,
* else its previous one. */
export function choiceAfterClose(
state: Pick<State, "terminals" | "activeByWorkspace">,
terminal: Terminal,
): string | null {
const workspaceId = String(terminal.workspaceId);
if (activeTerminal(state, workspaceId)?.id !== terminal.id) return state.activeByWorkspace[workspaceId] ?? null;
return nextTerminal(state.terminals, terminal)?.id ?? null;
}
// The detail pane owns the payload; this plugin only compares its identity and tests its truthiness.
const tab = z.object({ kind: z.string(), path: z.unknown().optional() });
export function terminalInPane(active: unknown): unknown {
const parsed = tab.safeParse(active);
return parsed.success && parsed.data.kind === "terminal" ? parsed.data.path : null;
}
export function pathsByWorkspace(workspaces: readonly Workspace[]): Record<string, string> {
return Object.fromEntries(workspaces.map((workspace) => [workspace.id, workspace.path]));
}
No comments yet.