ANSI转义序列:保存和恢复行位置 [英] ANSI escape sequences: save and restore a row position

查看:51
本文介绍了ANSI转义序列:保存和恢复行位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ]
-------------------------------------

然后经过一些处理

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 + [u ,但是只会还原,而不是行.


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.

在某些背景下,这是一个Node.JS程序.我已经简短地查看了 node-ncurses ,但是对于此任务来说似乎有点过头了(?)

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(?)

推荐答案

当您在屏幕的最后一行中打印并且必须向下滚动时,会出现此问题.文本滚动不会更新保存的光标位置.您需要检测到发生了一些滚动(我不知道如何发生),并采取相应的操作,例如发送序列在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.

再见.-

这篇关于ANSI转义序列:保存和恢复行位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆