From 00abdcdcba8f13b323d60409f33f2eeda3c171ef Mon Sep 17 00:00:00 2001 From: davidovski Date: Sat, 19 Mar 2022 22:19:36 +0000 Subject: added unicode character counting --- Makefile | 2 +- src/hbar.c | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 6fa1eee..882e481 100755 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ install-glyphs: src/glyphs.sh check-parseconf: install-shtests test/parseconf.sh ${DESTDIR}${PREFIX}/bin/shtests ./test/parseconf.sh -build-hbar: src/hbar.c install-colors +build-hbar: src/hbar.c mkdir -p bin ${CC} src/hbar.c -o bin/hbar ${FLAGS} diff --git a/src/hbar.c b/src/hbar.c index 64e91b7..2b55ed6 100644 --- a/src/hbar.c +++ b/src/hbar.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -20,7 +19,7 @@ #define SAVE_POS "\033[s" #define LOAD_POS "\033[u" -static void human_format(int bytes, char *output) { +void human_format(int bytes, char *output) { char *suffix[] = {"B", "KB", "MB", "GB", "TB"}; char length = sizeof(suffix) / sizeof(suffix[0]); @@ -35,6 +34,15 @@ static void human_format(int bytes, char *output) { sprintf(output, "%.0lf%s", dblBytes, suffix[i]); } +size_t len(char *s) { + size_t len = 0; + for (; *s; s++) { + if ((*s & 0XC0) != 0x80) + len++; + } + return len; +} + int main (int argc, char **argv) { struct winsize w; ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); @@ -100,13 +108,11 @@ int main (int argc, char **argv) { completed = atoi(argv[optind]); total = atoi(argv[optind+1]); - if (line != -1) { + if (line != -1) printf("\033[%dA", line, 0); - } char *count = malloc(width); if (human) { - // i suck at these char *c = malloc(width); char *t = malloc(width); human_format(completed, c); @@ -129,10 +135,10 @@ int main (int argc, char **argv) { printf(reset); } - if (text && i < strlen(text)) { + if (text && i < len(text)) { printf("%c", text[i]); - } else if (i + 1 > width - strlen(count)) { - printf("%c", count[i - width + strlen(count)]); + } else if (i + 1 > width - len(count)) { + printf("%c", count[i - width + len(count)]); } else { printf(" "); } -- cgit v1.2.1