LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ Output question (https://www.linuxquestions.org/questions/programming-9/c-output-question-591835/)

vibrokatana 10-14-2007 11:04 PM

C++ Output question
 
I always wondered how programs like wget and top create interfaces that can be updated without throwing out new lines of output.

I managed to dig out the function used to update the progress bar in wget:
Code:

static void display_image (char *buf)
{
  int old = log_set_save_context (0);
  logputs (LOG_VERBOSE, "\r");
  logputs (LOG_VERBOSE, buf);
  log_set_save_context (old);
}

My question is: how can you do the equivilent using strictly c++ stdlib or a library of some sort (I want to avoid using C and C++ outputs concurrently to avoid the performance issues).

Basically what I am trying to do is write a simple command line that may have text added above well after the prompt has been written to the terminal emulator. So far I have hit a pretty bad wall and have run out of ideas.

Any and all suggestions are welcome.

PatrickNew 10-15-2007 12:03 AM

While I'm not certain, I think they are using the terminfo library to do this, which is what ncurses is built on top of. Unfortunately, that's a pure C library and I think if you use it, you need to do so exclusively (I think). That is, if you use the terminfo/curses way of printing to the screen, you shouldn't mix in printf()'s or cout<<'s.

vibrokatana 10-15-2007 12:15 AM

thx for the info. There seems to be C++ bindings for ncurses so I may look at using that. Nifty, if a bit more complicated then what I need :\

ntubski 10-15-2007 10:26 AM

I think the wget function only changes stuff on the current line. If that's all you want, printing a carriage return ('\r') character will send the cursor back to the beginning of the line in most terminals. If you want to go above the current line you'll need ncurses or terminfo.

vibrokatana 10-15-2007 10:34 AM

hmm, interesting bit. I may end up having to go with a ncurses type system. Does anyone know anything about terminfo, all the information I have looked up has basically described it just as a database explaining the capabilities of the terminal.

I wonder how hard it would be to derive something from cout or an existing output stream and see how hard it would be to write my own. I might beable to cache the existing text, erase it, output the new text, then place the text back. I guess I have some stuff to test.

PatrickNew 10-15-2007 11:47 AM

Ntubski is right. Upon rereading the code snippet they are just using a carriage return. If one line isn't enough, I'd go with ncurses rather than try to use any raw terminfo, etc. It's just simpler.

vibrokatana 10-15-2007 11:56 AM

doing a simple program in C++ you can't get the carriage return from what I can guess:

Code:

int main(int argc, char *argv[])
{
  cout << "Hello, world!" << endl;
  cout << endl;
  cout << "Hello again!" << endl;
  return EXIT_SUCCESS;
}

That will output as:
Code:

Hello, world!

Hello again!

So I would probably need to use c style outputs to interface that way.


I will probably put this project on a GUI, probably end up being easier and more robust to use without having to deal with abstract libraries or methods.

PatrickNew 10-15-2007 12:09 PM

Well, the trouble is that you never tried using a carriage return. 'endl' translates to '\n' not '\r'. I'd recommend using '\r' explicitly.

vibrokatana 10-15-2007 02:15 PM

hmm, weird behavior:
Code:

int main(int argc, char *argv[])
{
  cout << "Hello, world!" << '\r';
  cout << "Hello again" << endl;
  return EXIT_SUCCESS;
}

Code:

Hello againd!
Fun, I guess another work around is in order.

I will work on this later tonight, this should be easily solvable with the .size and .resize functions in the string class.

Code:

#include <iostream>
#include <string>

using namespace std;

string OverWriteFormat(string LastLine, string NewLine)
{
  int NewSize = NewLine.size();
  int LastSize = LastLine.size();

  if(NewSize < LastSize)
    NewLine.resize(LastSize, ' ');
  return NewLine;
}

int main(int argc, char *argv[])
{
  string LastString = "OMG WTF BBQ!";
  cout << LastString;
  cout << '\r' << OverWriteFormat(LastString, "OMG") << endl;
  cout << LastString << endl;
  return EXIT_SUCCESS;
}

That works fine, now all I have to worry about is how it will behave with an input :\

graemef 10-15-2007 05:27 PM

Outputing a string that is always the same length, adding blanks before the \r should help...


All times are GMT -5. The time now is 07:47 AM.