Libon

清空控制台打印内容

0Mins #JavaScript#nodejs#console
通过 node 内置 readline 模块来读取、清空控制台打印
1
import process from 'node:process'
2
import readline from 'node:readline'
3
4
function clearScreen() {
5
const repeatCount = process.stdout.rows - 2
6
const blank = repeatCount > 0 ? '\n'.repeat(repeatCount) : ''
7
console.log(blank)
8
readline.cursorTo(process.stdout, 0, 0)
9
readline.clearScreenDown(process.stdout)
10
}

可以用一下代码进行测试:

1
import process from 'node:process'
2
import readline from 'node:readline'
3
4
function clearScreen() {
5
const repeatCount = process.stdout.rows - 2
6
const blank = repeatCount > 0 ? '\n'.repeat(repeatCount) : ''
7
console.log(blank)
8
readline.cursorTo(process.stdout, 0, 0)
9
readline.clearScreenDown(process.stdout)
10
}
11 collapsed lines
11
12
function print() {
13
for (let i = 0; i < 10; i++) {
14
console.log(i)
15
}
16
17
setTimeout(clearScreen, 1000)
18
setTimeout(print, 3000)
19
}
20
21
print()

CD ..
回顾上一篇
Level Logger