From 97a4ee78fd08e58fce62c6c0b22de94b625c90f6 Mon Sep 17 00:00:00 2001 From: Xavier G Date: Fri, 13 May 2016 03:42:56 +0200 Subject: get_alignment: handle UTF-8 strings. get_alignment now works with columns (width), not bytes, although it is liable to work with bytes if the given text is not a valid UTF-8 string. --- src/core/special-vars.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/core/special-vars.c b/src/core/special-vars.c index d6794d96..64011b8e 100644 --- a/src/core/special-vars.c +++ b/src/core/special-vars.c @@ -25,6 +25,7 @@ #include "settings.h" #include "servers.h" #include "misc.h" +#include "utf8.h" #define ALIGN_RIGHT 0x01 #define ALIGN_CUT 0x02 @@ -320,18 +321,24 @@ static char *get_alignment(const char *text, int align, int flags, char pad) { GString *str; char *ret; + int policy; + unsigned int cut_bytes; g_return_val_if_fail(text != NULL, NULL); + policy = string_policy(text); + str = g_string_new(text); /* cut */ - if ((flags & ALIGN_CUT) && align > 0 && str->len > align) - g_string_truncate(str, align); + if ((flags & ALIGN_CUT) && align > 0 && string_width(text, policy) > align) { + string_chars_for_width(text, policy, align, &cut_bytes); + g_string_truncate(str, cut_bytes); + } /* add pad characters */ if (flags & ALIGN_PAD) { - while (str->len < align) { + while (string_width(str->str, policy) < align) { if (flags & ALIGN_RIGHT) g_string_prepend_c(str, pad); else -- cgit v1.2.3