diff options
author | James Booth <boothj5@gmail.com> | 2015-02-01 00:14:11 +0000 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2015-02-01 00:14:11 +0000 |
commit | e8b6c505cb825422ee467c9210ba0b87097aa85d (patch) | |
tree | d90815fdfce5f34382a5ac8c49092349032116c5 /src | |
parent | a19c0a5989c45ced8460284fe6591bebe08d22ac (diff) | |
download | profani-tty-e8b6c505cb825422ee467c9210ba0b87097aa85d.tar.gz |
Handle word wrapping wide chars
Diffstat (limited to 'src')
-rw-r--r-- | src/ui/inputwin.c | 2 | ||||
-rw-r--r-- | src/ui/window.c | 45 |
2 files changed, 33 insertions, 14 deletions
diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c index ee800e56..165a0b32 100644 --- a/src/ui/inputwin.c +++ b/src/ui/inputwin.c @@ -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); } \ No newline at end of file diff --git a/src/ui/window.c b/src/ui/window.c index 915acb3c..37da8e9d 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -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); } |