0

I'm developing some application in which I want to manipulate some data comming from the embedded system. So, what do I want to do is that I want to display the values which are comming on the same position where they were before, leaving the static text on the same place and not using new line. Being more specific, I want to output my data in form of a table, and in this table on the same positions I want to update that data. There is some analogy in Linux, when in the terminal there is some update of the value(let's say some progress) while the static text remains and only the value is changing. So, the output should look like this:

Some_data: 0xFFFF

Some_data2: 0xA1B3

Some_data3: 0x1201

So in this case, "Some_data" remains unchanged on the same place, and only the data itself is updated. Are there maybe some libraries for doing that? What about Windows Console Functions? Also, it would be very nice if it could be made in such a way, in which the console would not flick, like when you clear the console and print something back. Any hints or suggestions? Thanks very much in advance guys!

P.S. There is no need to write the code, I just need some hints or suggestions, with very short examples if possible(but not required).

MrMan
  • 39
  • 1
  • 7

1 Answers1

0

On a *nix system you have two options.

1) If you want to manipulate the entire console in table form like you ask, then ncurses is the best option. The complete reference can be found here.

As you can see, that package is quite heavyweight and can often be overkill for simple projects, so I often use . ..

2) If you can contain your changing information on a single line, use the backspace escape char \b and then rewrite the information repeatedly to that line

For example, try this . . .

#include <iostream>
#include <chrono>
#include <thread>

using namespace std;

void writeStuff(int d)
{
    cout << string(100,'\b') << flush;
    cout << "Thing = " << d;
}

int main()
{
    cout << "AMAZING GIZMO" << "\n============" << endl;
    while(1) {
        writeStuff(rand());
        this_thread::sleep_for(chrono::milliseconds(250));
    }
}

For a real world example, the sox audio console playback command uses this technique to good effect by displaying a bar chart made of console characters to represent the audio playback level in real time.

Of course, you can get more creative with the method shown above if your console supports ANSI escape sequences.

learnvst
  • 15,455
  • 16
  • 74
  • 121
  • Well, I guess that the size of the library is not very important here, because besides the displaying of the values, there is communication part, processing part and so on, and I want the output to be very informative and comfortable, so I don't have to constantly shift up/down the console(I use ConeEMU instead of plain windows console, but anyway...). – MrMan May 04 '17 at 10:05
  • why don't just `cout << "AMAZING GIZMO\n============\n";` instead of printing 3 separate strings? – phuclv May 04 '17 at 10:53
  • ... errr that line is kinda disposable anyway. Format it however you like to read it in your code – learnvst May 04 '17 at 10:57
  • [backspace's behavior varies depending on platform](http://stackoverflow.com/a/6792867/995714). The correct way is to [write `\r` to go to the begin of the line and write the line again](http://stackoverflow.com/q/1508490/995714) – phuclv May 06 '17 at 09:32