Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the terminal tab title from Node.js?

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

echo -n -e "\033]0;My terminal tab title\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.

like image 641
Paul Go Avatar asked Apr 09 '15 20:04

Paul Go


People also ask

What is terminal in node JS?

Advertisements. REPL stands for Read Eval Print Loop and it represents a computer environment like a Windows console or Unix/Linux shell where a command is entered and the system responds with an output in an interactive mode. Node.js or Node comes bundled with a REPL environment.

How do I get node JS terminal?

js/JavaScript code. To launch the REPL (Node shell), open command prompt (in Windows) or terminal (in Mac or UNIX/Linux) and type node as shown below. It will change the prompt to > in Windows and MAC. You can now test pretty much any Node.

What is the syntax of node JS?

var _message="Hello World"; console. log(_message); Like any other programming language, in nodejs we have single line comment using double slash like // and multiline comments using single slash with star like /* */ .


2 Answers

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)
  );
}
like image 135
Paul Go Avatar answered Oct 04 '22 11:10

Paul Go


There is node library for that: 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:

"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 :)

like image 37
abnormi Avatar answered Oct 04 '22 09:10

abnormi