45

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++.

jww
  • 97,681
  • 90
  • 411
  • 885
ldog
  • 11,707
  • 10
  • 54
  • 70

8 Answers8

51

try using \r instead of \n when printing the new "version".

for(int i=0;i<=100;++i) printf("\r[%3d%%]",i);
printf("\n");
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
13

I'd say that a library like ncurses would be used to such things. curses helps move the cursor around the screen and draw text and such.

NCurses

KFro
  • 764
  • 3
  • 8
8

Something like this:

std::stringstream out;
for (int i = 0; i< 10; i++)
{
  out << "X";
  cout << "\r" << "[" << out.str() << "]";
}

The sneaky bit is the carriage return character "\r" which causes the cursor to move to the start of the line without going down to the next line.

1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
5

Others have already pointed out that you can use \r to go back to the beginning of the current line, and overwrite the entire line.

Another possibility is to use the backspace character ("\b") to erase a few spaces, and overwrite only those spaces. This can have a couple of advantages. First, it obviously avoids having to regenerate everything in the line, which can sometimes be mildly painful (though that is fairly unusual). Second, it can avoid some pain in displaying data that (for one example) shrinks in size as you write it -- for example, if you're displaying a count-down from 100 to 0, with \r you have to be careful about overwriting the entire previous length, or your countdown will go from (for example) 100 to 990 (i.e., leaving the previous "0" intact).

Note, however, that while back-space within a line normally works, a backspace at the beginning of a line may or may not move the cursor/write position back to a previous line. For most practical purposes, you can only move around within a single line.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

Another option is to simply print one character at a time. Typically, stdout is line buffered, so you'll need to call fflush(stdout) --

for(int i = 0; i < 50; ++i) {
   putchar('X'); fflush(stdout);
   /* do some stuff here */
}
putchar('\n');

But this doesn't have the nice terminating "]" that indicates completion.

NVRAM
  • 6,947
  • 10
  • 41
  • 44
1

'\r' will perform a carriage return. Imagine a printer doing a carriage return without a linefeed ('\n'). This will return the writing point back to the start of the line... then reprint your updated status on top of the original line.

kjfletch
  • 5,394
  • 3
  • 32
  • 38
1

It's a different language, but this question might be of assistance to you. Basically, the escape character \r (carriage Return, as opposed to \n Newline) moves you back to the beginning of your current printed line so you can overwrite what you've already printed.

Community
  • 1
  • 1
Tim
  • 59,527
  • 19
  • 156
  • 165
1

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..

Jannes D.
  • 65
  • 6