在电子方面,我正在注册打开终端的快捷方式:
globalShortcut.register('CommandOrControl+Alt+Shift+P', () => {
spawn(os.platform() === 'linux' ? 'xterm' : 'cmd');
});
[我注意到xterm
与从OS菜单打开的终端没有相同的“样式”,我发现后者是由桌面环境自定义的(我有一个Ubuntu Mate,其终端为mate-terminal
,带konsole
的RedHat 7.5)。
[我读到here,nodejs没有用于检测分发的API,因此根据分发来知道运行哪个终端似乎不可行。
有没有办法打开正确的终端或将xterm
设置为OS的样式?
Node.js没有提供本地检测分发名称的方法,因为没有标准方法可以从不同的[[Linux分发中获取该信息。
但是您可以将库添加为getos:const getos = require('getos');
getos((err, os) => {
if (err) return console.log(err);
console.log(os.dist, os.codename)
})
TL; DR:没有为此的标准化API,并且您不能确定分发是否表示终端应用程序。
首先,XTerm,Mate's Terminal,Konsole等都是不同的应用程序。而且由于Ubuntu Mate在后台显示为Ubuntu AFAIK,因此您甚至不能确定这是注册为“ the”终端应用程序的应用程序。另外,几乎每个桌面环境都具有自己的终端应用程序,并且由于可以并排安装多个终端应用程序,因此自动选择“合适的”终端应用程序至多是有根据的猜测。但是,可以考虑多种方法来解决此(美学)问题:
which
命令。示例:const { spawnSync } = require ("child_process");
const terms = [ "konsole", "mate-terminal", "gnome-terminal" /* ... */ ];
var terminal = null;
// In your startup method (e.g. before opening a BrowserWindow)
if (os.platform () === "linux") {
foreach (term in terms) {
which = spawnSync ("which", [term]);
if (which.status !== null && which.status === 0) { // Command found
terminal = term;
break;
}
}
if (terminal === null) terminal = "xterm";
} else {
terminal = "cmd";
}
当然,您也可以使用spawn
来代替spawnSync
,但这会变得更加复杂。此外,这仅为您提供第一个被发现安装的应用程序,它没有为桌面环境选择一个“合适的”应用程序。但是我相信,如果您希望(某种程度上)将您的应用程序看起来(似乎)集成到DE中,这是朝正确方向迈出的良好一步。
作为旁注,您的代码未考虑MacOS,但也可以在MacOS上打开终端,在此简称为“ Terminal”。