summaryrefslogtreecommitdiff
path: root/src/hbar.c
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2022-03-19 22:19:36 +0000
committerdavidovski <david@davidovski.xyz>2022-03-19 22:19:36 +0000
commit00abdcdcba8f13b323d60409f33f2eeda3c171ef (patch)
treed11a6f19742f54c17f8173d7bc0403b1db95cf76 /src/hbar.c
parent5b5f3e9e87ec29eddad45e4127ac068c0ce292fb (diff)
added unicode character counting
Diffstat (limited to 'src/hbar.c')
-rw-r--r--src/hbar.c22
1 files changed, 14 insertions, 8 deletions
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 <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(" ");
}