Created
May 20, 2014 06:26
-
-
Save vicentebolea/abe139800309d3f32e66 to your computer and use it in GitHub Desktop.
A simple status bar for terminal using ANSI escape sequences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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