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

173 lines (172 loc) 5.66 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // index.ts import { execSync } from "node:child_process"; import path from "node:path"; var detectFromEnv = /* @__PURE__ */ __name(() => { const platform = process.platform; if (platform === "android" && process.env.TERMUX_VERSION) { return "termux"; } let termProgram = process.env.TERM_PROGRAM?.trim()?.toLowerCase(); if (platform === "darwin" && termProgram) { termProgram = path.parse(termProgram).name; } if (termProgram) { switch (termProgram) { case "apple_terminal": return "terminal"; case "eterm": return "eterm"; case "gnome-terminal": return "gnome_terminal"; case "gnome-terminal-server": return "gnome_terminal"; case "hyper": return "hyper"; case "iterm.app": return "iterm"; case "iterm": return "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 "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: break; } return termProgram.replace(/[^a-z0-9]+/g, "_"); } if (typeof process.env.VSCODE_PID !== "undefined" || typeof process.env.TERM_PROGRAM_VERSION !== "undefined" && /vscode/i.test(process.env.TERM_PROGRAM_VERSION)) { return "vscode"; } const term = process.env.TERM?.trim()?.toLowerCase(); if (term && term !== "unknown") { if (term === "xterm" || term === "xterm-256color") { if (process.env.VTE_VERSION) { const vteVersion = parseInt(process.env.VTE_VERSION, 10); if (vteVersion >= 3803) { return "gnome_terminal"; } } for (const [key] of Object.entries(process.env)) { if (/konsole/i.test(key)) { return "konsole"; } } if (platform === "darwin") { return "terminal"; } return "xterm"; } 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"; return term.replace(/[^a-z0-9]+/g, "_"); } const colorTerm = process.env.COLORTERM?.trim()?.toLowerCase(); if (colorTerm === "truecolor" || colorTerm === "24bit") { return "truecolor_terminal"; } if (colorTerm) { return colorTerm.replace(/[^a-z0-9]+/g, "_"); } return null; }, "detectFromEnv"); var detectFromShell = /* @__PURE__ */ __name(() => { try { const isWindows = process.platform === "win32"; if (isWindows) { if (process.env.WT_SESSION) return "windows_terminal"; const shell = process.env.COMSPEC?.toLowerCase() || ""; if (/powershell/i.test(shell)) return "powershell"; if (/pwsh/i.test(shell)) return "powershell"; if (/cmd\.exe/i.test(shell)) return "cmd"; if (/wt\.exe/i.test(shell)) return "windows_terminal"; if (/conhost\.exe/i.test(shell)) return "conhost"; return "windows_cmd"; } const terminal = execSync("echo $TERM", { encoding: "utf8", timeout: 1e3, stdio: ["ignore", "pipe", "ignore"] }).trim().toLowerCase(); return terminal ? terminal.replace(/[^a-z0-9]+/g, "_") : null; } catch { return null; } }, "detectFromShell"); var detectFromProcessTitle = /* @__PURE__ */ __name(() => { const processTitle = process.title?.toLowerCase() ?? ""; if (/^alacritty/.test(processTitle)) return "alacritty"; if (/^kitty/.test(processTitle)) return "kitty"; if (/^wezterm/.test(processTitle)) return "wezterm"; if (/^hyper/.test(processTitle)) return "hyper"; if (/bash/.test(processTitle)) return "bash"; if (/zsh/.test(processTitle)) return "zsh"; if (/ksh/.test(processTitle)) return "ksh"; if (/fish/.test(processTitle)) return "fish"; if (/csh/.test(processTitle)) return "csh"; if (/tcsh/.test(processTitle)) return "tcsh"; if (/pwsh/.test(processTitle)) return "powershell"; if (/powershell/.test(processTitle)) return "powershell"; if (/cmd/.test(processTitle)) return "cmd"; if (/sh$/.test(processTitle)) return "sh"; if (/^node/.test(processTitle)) return "node"; return null; }, "detectFromProcessTitle"); var detectTerminal = /* @__PURE__ */ __name(() => { let terminal = detectFromEnv(); if (!terminal) { terminal = detectFromShell(); } if (!terminal) { terminal = detectFromProcessTitle(); } return terminal || "unknown"; }, "detectTerminal"); var detect_terminal_default = detectTerminal; export { detect_terminal_default as default, detectFromEnv, detectFromProcessTitle, detectFromShell, detectTerminal }; //# sourceMappingURL=index.mjs.map