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
102 lines (99 loc) • 2.61 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/shell.ts
import { execFileSync as nodeExecFileSync } from "child_process";
// src/normalize.ts
import path from "path";
var normalizeIdentifier = /* @__PURE__ */ __name((value) => {
return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "_");
}, "normalizeIdentifier");
// src/shell.ts
var runShellTerm = /* @__PURE__ */ __name((run) => {
return run("sh", ["-c", 'printf %s "$TERM"'], {
encoding: "utf8",
timeout: 1e3,
stdio: ["ignore", "pipe", "ignore"]
});
}, "runShellTerm");
var detectFromShellResult = /* @__PURE__ */ __name((options = {}) => {
const env = options.env ?? process.env;
const platform = options.platform ?? process.platform;
try {
if (platform === "win32") {
if (env.WT_SESSION) {
return {
terminal: "windows_terminal",
source: "shell"
};
}
const shell = env.COMSPEC?.toLowerCase() || "";
if (/powershell/i.test(shell)) {
return {
terminal: "powershell",
source: "shell"
};
}
if (/pwsh/i.test(shell)) {
return {
terminal: "powershell",
source: "shell"
};
}
if (/cmd\.exe/i.test(shell)) {
return {
terminal: "cmd",
source: "shell"
};
}
if (/wt\.exe/i.test(shell)) {
return {
terminal: "windows_terminal",
source: "shell"
};
}
if (/conhost\.exe/i.test(shell)) {
return {
terminal: "conhost",
source: "shell"
};
}
return {
terminal: "windows_cmd",
source: "shell"
};
}
const run = options.execFileSync ?? nodeExecFileSync;
const terminal = normalizeIdentifier(runShellTerm(run));
if (!terminal) {
return null;
}
if (!options.preferOuter) {
if (terminal.startsWith("tmux")) {
return {
terminal: "tmux",
source: "shell"
};
}
if (terminal.startsWith("screen")) {
return {
terminal: "screen",
source: "shell"
};
}
}
return {
terminal,
source: "shell"
};
} catch {
return null;
}
}, "detectFromShellResult");
var detectFromShell = /* @__PURE__ */ __name((options = {}) => {
return detectFromShellResult(options)?.terminal ?? null;
}, "detectFromShell");
export {
detectFromShell,
detectFromShellResult
};
//# sourceMappingURL=shell.mjs.map