]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pretty-print: take console glyph width into account when drawing progress bar
authorLennart Poettering <lennart@poettering.net>
Fri, 24 May 2024 11:56:10 +0000 (13:56 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 17 Jun 2024 15:12:11 +0000 (16:12 +0100)
So far this used string length, not character width. Fix that.

src/shared/pretty-print.c

index 470aaf77caad36b6772794756065cf1aaf783c72..8eba052f2c2c63d970363b3e7917410a038e8f53 100644 (file)
@@ -17,6 +17,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "utf8.h"
 
 void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos) {
         char *p = buffer;
@@ -473,8 +474,8 @@ void draw_progress_bar(const char *prefix, double percentage) {
 
         if (!terminal_is_dumb()) {
                 size_t cols = columns();
-                size_t prefix_length = strlen_ptr(prefix);
-                size_t length = cols > prefix_length + 6 ? cols - prefix_length - 6 : 0;
+                size_t prefix_width = utf8_console_width(prefix);
+                size_t length = cols > prefix_width + 6 ? cols - prefix_width - 6 : 0;
 
                 if (length > 5 && percentage >= 0.0 && percentage <= 100.0) {
                         size_t p = (size_t) (length * percentage / 100.0);
@@ -531,7 +532,7 @@ void clear_progress_bar(const char *prefix) {
         fputc('\r', stderr);
 
         if (terminal_is_dumb())
-                fputs(strrepa(" ", strlen_ptr(prefix) + 4), /* 4: %3.0f%% */
+                fputs(strrepa(" ", utf8_console_width(prefix) + 4), /* 4: %3.0f%% */
                       stderr);
         else
                 fputs(ANSI_ERASE_TO_END_OF_LINE, stderr);