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.
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.
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?
以上就是Returning the terminal cursor to start-of-line with wrapping enabled的详细内容，更多请关注web前端其它相关文章！