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

269 lines (265 loc) 9.16 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/env.ts var env_exports = {}; __export(env_exports, { detectFromEnv: () => detectFromEnv, detectFromEnvResult: () => detectFromEnvResult }); module.exports = __toCommonJS(env_exports); // 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 normalizeIdentifier = /* @__PURE__ */ __name((value) => { return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "_"); }, "normalizeIdentifier"); var normalizeAppName = /* @__PURE__ */ __name((value, platform) => { const appName = platform === "darwin" ? import_node_path.default.parse(value).name : value; return normalizeIdentifier(appName); }, "normalizeAppName"); var hasKonsoleEnvironment = /* @__PURE__ */ __name((env) => { return Object.keys(env).some((envVar) => /konsole/i.test(envVar)); }, "hasKonsoleEnvironment"); // src/env.ts var colorVariant = /* @__PURE__ */ __name((terminal, supports256Color, supportsTrueColor2) => { return supports256Color || supportsTrueColor2 ? `${terminal}_256color` : terminal; }, "colorVariant"); var normalizeTermProgram = /* @__PURE__ */ __name((termProgram) => { switch (termProgram) { case "apple_terminal": return "terminal"; case "eterm": return "eterm"; case "gnome_terminal": case "gnome_terminal_server": return "gnome_terminal"; case "hyper": return "hyper"; case "iterm_app": case "iterm": case "iterm2": return "iterm"; case "kitty": return "kitty"; case "konsole": return "konsole"; case "mate_terminal": return "mate_terminal"; case "powershell": return "powershell"; case "putty": return "putty"; case "qterminal": return "qterminal"; case "rio": return "rio"; case "rxvt": return "rxvt"; case "terminal_app": return "terminal_app"; case "terminal": return "terminal"; case "terminator": return "terminator"; case "termux": return "termux"; case "vscode": return "vscode"; case "warp": return "warp"; case "wezterm": return "wezterm"; case "xfce4_terminal": return "xfce4_terminal"; case "alacritty": return "alacritty"; default: return null; } }, "normalizeTermProgram"); var detectFromTerm = /* @__PURE__ */ __name((term, env, platform) => { const { supports256Color, supportsTrueColor: supportsTrueColor2 } = getColorSupport(env); if (term === "xterm" || term === "xterm_256color") { if (supportsTrueColor2) { return "xterm_truecolor"; } if (env.VTE_VERSION) { const vteVersion = Number.parseInt(env.VTE_VERSION, 10); if (vteVersion >= 3803) { return colorVariant("gnome_terminal", supports256Color, supportsTrueColor2); } } if (hasKonsoleEnvironment(env)) { return colorVariant("konsole", supports256Color, supportsTrueColor2); } if (platform === "darwin") { return "terminal"; } return term; } switch (term) { case "linux": return "linux_console"; case "gnome": case "gnome_256color": case "gnome_terminal": case "gnome_terminal_256color": case "guake": case "terminator": return colorVariant("gnome_terminal", supports256Color, supportsTrueColor2); case "konsole": return colorVariant("konsole", supports256Color, supportsTrueColor2); case "rxvt": case "rxvt_xpm": case "rxvt_unicode": case "rxvt_unicode_256color": case "urxvt": case "urxvt_ml": case "urxvt256c": case "urxvt256c_ml": return colorVariant("rxvt", term === "rxvt" || supports256Color, supportsTrueColor2); case "xfce": case "xfce_terminal": case "xfce4_terminal": return "xfce4_terminal"; case "eterm": return colorVariant("eterm", supports256Color, supportsTrueColor2); case "xterm_kitty": case "kitty": return "kitty"; case "screen": case "screen_256color": return "screen"; case "tmux": case "tmux_256color": return "tmux"; case "vt100": return "vt100"; case "aterm": case "atomic_terminal": case "dopamine": case "foot": case "ghostty": case "kuake": case "rio": case "terminology": case "termux": case "tilda": case "wterm": case "mrxvt": return term; default: if (/screen/.test(term)) return "screen"; if (/tmux/.test(term)) return "tmux"; if (/rxvt/.test(term)) return "rxvt"; if (/vt100/.test(term)) return "vt100"; if (/linux/.test(term)) return "linux_console"; if (/alacritty/.test(term)) return "alacritty"; if (/dopamine/.test(term)) return "dopamine"; if (/kitty/.test(term)) return "kitty"; if (/ghostty/.test(term)) return "ghostty"; if (/^foot/.test(term)) return "foot"; if (/^rio/.test(term)) return "rio"; return term; } }, "detectFromTerm"); var detectFromEnvResult = /* @__PURE__ */ __name((options = {}) => { const env = options.env ?? process.env; const platform = options.platform ?? process.platform; const term = env.TERM ? normalizeIdentifier(env.TERM) : void 0; if (platform === "android" && env.TERMUX_VERSION) { return { terminal: "termux", source: "env", generic: term }; } if (!options.preferOuter && term) { if (term.startsWith("tmux")) { return { terminal: "tmux", source: "env", generic: term }; } if (term.startsWith("screen")) { return { terminal: "screen", source: "env", generic: term }; } } if (env.KITTY_PID) { return { terminal: "kitty", source: "env", generic: term }; } if (env.GHOSTTY_RESOURCES_DIR) { return { terminal: "ghostty", source: "env", generic: term }; } if (env.ALACRITTY_SOCKET || env.ALACRITTY_LOG) { return { terminal: "alacritty", source: "env", generic: term }; } const termProgram = env.TERM_PROGRAM ? normalizeAppName(env.TERM_PROGRAM, platform) : void 0; if (termProgram) { return { terminal: normalizeTermProgram(termProgram) ?? termProgram, source: "env", generic: term }; } if (typeof env.VSCODE_PID !== "undefined" || typeof env.TERM_PROGRAM_VERSION !== "undefined" && /vscode/i.test(env.TERM_PROGRAM_VERSION)) { return { terminal: "vscode", source: "env", generic: term }; } if (term && term !== "unknown") { return { terminal: detectFromTerm(term, env, platform), source: "env", generic: term }; } const colorTerm = env.COLORTERM ? normalizeIdentifier(env.COLORTERM) : void 0; if (colorTerm === "truecolor" || colorTerm === "24bit" || colorTerm === "24bits") { return { terminal: "truecolor_terminal", source: "env", generic: term }; } if (colorTerm) { return { terminal: colorTerm, source: "env", generic: term }; } return null; }, "detectFromEnvResult"); var detectFromEnv = /* @__PURE__ */ __name((options = {}) => { return detectFromEnvResult(options)?.terminal ?? null; }, "detectFromEnv"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { detectFromEnv, detectFromEnvResult }); //# sourceMappingURL=env.js.map