mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
Read Finder file URLs before icon image data on macOS. Quote paths in bash mode and reject terminal control characters before rendering. Fixes #9999 Co-authored-by: 1dustycy <onedustycy@gmail.com>
65 lines
2.5 KiB
TypeScript
65 lines
2.5 KiB
TypeScript
import { createRequire } from "node:module";
|
|
import * as path from "node:path";
|
|
import { getNativeModuleCandidates } from "./native-module-path.ts";
|
|
|
|
const cjsRequire = createRequire(import.meta.url);
|
|
|
|
export type ModifierKey = "shift" | "command" | "control" | "option";
|
|
|
|
export interface NativeClipboard {
|
|
/** Undefined means unavailable, null means no text; transfer failures reject. */
|
|
getText(): Promise<string | null | undefined>;
|
|
/** Undefined means unavailable, null means no image; transfer failures reject. */
|
|
getImage(): Promise<Uint8Array | null | undefined>;
|
|
/** POSIX paths of file URLs on the clipboard. Undefined means unsupported, null means no files. */
|
|
getFilePaths?(): Promise<string[] | null | undefined>;
|
|
/** Linux uses command-line tools to retain clipboard ownership instead. */
|
|
setText?(text: string): Promise<void>;
|
|
}
|
|
|
|
type NativePlatformHelper = NativeClipboard & {
|
|
enableVirtualTerminalInput?: () => boolean;
|
|
isModifierPressed?: (name: ModifierKey) => boolean;
|
|
};
|
|
|
|
// Cache module loading, not display availability: a disconnected display can recover.
|
|
const helpers = new Map<string, NativePlatformHelper | undefined>();
|
|
|
|
function loadNativePlatformHelper(platform: string, suffix = ""): NativePlatformHelper | undefined {
|
|
const arch = process.arch;
|
|
if (arch !== "x64" && arch !== "arm64") return undefined;
|
|
const nativePath = path.join(
|
|
"native",
|
|
platform,
|
|
"prebuilds",
|
|
`${platform}-${arch}`,
|
|
`${platform}-platform${suffix}.node`,
|
|
);
|
|
if (helpers.has(nativePath)) return helpers.get(nativePath);
|
|
|
|
for (const modulePath of getNativeModuleCandidates(nativePath)) {
|
|
try {
|
|
const helper = cjsRequire(modulePath) as Partial<NativePlatformHelper> | null;
|
|
if (typeof helper?.getText === "function" && typeof helper.getImage === "function") {
|
|
helpers.set(nativePath, helper as NativePlatformHelper);
|
|
return helper as NativePlatformHelper;
|
|
}
|
|
} catch {
|
|
// Try the next possible packaging location.
|
|
}
|
|
}
|
|
helpers.set(nativePath, undefined);
|
|
return undefined;
|
|
}
|
|
|
|
export function getNativePlatformHelper(): NativePlatformHelper | undefined {
|
|
if (process.platform !== "darwin" && process.platform !== "win32") return undefined;
|
|
return loadNativePlatformHelper(process.platform);
|
|
}
|
|
|
|
/** Load a clipboard helper without opening the display until a read is requested. */
|
|
export function getNativeClipboard(): NativeClipboard | undefined {
|
|
if (process.platform !== "linux") return getNativePlatformHelper();
|
|
if (!process.env.DISPLAY) return undefined;
|
|
return loadNativePlatformHelper("linux", "-x11");
|
|
}
|