Implement wordWidth
This commit is contained in:
		
							parent
							
								
									05256b68fe
								
							
						
					
					
						commit
						c799310d67
					
				
							
								
								
									
										21
									
								
								ui.c
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								ui.c
									
									
									
									
									
								
							@ -25,6 +25,8 @@
 | 
				
			|||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <sysexits.h>
 | 
					#include <sysexits.h>
 | 
				
			||||||
#include <time.h>
 | 
					#include <time.h>
 | 
				
			||||||
 | 
					#include <wchar.h>
 | 
				
			||||||
 | 
					#include <wctype.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "chat.h"
 | 
					#include "chat.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -112,7 +114,6 @@ static struct Window *windowFor(size_t id) {
 | 
				
			|||||||
	if (!window) err(EX_OSERR, "malloc");
 | 
						if (!window) err(EX_OSERR, "malloc");
 | 
				
			||||||
	window->id = id;
 | 
						window->id = id;
 | 
				
			||||||
	window->pad = newpad(PadLines, COLS);
 | 
						window->pad = newpad(PadLines, COLS);
 | 
				
			||||||
	wsetscrreg(window->pad, 0, PadLines - 1);
 | 
					 | 
				
			||||||
	scrollok(window->pad, true);
 | 
						scrollok(window->pad, true);
 | 
				
			||||||
	wmove(window->pad, PadLines - 1, 0);
 | 
						wmove(window->pad, PadLines - 1, 0);
 | 
				
			||||||
	window->heat = Cold;
 | 
						window->heat = Cold;
 | 
				
			||||||
@ -211,20 +212,28 @@ static void styleParse(struct Style *style, const char **str, size_t *len) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
static int wordWidth(const char *str) {
 | 
					static int wordWidth(const char *str) {
 | 
				
			||||||
	size_t len = strcspn(str, " ");
 | 
						size_t len = strcspn(str, " ");
 | 
				
			||||||
	// TODO: wcswidth.
 | 
						int width = 0;
 | 
				
			||||||
	return len;
 | 
						while (len) {
 | 
				
			||||||
 | 
							wchar_t wc;
 | 
				
			||||||
 | 
							int n = mbtowc(&wc, str, len);
 | 
				
			||||||
 | 
							if (n < 1) return width + len;
 | 
				
			||||||
 | 
							width += (iswprint(wc) ? wcwidth(wc) : 0);
 | 
				
			||||||
 | 
							str += n;
 | 
				
			||||||
 | 
							len -= n;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return width;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void styleAdd(WINDOW *win, const char *str) {
 | 
					static void styleAdd(WINDOW *win, const char *str) {
 | 
				
			||||||
	int _, x, width;
 | 
						int y, x, width;
 | 
				
			||||||
	getmaxyx(win, _, width);
 | 
						getmaxyx(win, y, width);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	size_t len;
 | 
						size_t len;
 | 
				
			||||||
	struct Style style = Reset;
 | 
						struct Style style = Reset;
 | 
				
			||||||
	while (*str) {
 | 
						while (*str) {
 | 
				
			||||||
		if (*str == ' ') {
 | 
							if (*str == ' ') {
 | 
				
			||||||
 | 
								getyx(win, y, x);
 | 
				
			||||||
			const char *word = &str[strspn(str, " ")];
 | 
								const char *word = &str[strspn(str, " ")];
 | 
				
			||||||
			getyx(win, _, x);
 | 
					 | 
				
			||||||
			if (width - x - 1 < wordWidth(word)) {
 | 
								if (width - x - 1 < wordWidth(word)) {
 | 
				
			||||||
				waddch(win, '\n');
 | 
									waddch(win, '\n');
 | 
				
			||||||
				str = word;
 | 
									str = word;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user