@nuxt/cli
Version:
Nuxt CLI
64 lines (63 loc) • 2.51 kB
JavaScript
import { T as writeDirect, f as debug, i as useTerminalHost } from "./startup-clock-SyYej6uN.mjs";
import process from "node:process";
import { isCI } from "std-env";
import { Buffer } from "node:buffer";
// src/utils/display.ts
const DISPLAY_REQUIRED_PLATFORMS = /* @__PURE__ */ new Set([
"linux",
"freebsd",
"openbsd"
]);
/**
* Whether a graphical session exists to receive a clipboard write or a browser
* launch. The tools involved on Linux and BSD (`wl-copy`, `xsel`, `xclip`,
* `xdg-open`) all need one, and fail in unhelpful ways without it: clipboard
* tools exit before reading their input, so the write fails with `EPIPE`.
*/
function hasDisplayServer(env = process.env) {
if (!DISPLAY_REQUIRED_PLATFORMS.has(process.platform)) return true;
return !!(env.WSL_DISTRO_NAME || env.WAYLAND_DISPLAY || env.DISPLAY);
}
// src/utils/clipboard.ts
/**
* Terminals known to honour OSC 52. Support cannot be queried, and a terminal
* that ignores the sequence stays silent, so anything unlisted is left alone.
* Behind tmux the real terminal is hidden, so tmux is trusted to relay the
* sequence (`set-clipboard on`). A `screen` TERM counts only inside tmux, as
* GNU screen drops OSC 52.
*/
const OSC52_TERMS = /^(?:xterm-kitty|xterm-ghostty|foot|alacritty|wezterm|tmux)/i;
const OSC52_PROGRAMS = /* @__PURE__ */ new Set([
"iterm.app",
"wezterm",
"ghostty"
]);
/**
* Write `text` to the clipboard, resolving whether it succeeded. Uses the
* platform clipboard where it can be reached, and otherwise asks the terminal
* to set it with OSC 52, which also works over `ssh` and in containers.
*/
async function writeClipboard(text, options = {}) {
if (await writeSystemClipboard(text)) return true;
const write = options.write ?? (useTerminalHost() ? void 0 : writeDirect);
if (!write || !canReachTerminal()) return false;
write(`\u001B]52;c;${Buffer.from(text, "utf8").toString("base64")}\u0007`);
return true;
}
async function writeSystemClipboard(text) {
if (!hasDisplayServer()) return false;
try {
const { writeText } = await import("tinyclip");
await writeText(text);
return true;
} catch (error) {
debug("Failed to write to the clipboard:", error);
return false;
}
}
function canReachTerminal() {
if (!process.stdout.isTTY || isCI) return false;
const { TERM = "", TERM_PROGRAM = "", TMUX } = process.env;
return OSC52_TERMS.test(TERM) || !!TMUX && /^screen/i.test(TERM) || OSC52_PROGRAMS.has(TERM_PROGRAM.toLowerCase());
}
export { hasDisplayServer as n, writeClipboard as t };