-
-
Save timneutkens/f2933558b8739bbf09104fb27c5c9664 to your computer and use it in GitHub Desktop.
const readline = require('readline') | |
const blank = '\n'.repeat(process.stdout.rows) | |
console.log(blank) | |
readline.cursorTo(process.stdout, 0, 0) | |
readline.clearScreenDown(process.stdout) |
Have you tried
console.clear()
?
it worked perfectly fine for me!
//this also stops someone scrolling back and viewing sensitive data that may have been logged
function clearConsoleAndScrollbackBuffer() {
process.stdout.write("\u001b[3J\u001b[2J\u001b[1J");console.clear();
}
@EdgardoRodriguezSolano you're not preserving history if you do that.
console.clear()
removes all terminal data. Not the current stdout
.
Bravo! it works.... Professional!
console.clear()
It doesn't work for me
const readline = require('readline')
const blank = '\n'.repeat(process.stdout.rows)
console.log(blank)
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout)
And that to
I wonder what kind of console you clear with theses approach but none of them does not works for me.
As you can see in the image, my whole node output console still there.
Side note on this topic: I used the solution in the gist, but I got curious about something. I checked the source code for rollup --watch
since that includes an option to clear the screen. I noticed it uses '\u001Bc'
(which the beginning of @jonathon-annett's snippet above). Just noting.
Another note, if you're using nodemon
and want to clear the console when nodemon restarts; here is a relevant tip:
This is what the nodemon events are for. You can include either a global or local
nodemon.json
file as per the following to do what you're after.As per the design principles, I'd rather re-use the current tech rather than introduce new features:
{ "events": { "start": "echo \"\\x1Bc\"" } }
It is from this thread, which has more info about portability if you must deal with Windows.
//this also stops someone scrolling back and viewing sensitive data that may have been logged function clearConsoleAndScrollbackBuffer() { process.stdout.write("\u001b[3J\u001b[2J\u001b[1J");console.clear(); }
Worked for me, thanks @jonathan-annett !
For macOS to get the terminal to clear including scrollback I use this.
process.stdout.write('\u001Bc\u001B[3J');
It is a NodeJS compatible version of \33c\e[3J
(Note: Octal escapes will throw in strict mode so they must be converted)
The sequences being used are:
ESC c
- Reset to initial state
CSI 3J
- Clear entire screen and delete all lines saved in the scrollback buffer
For those interested in learning the escape sequences these two resources were useful.
https://en.wikipedia.org/wiki/ANSI_escape_code
https://bjh21.me.uk/all-escapes/all-escapes.txt
Have you tried
console.clear()
?