Make URLs in terminal output clickable via xterm web-links + tauri-plugin-opener

This commit is contained in:
megaproxy 2026-05-25 19:13:08 +01:00
parent 234a0b74a1
commit a24f7de7df
6 changed files with 36 additions and 1 deletions

View file

@ -1,11 +1,13 @@
import { useRef, useEffect } from "react";
import { Terminal } from "@xterm/xterm";
import { FitAddon } from "@xterm/addon-fit";
import { WebLinksAddon } from "@xterm/addon-web-links";
import type { UnlistenFn } from "@tauri-apps/api/event";
import {
readText as clipboardReadText,
writeText as clipboardWriteText,
} from "@tauri-apps/plugin-clipboard-manager";
import { openUrl } from "@tauri-apps/plugin-opener";
import {
spawnPane,
writeToPane,
@ -124,6 +126,16 @@ export default function XtermPane({
const fit = new FitAddon();
fitRef.current = fit;
term.loadAddon(fit);
// Underlines http(s) URLs in the terminal output and routes clicks
// through Tauri's opener plugin so they open in the user's default
// browser (WebView2 won't navigate on a plain window.open).
term.loadAddon(
new WebLinksAddon((_event, uri) => {
void openUrl(uri).catch((err) =>
console.warn("openUrl failed:", err),
);
}),
);
term.open(container);
// Initial size — fit before asking the PTY for its dimensions.