From 4c72e89f2ecb457a212b001bbaabd4c35ff77f82 Mon Sep 17 00:00:00 2001 From: davidovski Date: Sat, 19 Mar 2022 23:04:41 +0000 Subject: proper support for unicode characters --- src/hbar.c | 24 ++++++++++++++++++++---- test/hbar.sh | 9 ++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/hbar.c b/src/hbar.c index 2b55ed6..c0272b9 100644 --- a/src/hbar.c +++ b/src/hbar.c @@ -8,7 +8,9 @@ #include #include #include +#include #include +#include #include #include "colors.h" @@ -37,12 +39,27 @@ void human_format(int bytes, char *output) { size_t len(char *s) { size_t len = 0; for (; *s; s++) { - if ((*s & 0XC0) != 0x80) + if ((*s & 0XC0) != 0x80) { len++; + } } return len; } +void printat(char *s, size_t index) { + index++; + for (; *s; s++) { + if ((*s & 0XC0) != 0x80) { + index--; + } + if (index <= 0) { + fwrite(s, 1, 1, stdout); + if ((*s & 0XC0) == 0x80) + return; + } + } +} + int main (int argc, char **argv) { struct winsize w; ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); @@ -136,9 +153,9 @@ int main (int argc, char **argv) { } if (text && i < len(text)) { - printf("%c", text[i]); + printat(text, i); } else if (i + 1 > width - len(count)) { - printf("%c", count[i - width + len(count)]); + printat(count, i - width + len(count)); } else { printf(" "); } @@ -151,7 +168,6 @@ int main (int argc, char **argv) { printf(RESET "\n"); } - return 0; } diff --git a/test/hbar.sh b/test/hbar.sh index 9daf606..8d47595 100755 --- a/test/hbar.sh +++ b/test/hbar.sh @@ -1,10 +1,17 @@ #!/bin/sh HBAR=./bin/hbar -TEXT="Hello there" +#TEXT="Hello there" +TEXT="Привет мир" UNIT="mb" MAX=100 +for x in $(seq $MAX); do + ${HBAR} -T "${TEXT}" -u ${UNIT} $x $MAX + read +done +${HBAR} -t -T "${TEXT}" -u ${UNIT} $x $MAX + for x in $(seq $MAX); do ${HBAR} -T "${TEXT}" -u ${UNIT} $x $MAX sleep 0.01 -- cgit v1.2.1