如何从Node.js设置终端选项卡标题?

时间:2022-11-21 07:37:03

I'm aware that this can be done manually from the terminal using:

我知道这可以通过以下方式从终端手动完成:

echo -n -e "\033]0;My terminal tab title\007"

echo -n -e“\ 033] 0;我的终端标签标题\ 007”

I tried putting this into a console.log and process.stdout.write and fiddling with the escaping, but I can't get it to work.

我尝试将它放入console.log和process.stdout.write并摆弄转义,但我无法让它工作。

2 个解决方案

#1


16  

To save anyone reading this a bit of time, here is a function that will do it in strict mode:

为了节省任何阅读这个时间的人,这里有一个函数,它将在严格模式下执行:

function setTerminalTitle(title)
{
  process.stdout.write(
    String.fromCharCode(27) + "]0;" + title + String.fromCharCode(7)
  );
}

#2


0  

There is node library for that: node-bash-title

有节点库:node-bash-title

Usage in node

To install the library:

要安装库:

npm install node-bash-title --save

And within your script:

在你的脚本中:

const setTitle = require('node-bash-title');
setTitle('????  Server');

Usage in NPM script

This package also provides an executable script. You can use that in your npm scripts. For example:

该包还提供可执行脚本。您可以在npm脚本中使用它。例如:

"scripts": {
    "start:dev": "set-bash-title server && node server/app.js"
    "start:prod": "node server/app.js"
 },

So you can remove the title script from your code. Furthermore, the title is only set if you want to set a title(in development mode). In production mode you may not want to set a title as your script may not be executed in a XTerm :)

因此,您可以从代码中删除标题脚本。此外,仅在您要设置标题(在开发模式下)时才设置标题。在生产模式下,您可能不想设置标题,因为您的脚本可能无法在XTerm中执行:)

#1


16  

To save anyone reading this a bit of time, here is a function that will do it in strict mode:

为了节省任何阅读这个时间的人,这里有一个函数,它将在严格模式下执行:

function setTerminalTitle(title)
{
  process.stdout.write(
    String.fromCharCode(27) + "]0;" + title + String.fromCharCode(7)
  );
}

#2


0  

There is node library for that: node-bash-title

有节点库:node-bash-title

Usage in node

To install the library:

要安装库:

npm install node-bash-title --save

And within your script:

在你的脚本中:

const setTitle = require('node-bash-title');
setTitle('????  Server');

Usage in NPM script

This package also provides an executable script. You can use that in your npm scripts. For example:

该包还提供可执行脚本。您可以在npm脚本中使用它。例如:

"scripts": {
    "start:dev": "set-bash-title server && node server/app.js"
    "start:prod": "node server/app.js"
 },

So you can remove the title script from your code. Furthermore, the title is only set if you want to set a title(in development mode). In production mode you may not want to set a title as your script may not be executed in a XTerm :)

因此,您可以从代码中删除标题脚本。此外,仅在您要设置标题(在开发模式下)时才设置标题。在生产模式下,您可能不想设置标题,因为您的脚本可能无法在XTerm中执行:)