1
0
mirror of https://github.com/irssi/irssi.git synced 2025-02-02 15:08:01 -05:00

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.
This commit is contained in:
Xavier G 2016-05-13 03:42:56 +02:00
parent 09ca3ad48f
commit 97a4ee78fd

View File

@ -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