diff options
author | Xavier G <xavier.github@kindwolf.org> | 2016-05-13 03:42:56 +0200 |
---|---|---|
committer | Xavier G <xavier.github@kindwolf.org> | 2016-05-13 03:42:56 +0200 |
commit | 97a4ee78fd08e58fce62c6c0b22de94b625c90f6 (patch) | |
tree | 1ee0c404bf5f184da2952ebe6222295c43bdb567 /src/core | |
parent | 09ca3ad48f57b7febd6ad353fb38dbe9234e7e70 (diff) | |
download | irssi-97a4ee78fd08e58fce62c6c0b22de94b625c90f6.zip |
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.
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/special-vars.c | 13 |
1 files 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 |