简体   繁体   English

使用控制台字符擦除多个控制台行

[英]Erase multiple console lines with console characters

So I'm aware that with console characters you can return the cursor to the beginning of the current line ( \\r ) or just a single character ( \\b ) for overwriting. 所以我知道使用控制台字符可以将光标返回到当前行的开头( \\r ),或者只返回一个字符( \\b )进行覆盖。 What I would like to know is I overwrite multiple lines with control characters. 我想知道的是我用控制字符覆盖多行。

I have a grid of dots, printed to the console: 我有一个点网格,打印到控制台:

. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .

And then go back to the first character and redraw it 然后回到第一个字符并重新绘制它

. . . . . . . .
. . . 0 . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .

Not sure which language this will be in, that's why I'd prefer the solution to use characters. 不确定这将使用哪种语言,这就是为什么我更喜欢使用字符的解决方案。

(All the below only applies if you do not render the output yourself in a Swing JPanel or similar) (以下所有内容仅适用于您未在Swing JPanel或类似内容中自行渲染输出的情况)

Note that the emulation level you can expect from a console output unit is very limited. 请注意,您可以从控制台输出单元获得的仿真级别非常有限。 Typically you can expect Ctrl-H to backspace, and Ctrl-M to return the cursor to the beginning of the line. 通常,您可以期望Ctrl-H退格,而Ctrl-M则将光标返回到行的开头。 Anything more than that sets additional requirements. 除此以外的任何东西都会产生额外的要求

Historically this has been done by sending ANSI escape sequences (a close relative of the physical VT100 terminal) which was then interpreted to move the cursor, clear parts of the monitor and switch colors. 从历史上看,这是通过发送ANSI转义序列(物理VT100终端的近亲)来完成的,然后将其解释为移动光标,清除监视器的部分并切换颜色。 You will need something similar to do this in the output device. 在输出设备中,您将需要类似的功能。 For CMD.EXE in Windows see How to make win32 console recognize ANSI/VT100 escape sequences? 对于Windows中的CMD.EXE,请参阅如何使win32控制台识别ANSI / VT100转义序列? for suggestions. 建议。 Linux and OS X terminal emulators support this. Linux和OS X终端仿真器支持此功能。

You can then either generate the escape sequences yourself - which is relatively easy for simple needs - or delegate to a curses/ncurses library, to achieve what you need. 然后,您可以自己生成转义序列 - 这对于简单的需求来说相对容易 - 或者委托给curses / ncurses库,以实现您的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM