LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Rewriting multiple lines in C++ terminal application (https://www.linuxquestions.org/questions/programming-9/rewriting-multiple-lines-in-c-terminal-application-542756/)

bandwidthjunkie 04-02-2007 04:27 PM

Rewriting multiple lines in C++ terminal application
 
I have a fairly lengthy C++ terminal application that is doing some heavy maths. Ideally I would like to be able to get a short summary on the screen of what it is currently doing, without it scrolling down a million lines when I run it.

I know how to rewrite one line:
Code:

cout << "\r";
But, is there an "easy" way to clear the last few lines and then rewrite them? Or is this just devil talk?

This only has to run on Linux, would be nice if it could work off the command line (ie not only a terminal window / don't know if there is a difference).

tuxdev 04-02-2007 05:00 PM

Try ncurses. Although this feels like normal log-like behaviour seems like it would work if you were smarter about what and when you logged.

osor 04-02-2007 05:01 PM

There is no portable way to do this using only standard terminal i/o functions. If you really need it, I suggest you try using (n)curses.

bandwidthjunkie 04-03-2007 04:27 AM

Quote:

Originally Posted by tuxdev
Try ncurses. Although this feels like normal log-like behaviour seems like it would work if you were smarter about what and when you logged.

Possibly, but it would take a lot more time to be clever about it and I have spent long enough on the code already.

osor << portability is not really too much of an issue, at the moment there are only about 10 machines in the world that I will run this code on. That is fairly unlikely to change. Will give ncurses a look.

[*UPDATE*] Read the ncurses howto and it looks like just what I need. Thanks for the advice.

Siiiiiii 04-06-2007 09:34 PM

Alternatively, I found this:
http://66.102.9.104/search?q=cache:D...ient=firefox-a
which is a list of all escape sequences for the Linux terminal (or specifically all VT100 terminals). Unfortunately, the original link seems to be broken; I hope you are fine with Google's cache.

So basically, to clear the previous two lines, you would do something like this(in C):

Code:

#include <stdio.h>
#define NUM_LINES 2

int main(){
  char terminal_clearline [4];
  char terminal_moveup [4];

  sprintf(terminal_clearline, "%c[2K", 0x1B);
  sprintf(terminal_moveup, "%c[1A", 0x1B);

  for (int i = 0; i < NUM_LINES; i++){
    printf("%s", terminal_moveup);
    printf("%s", terminal_clearline);
  }
}

I tried to do this in C++, using iostream, but it doesn't seem to work. Perhaps I concatenated the terminal* strings incorrectly. I hope this helps you anyway!

This method is more easy on your computer than using curses (which I assume you'll prefer, calculating millions of lines of math).

/Siiiiiii


All times are GMT -5. The time now is 12:40 AM.