summaryrefslogtreecommitdiff
path: root/src/hbar.c
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 /src/hbar.c
parent00abdcdcba8f13b323d60409f33f2eeda3c171ef (diff)
proper support for unicode characters
Diffstat (limited to 'src/hbar.c')
-rw-r--r--src/hbar.c24
1 files changed, 20 insertions, 4 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;
}