I'm having trouble adding progress bar for my calculation tool (written in c). Having a simple progress bar code, I can print progress:
void print_progress(float progress)
{
int barWidth = 70;
int pos = barWidth * progress;
printf("%c",'[');
for (int i = 0; i < barWidth; ++i)
{
if (i < pos) {
printf("%c",'=');
}
else if (i == pos) {
printf("%c",'>');
}
else {
printf("%c",' ');
}
}
printf("] %f%% \r",(progress * 100.0));
fflush(stdout);
}
Though, it prints only a fixed width progress bar. How can I change this to read the screen width and print a full-width progress bar? (Like wget
or apt-get
progress bars)
UPDATE
What I tried so far is to read terminal width using ioctl :
struct winsize max;
ioctl(0, TIOCGWINSZ , &max);
printf ("columns %d\n", max.ws_col); // Always 70
And also I don't want to add a dependency like ncurses
...
UPDATE 2
Final (not working) version:
void loadbar(unsigned int x, unsigned int n, unsigned int max_width) {
struct winsize ws;
ioctl(0, TIOCGWINSZ, &ws);
int barWidth = ws.ws_col;
if(barWidth > max_width) {
barWidth = max_width;
}
float ratio = x/(float)n;
int c = ratio * barWidth;
printf("%s","[");
for (x=0; x<c; x++) {
printf("%s","=");
}
printf("%s",">");
for (x=c+1; x<barWidth; x++) {
printf("%s"," ");
}
printf("] %03.2f%%\r",100.0*ratio);
fflush(stdout);
}