ANSI 转义序列:保存和恢复行位置

发布于 2024-12-05 03:51:23 字数 926 浏览 2 评论 0原文

我正在编写一个非常简单的小控制台应用程序,并且正在使用一些 ANSI 转义序列 以获得更好的输出。

我想做的就是这样的事情。有一个带有名称的标题,然后在程序运行时,在下面打印几行。当打印每一行时,我想用进度表更新标题行。例如:

My header row                 [ 0/5 ]
-------------------------------------

然后经过一些处理

My header row                 [ 1/5 ]
-------------------------------------
here is some output

...

My header row                 [ 2/5 ]
-------------------------------------
here is some output
the output could
be over several
lines

我尝试使用保存光标位置代码 (ESC + [s),然后使用 ESC< 恢复该位置/kbd> + [u,但是这只恢复,而不恢复行。

对于一些背景知识,这是一个 Node.JS 程序。我简单地查看了 node-ncurses ,但是对于这项任务来说似乎有点矫枉过正(?)

I'm writing a very simple little console application and I'm playing around with some ANSI escape sequences to get some nicer output.

What I'm trying to do is something like this. There's a header with a name, and then underneath, as the program runs, several lines are printed. As each line is printed, I'd like to update the header row with a progress meter. eg:

My header row                 [ 0/5 ]
-------------------------------------

then after some processing

My header row                 [ 1/5 ]
-------------------------------------
here is some output

...

My header row                 [ 2/5 ]
-------------------------------------
here is some output
the output could
be over several
lines

I've tried using the save cursor position code (ESC + [s) and then restoring that position with ESC + [u, however that only restores the column, not the row.

for some background, this is a Node.JS program. I've looked at node-ncurses briefly, however it seems like a bit of overkill for this task(?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行雁书 2024-12-12 03:51:23

这是在屏幕最后一行打印并且必须向下滚动时出现的问题。文本的滚动不会更新已保存的光标位置。您需要检测到发生了一些滚动(我不知道如何)并采取相应的操作,例如发送序列,在 Esc[u 序列之后向上移动光标,以便转到原始位置。

再见。-

This is a problem arising when you are printing in the last row of the screen and it has to scroll down. The scroll of the text do not update the saved cursor position. You need to detect that some scroll happend (I don't know how) and take the corresponding action like send sequences moving the cursor up after the Esc[u sequence in order to go to the original position.

Bye.-

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文