From f090af0af78491efbbe41ef0b888dc7e7304e7fe Mon Sep 17 00:00:00 2001 From: davidovski Date: Fri, 11 Feb 2022 23:48:29 +0000 Subject: completed hbar --- src/hbar.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/hbar.c (limited to 'src/hbar.c') diff --git a/src/hbar.c b/src/hbar.c new file mode 100644 index 0000000..d0482c0 --- /dev/null +++ b/src/hbar.c @@ -0,0 +1,98 @@ +/* + * hbar + * + * create a horizontal progres bar across the screen + */ + +#include +#include +#include +#include +#include +#include + +#include "colors.h" + +#define DEFAULT_COLOR BLACK BG_WHITE +#define DEFAULT_RESET WHITE BG_BLACK + +int main (int argc, char **argv) { + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + int width = w.ws_col; + + char *text = ""; + char *unit = ""; + int total = 0; + int completed = 0; + + char *color = DEFAULT_COLOR; + char *reset = DEFAULT_RESET; + + int opt; + int option_index = 0; + + const char *optstring = "T:u:c:r:"; + static const struct option opts[] = { + {"text", required_argument, 0, 'T'}, + {"unit", optional_argument, 0, 'u'}, + {"color", optional_argument, 0, 'c'}, + {"reset", optional_argument, 0, 'r'} + }; + + while ((opt = getopt_long(argc, argv, optstring, opts, &option_index)) != -1) { + switch (opt) { + case 'T': + text = optarg; + break; + case 'c': + printf("%s color is \n", optarg); + color = optarg; + break; + case 'r': + reset = optarg; + break; + case 'u': + unit = optarg; + break; + } + } + + + if (argc < optind + 2) { + printf(RESET "\n"); + return 1; + } + + completed = atoi(argv[optind]); + total = atoi(argv[optind+1]); + + char *count = malloc(width); + sprintf(count, "[%d%s/%d%s]", completed, unit, total, unit); + + printf(color); + for (int i = 0; i < width; i++) { + int reset_at = 0; + if (total > 0) { + float percent = (float) completed / (float) total; + reset_at = percent * width; + } + + if (i == reset_at) { + printf(reset); + } + + if (text && i < strlen(text)) { + printf("%c", text[i]); + } else if (i + 1 > width - strlen(count)) { + printf("%c", count[i - width + strlen(count)]); + } else { + printf(" "); + } + } + + printf(RESET "\r"); + + return 0; +} + -- cgit v1.2.1