UNPKG

detect-terminal

Version:

Detect the terminal program currently being used, with support for iTerm, Terminal.app, Hyper, iTerm2, ConEmu, Cmde,r Alacritty, Xterm, Terminator, Termux, Kitty, and others. Detection is based on environment variables and process-level indicators to iden

256 lines (252 loc) 7.81 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/parent.ts var parent_exports = {}; __export(parent_exports, { detectFromParentProcess: () => detectFromParentProcess, detectFromParentProcessResult: () => detectFromParentProcessResult, getParentProcess: () => getParentProcess }); module.exports = __toCommonJS(parent_exports); var import_node_child_process = require("child_process"); // src/color.ts var TRUE_COLOR_PATTERN = /^(truecolor|24bits?)$/; var has256Color = /* @__PURE__ */ __name((value) => { return Boolean(value?.match(/256/)); }, "has256Color"); var supportsTrueColor = /* @__PURE__ */ __name((value) => { return Boolean(value?.match(TRUE_COLOR_PATTERN)); }, "supportsTrueColor"); var getColorSupport = /* @__PURE__ */ __name((env) => { return { supports256Color: has256Color(env.TERM) || has256Color(env.COLORTERM), supportsTrueColor: supportsTrueColor(env.COLORTERM) }; }, "getColorSupport"); // src/normalize.ts var import_node_path = __toESM(require("path")); var SHELL_PROCESS_NAMES = /* @__PURE__ */ new Set([ "bash", "csh", "dash", "fish", "ksh", "login", "node", "nodejs", "nu", "sh", "sudo", "tcsh", "zsh" ]); var normalizeIdentifier = /* @__PURE__ */ __name((value) => { return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "_"); }, "normalizeIdentifier"); var normalizeParentProcessName = /* @__PURE__ */ __name((processName) => { const normalized = normalizeIdentifier(import_node_path.default.parse(processName).name); switch (normalized) { case "alacritty": case "aterm": case "atomic_terminal": case "eterm": case "foot": case "ghostty": case "hyper": case "kitty": case "konsole": case "kuake": case "mate_terminal": case "mrxvt": case "powershell": case "putty": case "qterminal": case "rio": case "rxvt": case "terminator": case "terminology": case "tilda": case "tmux": case "warp": case "wezterm": case "wterm": return normalized; case "apple_terminal": case "terminal": return "terminal"; case "cmd": return "cmd"; case "code": case "code_insiders": return "vscode"; case "conhost": return "conhost"; case "gnome": case "gnome_terminal": case "gnome_terminal_server": case "guake": return "gnome_terminal"; case "iterm": case "iterm2": return "iterm"; case "login": case "linux": return "linux_console"; case "pwsh": return "powershell"; case "screen": return "screen"; case "terminal_app": return "terminal_app"; case "urxvt": case "urxvt256c": case "urxvt256c_ml": case "urxvt_ml": return "rxvt"; case "wt": case "windows_terminal": return "windows_terminal"; case "xfce": case "xfce_terminal": case "xfce4_terminal": return "xfce4_terminal"; default: { if (normalized.startsWith("gnome_terminal")) { return "gnome_terminal"; } if (normalized.startsWith("warp")) { return "warp"; } return null; } } }, "normalizeParentProcessName"); // src/parent.ts var runPs = /* @__PURE__ */ __name((run, args) => { return run("ps", args, { encoding: "utf8", timeout: 1e3, stdio: ["ignore", "pipe", "ignore"] }); }, "runPs"); var getParentProcess = /* @__PURE__ */ __name((pid, options = {}) => { const run = options.execFileSync ?? import_node_child_process.execFileSync; const parentPid = Number.parseInt( runPs(run, ["-o", "ppid=", "-p", String(pid)]).trim(), 10 ); if (!Number.isFinite(parentPid) || parentPid <= 1) { return null; } const appName = runPs(run, ["-o", "comm=", "-p", String(parentPid)]).trim(); if (!appName) { return null; } return { pid: parentPid, appName }; }, "getParentProcess"); var colorVariant = /* @__PURE__ */ __name((terminal, supports256Color, supportsTrueColor2) => { return supports256Color || supportsTrueColor2 ? `${terminal}_256color` : terminal; }, "colorVariant"); var applyParentColorVariant = /* @__PURE__ */ __name((terminal, env) => { const { supports256Color, supportsTrueColor: supportsTrueColor2 } = getColorSupport(env); switch (terminal) { case "eterm": case "konsole": case "rxvt": return colorVariant(terminal, supports256Color, supportsTrueColor2); case "gnome_terminal": case "terminator": return colorVariant("gnome_terminal", supports256Color, supportsTrueColor2); default: return terminal; } }, "applyParentColorVariant"); var detectFromParentProcessResult = /* @__PURE__ */ __name((options = {}) => { const env = options.env ?? process.env; const platform = options.platform ?? process.platform; if (platform === "win32" || env.SSH_CONNECTION) { return null; } const seenPids = /* @__PURE__ */ new Set(); let pid = options.pid ?? process.pid; for (let depth = 0; depth < 12; depth += 1) { if (seenPids.has(pid)) { break; } seenPids.add(pid); try { const parent = getParentProcess(pid, options); if (!parent) { break; } const normalized = normalizeParentProcessName(parent.appName); if (normalized === "screen" || normalized === "tmux") { if (!options.preferOuter) { return { terminal: normalized, source: "parent", appName: parent.appName, pid: parent.pid }; } pid = parent.pid; continue; } if (normalized) { return { terminal: applyParentColorVariant(normalized, env), source: "parent", appName: parent.appName, pid: parent.pid }; } if (!SHELL_PROCESS_NAMES.has(normalizeIdentifier(parent.appName))) { break; } pid = parent.pid; } catch { break; } } return null; }, "detectFromParentProcessResult"); var detectFromParentProcess = /* @__PURE__ */ __name((options = {}) => { return detectFromParentProcessResult(options)?.terminal ?? null; }, "detectFromParentProcess"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { detectFromParentProcess, detectFromParentProcessResult, getParentProcess }); //# sourceMappingURL=parent.js.map