Skip to content

Commit 8de1510

Browse files
Merge pull request anomalyco#462 from different-ai/feat/openwrk-tui-clickable
feat(openwrk): clickable TUI tabs + auto-copy selection
2 parents a6c0714 + 0daddf0 commit 8de1510

2 files changed

Lines changed: 112 additions & 9 deletions

File tree

‎packages/headless/src/cli.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3154,6 +3154,7 @@ async function runStart(args: ParsedArgs) {
31543154
const result = await copyToClipboard(attachCommand);
31553155
return { command: attachCommand, ...result };
31563156
},
3157+
onCopySelection: async (text) => copyToClipboard(text),
31573158
onOwpenbotHealth: async () => fetchOwpenbotHealth(owpenbotHealthUrl),
31583159
onOwpenbotQr: async () => {
31593160
if (!owpenbotBinary) {

‎packages/headless/src/tui/app.tsx‎

Lines changed: 111 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { RGBA, TextAttributes, type InputRenderable, type KeyEvent } from "@opentui/core";
2-
import { render, useKeyboard, useRenderer, useTerminalDimensions } from "@opentui/solid";
3-
import { For, Show, createMemo, createSignal, onCleanup } from "solid-js";
1+
import { RGBA, TextAttributes, type InputRenderable, type KeyEvent, type TabSelectRenderable } from "@opentui/core";
2+
import { render, useKeyboard, useRenderer, useSelectionHandler, useTerminalDimensions } from "@opentui/solid";
3+
import { For, Show, createEffect, createMemo, createSignal, onCleanup } from "solid-js";
44
import { createStore } from "solid-js/store";
55

66
export type TuiLogLevel = "debug" | "info" | "warn" | "error";
@@ -67,6 +67,7 @@ type TuiOptions = {
6767
onQuit: () => void | Promise<void>;
6868
onDetach: () => void | Promise<void>;
6969
onCopyAttach: () => Promise<{ command: string; copied: boolean; error?: string }>;
70+
onCopySelection?: (text: string) => Promise<{ copied: boolean; error?: string }>;
7071
onOwpenbotHealth: () => Promise<TuiOwpenbotHealth>;
7172
onOwpenbotQr: () => Promise<string>;
7273
onOwpenbotSetTelegramToken: (token: string) => Promise<{ ok: boolean; error?: string }>;
@@ -116,6 +117,15 @@ const levelCycle: Array<"all" | TuiLogLevel> = ["all", "info", "warn", "error",
116117

117118
const serviceCycle = ["all", "openwrk", "opencode", "openwork-server", "owpenbot"];
118119

120+
const viewTabs: Array<{ name: string; description: string; value: ViewName }> = [
121+
{ name: "Overview", description: "Overview", value: "overview" },
122+
{ name: "Logs", description: "Logs", value: "logs" },
123+
{ name: "Owpenbot", description: "Owpenbot", value: "owpenbot" },
124+
{ name: "Help", description: "Help", value: "help" },
125+
];
126+
127+
const viewIndexByName = new Map(viewTabs.map((entry, index) => [entry.value, index]));
128+
119129
function formatDuration(ms: number) {
120130
if (ms < 1000) return `${ms}ms`;
121131
if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`;
@@ -215,6 +225,67 @@ export function startOpenwrkTui(options: TuiOptions): TuiHandle {
215225
if (toastTimer) clearTimeout(toastTimer);
216226
});
217227

228+
const setView = (view: ViewName) => {
229+
setState("view", view);
230+
if (view !== "owpenbot") {
231+
setState("owpenbotEditing", false);
232+
}
233+
};
234+
235+
let tabSelect: TabSelectRenderable | undefined;
236+
const tabWidth = createMemo(() => {
237+
const width = Math.max(0, dimensions().width - 4);
238+
return Math.max(12, Math.floor(width / viewTabs.length));
239+
});
240+
createEffect(() => {
241+
if (!tabSelect) return;
242+
const index = viewIndexByName.get(state.view) ?? 0;
243+
if (tabSelect.getSelectedIndex() !== index) {
244+
tabSelect.setSelectedIndex(index);
245+
}
246+
});
247+
248+
const copySelection = async (text: string) => {
249+
let copied = false;
250+
let error: string | undefined;
251+
if (options.onCopySelection) {
252+
try {
253+
const result = await options.onCopySelection(text);
254+
copied = result.copied;
255+
error = result.error;
256+
} catch (err) {
257+
error = err instanceof Error ? err.message : String(err);
258+
}
259+
}
260+
if (!copied && renderer.isOsc52Supported()) {
261+
copied = renderer.copyToClipboardOSC52(text);
262+
}
263+
if (!copied) {
264+
if (error) {
265+
showToast(`Selection copy failed: ${error}`);
266+
}
267+
return;
268+
}
269+
showToast("Selection copied");
270+
};
271+
272+
let lastCopiedSelection = "";
273+
useSelectionHandler((selection) => {
274+
if (!selection || !selection.isActive) {
275+
lastCopiedSelection = "";
276+
return;
277+
}
278+
const text = selection.getSelectedText();
279+
if (!text) {
280+
lastCopiedSelection = "";
281+
return;
282+
}
283+
if (selection.isDragging) return;
284+
if (text === lastCopiedSelection) return;
285+
lastCopiedSelection = text;
286+
void copySelection(text);
287+
});
288+
218289
let tokenInput: InputRenderable | undefined;
219290

220291
const logHeight = createMemo(() => {
@@ -294,23 +365,22 @@ export function startOpenwrkTui(options: TuiOptions): TuiHandle {
294365
}
295366
if (evt.name === "l") {
296367
evt.preventDefault();
297-
setState("view", "logs");
368+
setView("logs");
298369
return;
299370
}
300371
if (evt.name === "w") {
301372
evt.preventDefault();
302-
setState("view", "owpenbot");
373+
setView("owpenbot");
303374
return;
304375
}
305376
if (evt.name === "h" || evt.name === "?") {
306377
evt.preventDefault();
307-
setState("view", "help");
378+
setView("help");
308379
return;
309380
}
310381
if (evt.name === "b" || evt.name === "o") {
311382
evt.preventDefault();
312-
setState("view", "overview");
313-
setState("owpenbotEditing", false);
383+
setView("overview");
314384
return;
315385
}
316386
if (evt.name === "c") {
@@ -331,7 +401,7 @@ export function startOpenwrkTui(options: TuiOptions): TuiHandle {
331401
if (state.view === "owpenbot") {
332402
if (evt.name === "b" || evt.name === "o") {
333403
evt.preventDefault();
334-
setState("view", "overview");
404+
setView("overview");
335405
return;
336406
}
337407
if (evt.name === "g") {
@@ -426,6 +496,34 @@ export function startOpenwrkTui(options: TuiOptions): TuiHandle {
426496
</box>
427497
<text fg={theme.textMuted}>run id: {state.connect.runId}</text>
428498

499+
<box paddingTop={1}>
500+
<tab_select
501+
ref={(node) => {
502+
tabSelect = node;
503+
if (tabSelect) {
504+
const index = viewIndexByName.get(state.view) ?? 0;
505+
tabSelect.setSelectedIndex(index);
506+
}
507+
}}
508+
options={viewTabs}
509+
tabWidth={tabWidth()}
510+
showDescription={false}
511+
showUnderline={true}
512+
wrapSelection={true}
513+
backgroundColor={theme.panel}
514+
textColor={theme.textMuted}
515+
focusedBackgroundColor={theme.border}
516+
focusedTextColor={theme.text}
517+
selectedBackgroundColor={theme.accent}
518+
selectedTextColor={theme.panel}
519+
selectedDescriptionColor={theme.text}
520+
onSelect={(_, option) => {
521+
if (!option?.value) return;
522+
setView(option.value as ViewName);
523+
}}
524+
/>
525+
</box>
526+
429527
<Show when={state.view === "overview"}>
430528
<box flexDirection="row" gap={4} paddingTop={1}>
431529
<box width={Math.floor(dimensions().width / 2) - 4} flexDirection="column" gap={1}>
@@ -596,6 +694,8 @@ export function startOpenwrkTui(options: TuiOptions): TuiHandle {
596694
<text fg={theme.textMuted}>T: Telegram token (owpenbot)</text>
597695
<text fg={theme.textMuted}>D: Detach</text>
598696
<text fg={theme.textMuted}>Q: Quit</text>
697+
<text fg={theme.textMuted}>Mouse: click tabs</text>
698+
<text fg={theme.textMuted}>Mouse: select text to copy</text>
599699
</box>
600700
</Show>
601701

@@ -615,6 +715,8 @@ export function startOpenwrkTui(options: TuiOptions): TuiHandle {
615715
targetFps: 60,
616716
gatherStats: false,
617717
exitOnCtrlC: false,
718+
useMouse: true,
719+
enableMouseMovement: true,
618720
useKittyKeyboard: {},
619721
autoFocus: false,
620722
consoleOptions: {

0 commit comments

Comments
 (0)