After some tests for hiding my console cursor.
I've found those npm packages that implements functionnalities for hiding and showing back again the cursor.
Looking into those packages files I've found that the writing in console of the following sequences is used to perform such thing :
\u001B[?25l: hides the cursor when writted or logged to console
\u001B[?25h: shows the cursor when writted or logged to console
When any of those methods is used for hiding the console cursor in an external terminal the cursor will not be displayed.
But using it inside the integrated Visual Studio Code terminal I noticed that the cursor was still displayed but at a very different position.
So I guess those methods simply hides the cursor by moving it outside of the console visible frame.
This lead me to this question :
Is there a way in js or NodeJs to hide the console cursor without affecting it's position ?
or if not possible:
What it would imply in case we want to move the cursor while it is not visible ?
After some more tests I removed the alternative question as moving the cursor while it is not visible see it restored to the right position when it is showed again.
For example:
Using the readline interface to write the sequences to console.
import readline from 'readline'; // ES (*.mjs or type module in package.json)
// OR
const readline = require('readline'); // CommonJS
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.write('\u001B[?25l'); // hides cursor
rl.write('\u001B[?25h'); // show cursor
This hides and show back the cursor at the same position.
If the instruction: readline.moveCursor();
is used between the hide and show instructions the cursor will show up on the right position.
Using the cli-cursor package as below will produce the same result.
import cliCursor from 'cli-cursor';
cliCursor.hide();
cliCursor.show();
For the ansi-escapes npm-package I assume it will behave the same but haven't verified it.
Note:
If you decide to use console.log() instead of rl.write() to hide and show the cursor, you will have to correct the cursor position by using this instruction :
readline.moveCursor(process.stdout, 0, -2);
I assume this is because each time console.log() is called it adds a new line in the terminal.