1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00

Handle word wrapping wide chars

This commit is contained in:
James Booth 2015-02-01 00:14:11 +00:00
parent a19c0a5989
commit e8b6c505cb
2 changed files with 33 additions and 14 deletions

View File

@ -485,5 +485,5 @@ _inp_win_update_virtual(void)
{
int wrows, wcols;
getmaxyx(stdscr, wrows, wcols);
pnoutrefresh(inp_win, 0, pad_start, wrows-1, 0, wrows-1, wcols-1);
pnoutrefresh(inp_win, 0, pad_start, wrows-1, 0, wrows-1, wcols-2);
}

View File

@ -37,6 +37,7 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <wchar.h>
#include <glib.h>
#ifdef HAVE_NCURSESW_NCURSES_H
@ -985,7 +986,6 @@ _win_indent(WINDOW *win, int size)
static void
_win_print_wrapped(WINDOW *win, const char * const message)
{
int linei = 0;
int wordi = 0;
char *word = malloc(strlen(message) + 1);
@ -998,18 +998,27 @@ _win_print_wrapped(WINDOW *win, const char * const message)
}
free(time_pref);
while (message[linei] != '\0') {
if (message[linei] == ' ') {
gchar *curr_ch = g_utf8_offset_to_pointer(message, 0);
while (*curr_ch != '\0') {
if (*curr_ch == ' ') {
waddch(win, ' ');
linei++;
} else if (message[linei] == '\n') {
curr_ch = g_utf8_next_char(curr_ch);
} else if (*curr_ch == '\n') {
waddch(win, '\n');
_win_indent(win, indent);
linei++;
curr_ch = g_utf8_next_char(curr_ch);
} else {
// get word
wordi = 0;
while (message[linei] != ' ' && message[linei] != '\n' && message[linei] != '\0') {
word[wordi++] = message[linei++];
mbstate_t internal;
while (*curr_ch != ' ' && *curr_ch != '\n' && *curr_ch != '\0') {
size_t ch_len = mbrlen(curr_ch, 4, &internal);
int offset = 0;
while (offset < ch_len) {
word[wordi++] = curr_ch[offset++];
}
curr_ch = g_utf8_next_char(curr_ch);
}
word[wordi] = '\0';
@ -1017,17 +1026,27 @@ _win_print_wrapped(WINDOW *win, const char * const message)
int maxx = getmaxx(win);
// word larger than line
if (strlen(word) > (maxx - indent)) {
int i;
for (i = 0; i < wordi; i++) {
if (utf8_display_len(word) > (maxx - indent)) {
gchar *word_ch = g_utf8_offset_to_pointer(word, 0);
while(*word_ch != '\0') {
curx = getcurx(win);
if (curx < indent) {
_win_indent(win, indent);
}
waddch(win, word[i]);
gchar copy[wordi++];
g_utf8_strncpy(copy, word_ch, 1);
if (curx + utf8_display_len(copy) > maxx) {
waddch(win, '\n');
_win_indent(win, indent);
}
waddstr(win, copy);
word_ch = g_utf8_next_char(word_ch);
}
} else {
if (curx + strlen(word) > maxx) {
if (curx + utf8_display_len(word) > maxx) {
waddch(win, '\n');
_win_indent(win, indent);
}