1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05: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; int wrows, wcols;
getmaxyx(stdscr, 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 <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <wchar.h>
#include <glib.h> #include <glib.h>
#ifdef HAVE_NCURSESW_NCURSES_H #ifdef HAVE_NCURSESW_NCURSES_H
@ -985,7 +986,6 @@ _win_indent(WINDOW *win, int size)
static void static void
_win_print_wrapped(WINDOW *win, const char * const message) _win_print_wrapped(WINDOW *win, const char * const message)
{ {
int linei = 0;
int wordi = 0; int wordi = 0;
char *word = malloc(strlen(message) + 1); char *word = malloc(strlen(message) + 1);
@ -998,18 +998,27 @@ _win_print_wrapped(WINDOW *win, const char * const message)
} }
free(time_pref); free(time_pref);
while (message[linei] != '\0') { gchar *curr_ch = g_utf8_offset_to_pointer(message, 0);
if (message[linei] == ' ') {
while (*curr_ch != '\0') {
if (*curr_ch == ' ') {
waddch(win, ' '); waddch(win, ' ');
linei++; curr_ch = g_utf8_next_char(curr_ch);
} else if (message[linei] == '\n') { } else if (*curr_ch == '\n') {
waddch(win, '\n'); waddch(win, '\n');
_win_indent(win, indent); _win_indent(win, indent);
linei++; curr_ch = g_utf8_next_char(curr_ch);
} else { } else {
// get word
wordi = 0; wordi = 0;
while (message[linei] != ' ' && message[linei] != '\n' && message[linei] != '\0') { mbstate_t internal;
word[wordi++] = message[linei++]; 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'; word[wordi] = '\0';
@ -1017,17 +1026,27 @@ _win_print_wrapped(WINDOW *win, const char * const message)
int maxx = getmaxx(win); int maxx = getmaxx(win);
// word larger than line // word larger than line
if (strlen(word) > (maxx - indent)) { if (utf8_display_len(word) > (maxx - indent)) {
int i; gchar *word_ch = g_utf8_offset_to_pointer(word, 0);
for (i = 0; i < wordi; i++) { while(*word_ch != '\0') {
curx = getcurx(win); curx = getcurx(win);
if (curx < indent) { if (curx < indent) {
_win_indent(win, 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 { } else {
if (curx + strlen(word) > maxx) { if (curx + utf8_display_len(word) > maxx) {
waddch(win, '\n'); waddch(win, '\n');
_win_indent(win, indent); _win_indent(win, indent);
} }