Node.js:防止光标换行

Node.js: Prevent cursor wrapping

本文关键字:光标 换行 js Node      更新时间:2023-09-26

使用process.stdout.write,光标位于输出字符串的末尾
但是,当您在端子的右边框上书写任何内容时,光标将放置在下一行的开头。

如果您希望通过填充整个终端来为应用程序提供背景,这会导致问题,因为它会滚动窗口。

有一种变通方法,在写入右下角的字符槽后,向上滚动一行,但这似乎并不适用于所有平台,即cmd。

从描述中,您似乎在谈论ansi.js,它(正如ANSI转义序列中所指出的,在接受的答案中,不会打印到Windows上的stdout)依赖于libuv之类的库。

该库实现了适量的转义序列。但参考ansi.js的源代码,它使用转义[1

libuv中的一条评论解释了原因:

 * Normally cursor movement in windows is relative to the console screen buffer,
 * e.g. the application is allowed to overwrite the 'history'. This is very
 * inconvenient, it makes absolute cursor movement pretty useless. There is
 * also the concept of 'client rect' which is defined by the actual size of
 * the console window and the scroll position of the screen buffer, but it's
 * very volatile because it changes when the user scrolls.

在curses应用程序中,这类操作是通过在倒数第二列的右下角写入字符来完成的,然后使用插入字符将其放置到位。但ansi.jslibuv都没有实现这一点。