返回终端光标开始的行与环绕启用 [英] Returning the terminal cursor to start-of-line with wrapping enabled

查看:87
本文介绍了返回终端光标开始的行与环绕启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个过滤器(在发往终端输出管道),有时需要覆盖刚刚发生的一条线。其工作原理是通过标准输入到标准输出字符由字符,直到 \\ n为达到,然后调用特殊的行为。我的问题关于如何返回到该行的开头。

I'm writing a filter (in a pipe destined for a terminal output) that sometimes needs to "overwrite" a line that has just occurred. It works by passing stdin to stdout character-by-character until a \n is reached, and then invoking special behaviour. My problem regards how to return to the beginning of the line.

我想到的第一件事情是使用 \\ r 或ANSI序列 \\ 033 [1G 。但是,如果行得足够长,已经包裹在终端上(因此导致它滚动),这些只会将光标移动回当前的物理的行。

The first thing I thought of was using a \r or the ANSI sequence \033[1G. However, if the line was long enough to have wrapped on the terminal (and hence caused it to scroll), these will only move the cursor back to the current physical line.

我的第二个想法是跟踪线(因为previous 通过\\ n 字符数)的长度,然后回声 \\ b 许多倍。然而,出了问题,如果该行包含控制字符或转义序列(以及可能的Uni code?)。

My second idea was to track the length of the line (number of characters passed since previous \n), and then echo \b that many times. However, that goes wrong if the line contained control characters or escape sequences (and possibly Unicode?).

搜索所有特殊序列,并以此来调整我的字符数短,有一个简单的方法来实现这一目标?

Short of searching for all special sequences and using this to adjust my character count, is there a simple way to achieve this?

推荐答案

即使有一个神奇的顺序,当写入到一个控制台将可靠地清除最后写入行,你仍然会得到线和序列输出(虽然隐藏在控制台上)。想想如果有人写了输出到文件,或者传递下来的管道,其他过滤器,会发生什么?他们知道如何处理这样的输入?不要告诉我你排除写作不是直接别处控制台的可能性。迟早有人会想重定向输出 - !甚至您

Even if there were a "magic sequence" that when written to a console would reliably erase the last written line, you would STILL get the line and the sequence on the output (though hidden on a console). Think what would happen if somebody wrote the output to a file, or passed it down the pipe to other filters? Would they know how to handle such input? And don't tell me you rule out the possibility of writing somewhere else than directly to a console. Sooner or later, somebody WILL want to redirect the output - maybe even you!

正确的方式做,这是缓冲每条线在内存中,因为它被处理后,再决定是否输出与否。真的没有办法解决这个问题。

The Right Way to do this is to buffer each line in memory as it is processed, and then decide whether to output it or not. There's really no way around this.

这篇关于返回终端光标开始的行与环绕启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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