refreshing things (like progress bars) in the command prompt/terminal

Oct 8, 2008 at 7:30pm
Hello all,
It's been awhile since I last posted and I have a new idea for a program. I am looking to find out how progress bars work in the command promt or terminal. Linux users will know that when you install a program that it needs to download (in the terminal) it'll show you a progress bar like this: [====> ]. My question is this, how do they do it so it's not constantly refreshing everything, but still updating the progress bar? I am planning on making a couple of clocks and maybe even a text-based game if I can find out how this works. Thanks,

enduser000
Oct 9, 2008 at 2:49am
Ummmm maybe they only refresh it when its over every 10% or something...Although I don't know what kinds of functions are available in Linux for those kinds of things, someone else will probably be able to give you a better answer.
Oct 9, 2008 at 4:40am
dunno... The wierd thing is it's accurate when you maximize the terminal too (proportions are correct, covers more ground when it gets bigger). And only in this little bar. Has anyone thought of doing a side-scrolling game using text? like mario but in cmd/terminal?
Oct 9, 2008 at 12:06pm
I don't remember the technical details, but the terminal has to be told when the window is resized. Then the terminal can query the width.

As for your first question, it is done one of two ways: either by using curses or by printing \h (backspace) to move the cursor backwards and rewriting the line.
Oct 9, 2008 at 1:35pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "stdafx.h"
#include <iostream>
#include <time.h>

using namespace std;

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}


int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"[          ]";
	cout<<"\b\b\b\b\b\b\b\b\b\b\b";
	for(int a=1;a<11;a++)
	{
		
		cout<<"=";
		wait(1);
	}
	int b;cin>>b;
	return 0;
}


this program will write a small progress bar like: [== ].
Oct 12, 2008 at 9:08pm
Hey thanks for all your help. I took the progress bar you gave me and added a couple things and learned a couple of things. I never knew the '\b' character could be so useful. Looks like a side scrolling text baised game'll b alotta work though so I'm gonna start out with some real simple games. Thanks again!

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <ctime>
using namespace std;

const int SIZE = 32; // size of bar (including brackets)

void wait(int seconds);

int main ()
{
	double temp=0; // temp finds percent done, percent trunciates the decimal
	int percent=0, var=1000000000;

	cout << "Counting to a billion...  ";

	cout << '[';
	for(int i=SIZE-2; i>0; i--) cout << ' ';
	cout << ']'; // makes this: [   ]
	for(int i=0; i<SIZE-1; i++) cout << '\b';


	for(int a=1;a<SIZE-1;a++) // a is the actual bar length, use to find % done
	{
		temp = (((double)a)/((double)SIZE))*100;
		percent = temp;
		if(a<=SIZE-3){
			for(int i=a-1; i>0; i--) cout << '=';
			cout << '>';
			for(int i=SIZE-a-1; i>0; i--) cout << ' ';
			cout << "] ";
			if(!(percent/10)) cout << "  " << percent << '%'; // add another space if it's a single digit
			else cout << ' ' << percent << '%';

			for(int i=SIZE+5; i>0; i--) cout << '\b';
			//wait(1);
			for(int i=var/SIZE; i>0; i--){}
		}else{
			for(int i=a; i>0; i--) cout << '=';
			cout << "=] 100%";
			for(int i=var/SIZE; i>0; i--){}
		}
	}
	return 0;
}

void wait(int seconds)
{
  clock_t endwait;
  endwait = clock() + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait){}
}


enduser000
Topic archived. No new replies allowed.