1import process from 'node:process'2import readline from 'node:readline'3 4function clearScreen() {5 const repeatCount = process.stdout.rows - 26 const blank = repeatCount > 0 ? '\n'.repeat(repeatCount) : ''7 console.log(blank)8 readline.cursorTo(process.stdout, 0, 0)9 readline.clearScreenDown(process.stdout)10}
可以用一下代码进行测试:
1import process from 'node:process'2import readline from 'node:readline'3 4function clearScreen() {5 const repeatCount = process.stdout.rows - 26 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 lines11 12function 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 21print()