summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2022-03-19 23:04:41 +0000
committerdavidovski <david@davidovski.xyz>2022-03-19 23:04:41 +0000
commit4c72e89f2ecb457a212b001bbaabd4c35ff77f82 (patch)
treeb4336903bb8abf26ce8f924fd93dab5109d7d513
parent00abdcdcba8f13b323d60409f33f2eeda3c171ef (diff)
proper support for unicode characters
-rw-r--r--src/hbar.c24
-rwxr-xr-xtest/hbar.sh9
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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <unistr.h>
#include <stdbool.h>
+#include <string.h>
#include <getopt.h>
#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,12 +1,19 @@
#!/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
done
${HBAR} -t -T "${TEXT}" -u ${UNIT} $x $MAX