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:
parent
09ca3ad48f
commit
97a4ee78fd
@ -25,6 +25,7 @@
|
|||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "servers.h"
|
#include "servers.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
#include "utf8.h"
|
||||||
|
|
||||||
#define ALIGN_RIGHT 0x01
|
#define ALIGN_RIGHT 0x01
|
||||||
#define ALIGN_CUT 0x02
|
#define ALIGN_CUT 0x02
|
||||||
@ -320,18 +321,24 @@ static char *get_alignment(const char *text, int align, int flags, char pad)
|
|||||||
{
|
{
|
||||||
GString *str;
|
GString *str;
|
||||||
char *ret;
|
char *ret;
|
||||||
|
int policy;
|
||||||
|
unsigned int cut_bytes;
|
||||||
|
|
||||||
g_return_val_if_fail(text != NULL, NULL);
|
g_return_val_if_fail(text != NULL, NULL);
|
||||||
|
|
||||||
|
policy = string_policy(text);
|
||||||
|
|
||||||
str = g_string_new(text);
|
str = g_string_new(text);
|
||||||
|
|
||||||
/* cut */
|
/* cut */
|
||||||
if ((flags & ALIGN_CUT) && align > 0 && str->len > align)
|
if ((flags & ALIGN_CUT) && align > 0 && string_width(text, policy) > align) {
|
||||||
g_string_truncate(str, align);
|
string_chars_for_width(text, policy, align, &cut_bytes);
|
||||||
|
g_string_truncate(str, cut_bytes);
|
||||||
|
}
|
||||||
|
|
||||||
/* add pad characters */
|
/* add pad characters */
|
||||||
if (flags & ALIGN_PAD) {
|
if (flags & ALIGN_PAD) {
|
||||||
while (str->len < align) {
|
while (string_width(str->str, policy) < align) {
|
||||||
if (flags & ALIGN_RIGHT)
|
if (flags & ALIGN_RIGHT)
|
||||||
g_string_prepend_c(str, pad);
|
g_string_prepend_c(str, pad);
|
||||||
else
|
else
|
||||||
|
Loading…
x
Reference in New Issue
Block a user