diff options
author | davidovski <david@davidovski.xyz> | 2022-02-11 23:48:29 +0000 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2022-02-11 23:48:29 +0000 |
commit | f090af0af78491efbbe41ef0b888dc7e7304e7fe (patch) | |
tree | a89684e76086d24a2b8603e4a51fce7c747411cd | |
parent | 33e775af4521a4861f3e50c6e11faf754a708824 (diff) |
completed hbar
-rwxr-xr-x | Makefile | 25 | ||||
-rw-r--r-- | src/colors.h | 48 | ||||
-rw-r--r-- | src/hbar.c | 98 | ||||
-rw-r--r-- | src/hbar/hbar.c | 50 | ||||
-rwxr-xr-x | test/hbar.sh | 13 |
5 files changed, 184 insertions, 50 deletions
diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..5be29ab --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +CC=gcc +FLAGS= + +DESTDIR= +PREFIX=/usr + + +.DEFAULT_GOAL := build-hbar + +install: install-hbar install-headers +build: build-hbar + +install-headers: src/*.h + install -m644 src/*.h ${DESTDIR}${PREFIX}/include + +install-hbar: bin/hbar + install -m755 bin/hbar ${DESTDIR}${PREFIX}/bin + +build-hbar: src/hbar.c + ${CC} src/hbar.c -o bin/hbar ${FLAGS} + +prepare: + rm -rf bin + mkdir -pv bin + diff --git a/src/colors.h b/src/colors.h new file mode 100644 index 0000000..627fa67 --- /dev/null +++ b/src/colors.h @@ -0,0 +1,48 @@ +/* + * Color values + * + * provided by xiutils + */ +#define RESET "\033[0m" + +#define BOLD "\033[0;1m" + +#define BLACK "\033[0;30m" +#define RED "\033[0;31m" +#define GREEN "\033[0;32m" +#define YELLOW "\033[0;33m" +#define BLUE "\033[0;34m" +#define MAGENTA "\033[0;35m" +#define CYAN "\033[0;36m" +#define WHITE "\033[0;37m" +#define DEFAULT "\033[0;39m" + +#define BG_BLACK "\033[40m" +#define BG_RED "\033[41m" +#define BG_GREEN "\033[42m" +#define BG_YELLOW "\033[43m" +#define BG_BLUE "\033[44m" +#define BG_MAGENTA "\033[45m" +#define BG_CYAN "\033[46m" +#define BG_WHITE "\033[47m" +#define BG_DEFAULT "\033[49m" + +#define LIGHT_BLACK "\033[0;90m" +#define LIGHT_RED "\033[0;91m" +#define LIGHT_GREEN "\033[0;92m" +#define LIGHT_YELLOW "\033[0;93m" +#define LIGHT_BLUE "\033[0;94m" +#define LIGHT_MAGENTA "\033[0;95m" +#define LIGHT_CYAN "\033[0;96m" +#define LIGHT_WHITE "\033[0;97m" +#define LIGHT_DEFAULT "\033[0;99m" + +#define BOLDBLACK "\033[1;30m" +#define BOLDRED "\033[1;31m" +#define BOLDGREEN "\033[1;32m" +#define BOLDYELLOW "\033[1;33m" +#define BOLDBLUE "\033[1;34m" +#define BOLDMAGENTA "\033[1;35m" +#define BOLDCYAN "\033[1;36m" +#define BOLDWHITE "\033[1;37m" +#define GREY46 "\033[38;5;243m" 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 <sys/ioctl.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <getopt.h> + +#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; +} + diff --git a/src/hbar/hbar.c b/src/hbar/hbar.c deleted file mode 100644 index 5c90349..0000000 --- a/src/hbar/hbar.c +++ /dev/null @@ -1,50 +0,0 @@ -/* - * hbar - * - * create a horizontal progres bar across the screen - */ - -#include <sys/ioctl.h> -#include <stdio.h> -#include <unistd.h> -#include <getopt.h> - -int main (int argc, char **argv) { - struct winsize w; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - int width = w.ws_col; - - char *text; - int total; - int completed; - - int opt; - int option_index = 0; - - const char *optstring = "T:ucr:"; - 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; - } - } - - if (argc < optind + 2) { - fprintf(stderr, "Not enough arguments provided"); - } - - completed = atoi(argv[optind]); - total = atoi(argv[optind+1]); - - char output[width+6] = '\0'; - - return 0; -} - diff --git a/test/hbar.sh b/test/hbar.sh new file mode 100755 index 0000000..d40c2ad --- /dev/null +++ b/test/hbar.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +HBAR=./src/hbar/hbar +TEXT="Hello there" +UNIT="mb" +MAX=100 + +for x in $(seq $MAX); do + ${HBAR} -T "${TEXT}" -u ${UNIT} $x $MAX + sleep 0.01 +done + +${HBAR} |