How to update a printed message in terminal without reprinting

Viewed 24226

I want to make a progress bar for my terminal application that would work something like:

 [XXXXXXX       ] 

which would give a visual indication of how much time there is left before the process completes.

I know I can do something like printing more and more X's by adding them to the string and then simply printf, but that would look like:

 [XXXXXXX       ] 
 [XXXXXXXX      ] 
 [XXXXXXXXX     ] 
 [XXXXXXXXXX    ] 

or something like that (obviously you can play with the spacing.) But this is not visually aesthetic. Is there a way to update the printed text in a terminal with new text without reprinting? This is all under linux, c++.

8 Answers

I've written this loading bar utility some time ago. Might be useful...

https://github.com/BlaDrzz/CppUtility/tree/master/LoadingBar

You can customise basically anything in here.

int max = 1000;
LoadingBar* lb = new LoadingBar(10, 0, max);

for (size_t i = 0; i <= max; i++)
{
    lb->print();
    lb->iterate();
}
cout << lb->toString() << endl;

Very simple and customisable implementation..

Related