用c++代码清除Linux中的终端

时间:2022-08-31 17:28:46

Okay, I have been researching on how to do this, but say I am running a program that has a whole bit of output on the terminal, how would I clear the screen from within my program so that I can keep my program running?

好吧,我一直在研究如何做到这一点,但是说我正在运行一个在终端上有一点输出的程序,我如何从程序中清除屏幕以便我可以保持程序运行?

I know I can just type clear in terminal and it clears it fine, but like I said, for this program it would be more beneficial for me.

我知道我可以在终端输入清除它清除它很好,但就像我说的,对于这个程序,它对我来说会更有益。

I found something that works, however, I'm not sure what it is or what it is doing.

我找到了一些有效的东西,但是,我不确定它是什么或它在做什么。

cout << "\033[2J\033[1;1H";

That works but I have no clue what it is, if you could explain it, than I would much appreciate it.

这是有效的,但我不知道它是什么,如果你能解释它,我会非常感激。

3 个解决方案

#1


36  

These are ANSI escape codes. The first one (\033[2J) clears the entire screen (J) from top to bottom (2). The second code (\033[1;1H) positions the cursor at row 1, column 1.

这些是ANSI转义码。第一个(\ 033 [2J]从上到下清除整个屏幕(J)(2)。第二个代码(\ 033 [1; 1H)将光标定位在第1行第1列。

All ANSI escapes begin with the sequence ESC[, have zero or more parameters delimited by ;, and end with a command letter (J and H in your case). \033 is the C-style octal sequence for the escape character.

所有ANSI转义都以序列ESC [开头,有零个或多个参数由;分隔,并以命令字母结束(在您的情况下为J和H)。 \ 033是转义字符的C风格八进制序列。

See here for the full roadshow.

在这里查看完整的路演。

#2


9  

Instead of depending on specific escape sequences that may break in unexpected situations (though accepting that trade-off is fine, if it's what you want), you can just do the same thing you'd do at your shell:

而不是依赖于可能在意外情况下中断的特定转义序列(虽然接受权衡很好,如果它是你想要的),你可以做你在shell上做的同样的事情:

std::system("clear");

Though generally system() is to be avoided, for a user-interactive program neither the extra shell parsing nor process overhead is significant. There's no problem with shell escaping either, in this case.

虽然通常要避免使用system(),但对于用户交互式程序来说,额外的shell解析和进程开销都不重要。在这种情况下,shell转义也没有问题。

You could always fork/exec to call clear if you did want to avoid system(). If you're already using [n]curses or another terminal library, use that.

如果你想避免使用system(),你总是可以fork / exec来调用clear。如果您已经在使用[n] curses或其他终端库,请使用它。

#3


0  

For portability you should get the string from termcap's cl (clear) capability (Clear screen and cursor home). (Or use std::system("clear") as told by Roger Pate).

为了便于携带,你应该从termcap的cl(清除)功能中获取字符串(Clear screen和cursor home)。 (或者使用std :: system(“clear”),如Roger Pate所述)。

man 3 termcap (in ncurses)
man 5 termcap
set | grep TERMCAP

man 3 termcap(在ncurses中)man 5 termcap set | grep TERMCAP

#1


36  

These are ANSI escape codes. The first one (\033[2J) clears the entire screen (J) from top to bottom (2). The second code (\033[1;1H) positions the cursor at row 1, column 1.

这些是ANSI转义码。第一个(\ 033 [2J]从上到下清除整个屏幕(J)(2)。第二个代码(\ 033 [1; 1H)将光标定位在第1行第1列。

All ANSI escapes begin with the sequence ESC[, have zero or more parameters delimited by ;, and end with a command letter (J and H in your case). \033 is the C-style octal sequence for the escape character.

所有ANSI转义都以序列ESC [开头,有零个或多个参数由;分隔,并以命令字母结束(在您的情况下为J和H)。 \ 033是转义字符的C风格八进制序列。

See here for the full roadshow.

在这里查看完整的路演。

#2


9  

Instead of depending on specific escape sequences that may break in unexpected situations (though accepting that trade-off is fine, if it's what you want), you can just do the same thing you'd do at your shell:

而不是依赖于可能在意外情况下中断的特定转义序列(虽然接受权衡很好,如果它是你想要的),你可以做你在shell上做的同样的事情:

std::system("clear");

Though generally system() is to be avoided, for a user-interactive program neither the extra shell parsing nor process overhead is significant. There's no problem with shell escaping either, in this case.

虽然通常要避免使用system(),但对于用户交互式程序来说,额外的shell解析和进程开销都不重要。在这种情况下,shell转义也没有问题。

You could always fork/exec to call clear if you did want to avoid system(). If you're already using [n]curses or another terminal library, use that.

如果你想避免使用system(),你总是可以fork / exec来调用clear。如果您已经在使用[n] curses或其他终端库,请使用它。

#3


0  

For portability you should get the string from termcap's cl (clear) capability (Clear screen and cursor home). (Or use std::system("clear") as told by Roger Pate).

为了便于携带,你应该从termcap的cl(清除)功能中获取字符串(Clear screen和cursor home)。 (或者使用std :: system(“clear”),如Roger Pate所述)。

man 3 termcap (in ncurses)
man 5 termcap
set | grep TERMCAP

man 3 termcap(在ncurses中)man 5 termcap set | grep TERMCAP