mirror of
https://github.com/troglobit/libite.git
synced 2026-10-20 11:09:33 +00:00
progress(): Very simple ASCII progress bar from troglobit/toolbox@f98c5d0
This patch adds a very simple progress bar I wrote once. It's revision f98c5d0 from https://github.com/troglobit/toolbox Also included in this patch is conio.h, a neat little set of macros for manipulating your terminal using ANSI escape seequences. Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
a157fe4162
commit
1909dd475c
3 files changed
+184
-1
No files matched your search
+102
@@ -0,0 +1,102 @@
|
||||
/* Very simple termios based progress bar
|
||||
*
|
||||
* Copyright (c) 2012, 2013 Joachim Nilsson <troglobit@gmail.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <limits.h> /* INT_MAX */
|
||||
#include <stdio.h>
|
||||
|
||||
#include "conio.h"
|
||||
|
||||
static char spinner(void)
|
||||
{
|
||||
static int i = INT_MAX;
|
||||
char *states = "|/-\\";
|
||||
// char *states = ".oOo";
|
||||
// char *states = ".oO°Oo.";
|
||||
// char *states = "v<^>";
|
||||
// char *states = ".oO@*";
|
||||
|
||||
/* Subtract to get clockwise spinning. */
|
||||
return states[i-- % 4]; /* % Number of states */
|
||||
}
|
||||
|
||||
void progress(int percent, int max_width)
|
||||
{
|
||||
int i, bar = percent * max_width / 100;
|
||||
|
||||
if (0 == percent)
|
||||
hidecursor();
|
||||
|
||||
fprintf(stderr, "\r%3d%% %c [", percent, spinner());
|
||||
|
||||
for (i = 0; i < max_width; i++) {
|
||||
if (i > bar)
|
||||
fputc(' ', stderr);
|
||||
else if (i == bar)
|
||||
fputc('>', stderr);
|
||||
else
|
||||
fputc('=', stderr);
|
||||
}
|
||||
|
||||
fprintf(stderr, "]");
|
||||
if (100 == percent) {
|
||||
showcursor();
|
||||
fputc('\n', stderr);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef UNITTEST
|
||||
#include <stdlib.h> /* atexit() */
|
||||
#include <unistd.h> /* usleep() */
|
||||
|
||||
#define MAX_WIDTH 80
|
||||
#define msleep(msec) usleep(msec * 1000)
|
||||
|
||||
static void bye(void)
|
||||
{
|
||||
showcursor();
|
||||
}
|
||||
|
||||
int main(int argc __attribute__((unused)), char const *argv[] __attribute__((unused)))
|
||||
{
|
||||
int i, percent, block = 0, num = 85;
|
||||
|
||||
atexit(bye);
|
||||
hidecursor();
|
||||
|
||||
while (block < num) {
|
||||
percent = block * 100 / num;
|
||||
for (i = 0; i < 10; i++) {
|
||||
progress(percent, MAX_WIDTH);
|
||||
msleep(2);
|
||||
}
|
||||
block++;
|
||||
msleep(50);
|
||||
}
|
||||
progress(100, MAX_WIDTH);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* UNITTEST */
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* compile-command: "gcc -g -I../../include -o unittest -DUNITTEST progress.c; and ./unittest"
|
||||
* version-control: t
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
Reference in new issue
Block a user