Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Command Line Progress Bar (C/C++ and/or Java)

This thread is locked; no one can reply to it. rss feed Print
Command Line Progress Bar (C/C++ and/or Java)
bamccaig
Member #7,536
July 2006
avatar

I am looking for an explanation/instructions on how some command-line applications show progress bars. For example,

prompt>./dosomething

	Doing something... Progress: [===========         ] 55%

Does anybody know how this is done? I am looking for a solution that doesn't require clearing the screen with 'clear' or 'cls', etc. For example, the Linux update utility 'yum' shows similar progress bars on the command-line. You can still scroll up through previous output so clearly it is possible, at least in some cases, to achieve this without any crazy clearing of screens.

I'm quite confident that it is possible with C/C++ in at least Windows and Linux (which I assume implies most Unix-like systems as well), although I don't know how or which libraries would achieve this.

My instincts tell me it would be shell and operating system dependent, but I don't want excuses. I want results! ;)

As implied in the title of this thread, I am also interested in doing this with Java. Does anybody know of packages that can achieve this on the command-line?

Your comments are appreciated.

Steve Terry
Member #1,989
March 2002
avatar

I think you can use a special escape character, like /r to move the cursor to the beginning of the line.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Matthew Dalrymple
Member #7,922
October 2006
avatar

If you want complete control of the console window (changing colors, mouse support, cursor control)
Check out this and the related functions:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/writeconsole.asp
SetConsoleCursorPosition()
etc...

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

Steve Terry
Member #1,989
March 2002
avatar

1#include <stdio.h>
2#include <stdlib.h>
3#include <windows.h>
4#include <winbase.h>
5 
6int main(int argc, char **argv)
7{
8 char buffer[256] = { 0 };
9 char percent[7] = "0.0%%";
10 int i, j, k;
11 buffer[0] = '[';
12 for(i = 0; i < 40; i++)
13 {
14 buffer[i + 1] = '=';
15 j = i % 4;
16 if(j == 0)
17 buffer[i + 2] = '\\';
18 else if(j == 1)
19 buffer[i + 2] = '|';
20 else if(j == 2)
21 buffer[i + 2] = '/';
22 else
23 buffer[i + 2] = '-';
24
25 for(k = i + 3; k < 41; k++)
26 buffer[k] = ' ';
27 
28 buffer[41] = ']';
29 
30 sprintf(percent, "%3.2f%%", (i / 40.0) * 100.0);
31 
32 printf("%s%s\r", buffer, percent);
33
34 Sleep(200);
35 }
36 
37 sprintf(percent, "%3.2f%%", (i / 40.0) * 100.0);
38 
39 printf("%s%s\r", buffer, percent);
40 
41 return 0;
42}

I was bored ;D

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

bamccaig
Member #7,536
July 2006
avatar

Thank you both! The \r is working and I also discovered that generating the string before printing it speeds up the line and makes the caret appear only at the end of the line flashing, as opposed to flashing inside of the progress bar ugly-ly. :)

I also appreciate the article about controlling the command line. I will definitely look at that!

Thanks again! 8-)

Goalie Ca
Member #2,579
July 2002
avatar

If you want more control over it i suggest looking into ncurses.

-------------
Bah weep granah weep nini bong!

Go to: