Skip to content

Instantly share code, notes, and snippets.

@vicentebolea
Created May 20, 2014 06:26
Show Gist options
  • Select an option

  • Save vicentebolea/abe139800309d3f32e66 to your computer and use it in GitHub Desktop.

Select an option

Save vicentebolea/abe139800309d3f32e66 to your computer and use it in GitHub Desktop.
A simple status bar for terminal using ANSI escape sequences
#include <stdio.h>
#include <unistd.h>
#include <string.h>
void statusbar_to_stdout (const char * item, int percentage) {
int i, pos;
char per_bar [50 + 1] = {'-'};
if (percentage > 100) percentage %= 100;
pos = percentage / 2;
printf ("\e[?25l"); // Hide cursor
printf ("\e[1G%s", item);
memset (per_bar, '-', 50);
memset (per_bar, '#', pos);
per_bar[50] = '\0';
printf ("\e[20G\e[31m%s\e[0m", per_bar);
printf ("\e[100G");
fflush (stdout);
}
int main(int argc, const char *argv[])
{
int i;
for (i = 0; i <= 100; i++) {
usleep (100000);
statusbar_to_stdout ("Downloading File", i);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment