Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

erasing terminal output on linux

I was writing a command line program which will have a status bar, much like wget.

The main problem I'm facing is: how do I delete what I've already sent into stdout/stderr?

I had on idea: use the backspace char '\b' and erase the output I've sent. Is that the best way? Is it the only way? Is there a better way?

PS: I don't want to use anything like ncurses. Plain old C please.

Thanks


EDIT:

Can I also go up and/or down? Example: I have 10 lines of output, I want to change the 3rd line from Doing ABC to ABC: Done. How can I do that?

Also, can anyone post more details about what VT102 characters are? What are its capabilities? Please post good links on this if you have any.

Thanks

like image 581
jrharshath Avatar asked Feb 05 '10 15:02

jrharshath


2 Answers

The basic formatting control characters are backspace (\b), tab (\t), newline (\n), and carriage return (\r). If you need more than that then you can use ANSI X3.64 / ISO/IEC 6429 / ECMA-48 escape sequences; at least the VT100 subset is recognized by most modern terminals and emulators. An advantage of using ncurses is that it will look up the capabilities of your particular terminal and so it will work even if your terminal uses a different set of escape sequences.

like image 162
mark4o Avatar answered Sep 30 '22 18:09

mark4o


You have to remember that as far as the regular stdio routines are concerned, stdout is just a byte stream with no inherent display characteristics; that depends on the target device, which can be anything from a regular VT100-style terminal to a hardcopy terminal to a sheet-fed printer to a plotter to whatever.

IMO, you're far better off using a library like ncurses than trying to hack together your own display management code with VT100 escape codes, even for a relatively simple task like this. I know you want to stick with "plain old C", but this is a task that falls outside the bounds of plain old C.

like image 24
John Bode Avatar answered Sep 30 '22 19:09

John Bode