Last active
March 4, 2024 14:01
-
-
Save timneutkens/f2933558b8739bbf09104fb27c5c9664 to your computer and use it in GitHub Desktop.
Clear console/terminal in node.js the right way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require('readline') | |
const blank = '\n'.repeat(process.stdout.rows) | |
console.log(blank) | |
readline.cursorTo(process.stdout, 0, 0) | |
readline.clearScreenDown(process.stdout) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked for me, thanks @jonathan-annett !