diff options
author | davidovski <david@davidovski.xyz> | 2022-03-19 22:19:36 +0000 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2022-03-19 22:19:36 +0000 |
commit | 00abdcdcba8f13b323d60409f33f2eeda3c171ef (patch) | |
tree | d11a6f19742f54c17f8173d7bc0403b1db95cf76 /src | |
parent | 5b5f3e9e87ec29eddad45e4127ac068c0ce292fb (diff) |
added unicode character counting
Diffstat (limited to 'src')
-rw-r--r-- | src/hbar.c | 22 |
1 files changed, 14 insertions, 8 deletions
@@ -8,7 +8,6 @@ #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#include <string.h> #include <stdbool.h> #include <getopt.h> @@ -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(" "); } |