From 172a239edb25e3d489ee5d5e2869ce9dba1fa716 Mon Sep 17 00:00:00 2001 From: Utkarsh Verma Date: Thu, 30 Dec 2021 10:44:06 +0530 Subject: [PATCH] Handle trimming of unicode characters properly --- config.h | 12 ++++++------ main.c | 7 ++++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/config.h b/config.h index 40ce7e7..e45f2e7 100644 --- a/config.h +++ b/config.h @@ -1,16 +1,16 @@ -#define CMDLENGTH 50 -#define DELIMITER "<" -#define LEADING_DELIMITER +#define CMDLENGTH 60 +#define DELIMITER " " #define CLICKABLE_BLOCKS const Block blocks[] = { BLOCK("sb-mail", 1800, 17) BLOCK("sb-music", 0, 18) BLOCK("sb-disk", 1800, 19) - BLOCK("sb-memory", 1800, 20) - BLOCK("sb-loadavg", 1800, 21) + BLOCK("sb-memory", 10, 20) + BLOCK("sb-loadavg", 5, 21) + BLOCK("sb-mic", 0, 26) BLOCK("sb-volume", 0, 22) BLOCK("sb-battery", 5, 23) BLOCK("sb-date", 1, 24) - BLOCK("sb-network", 5, 25) + // BLOCK("sb-network", 5, 25) }; diff --git a/main.c b/main.c index 3a17406..15f6f34 100644 --- a/main.c +++ b/main.c @@ -94,7 +94,12 @@ void updateBlock(int i) { char* output = outputs[i]; char buffer[LEN(outputs[0])]; int bytesRead = read(pipes[i][0], buffer, LEN(buffer)); - buffer[bytesRead - 1] = '\0'; + + // Trim UTF-8 characters properly + int j = bytesRead - 1; + while ((buffer[j] & 0b11000000) == 0x80) + j--; + buffer[j] = '\0'; if (bytesRead == LEN(buffer)) { // Clear the pipe