Search code examples
clinuxbashwhiptail

whiptail gauge in C


here is a simple bash code to display a gauge in a terminal:

#!/bin/bash
{
for ((i = 0 ; i <= 100 ; i+=5)); do
    sleep 0.1
    echo $i
done
} | whiptail --gauge "Please wait while we are sleeping..." 6 50 0
# you can replace 'whiptail' with 'dialog', it will work.

I want to reproduce the same thing in C. Thus I do:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

// set DIALOG to "dialog" or "whiptail"
#define DIALOG "whiptail"

int main()
{
    FILE* pipe;
    if( (pipe = popen(DIALOG " --gauge 'Loading...' 6 50 0","w") )!=NULL)
    {
        int i;
        for (i=1; i<=100; i++)
        {
            usleep(0.1);
            fprintf(pipe, "%d\n",i);
            fflush(pipe);
        }
        pclose(pipe);
    }
    return 0;
}

But it only work with "dialog", I can't get it to work with "whiptail" :(

Any help ??

Solution as Brad S. explain it, If was just too fast... changing to usleep(100000) did the trick


Solution

  • it works for me if I sleep for a more reasonable period of time....try: usleep(100000);