Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 17 Jun 2009 14:47:06 +0000 (UTC)
From:      Hajimu UMEMOTO <ume@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r194366 - head/usr.bin/ncal
Message-ID:  <200906171447.n5HEl6Qg078846@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ume
Date: Wed Jun 17 14:47:06 2009
New Revision: 194366
URL: http://svn.freebsd.org/changeset/base/194366

Log:
  Fix column width of weekday names for multibyte locales.
  
  Noticed by:	nyan
  Discussed with:	matusita, takawata
  MFC after:	1 week

Modified:
  head/usr.bin/ncal/ncal.c

Modified: head/usr.bin/ncal/ncal.c
==============================================================================
--- head/usr.bin/ncal/ncal.c	Wed Jun 17 14:45:58 2009	(r194365)
+++ head/usr.bin/ncal/ncal.c	Wed Jun 17 14:47:06 2009	(r194366)
@@ -446,13 +446,18 @@ printmonth(int y, int m, int jd_flag)
 {
 	struct monthlines month;
 	struct weekdays wds;
-	int i;
+	int i, len;
 
 	mkmonth(y, m - 1, jd_flag, &month);
 	mkweekdays(&wds);
 	printf("    %ls %d\n", month.name, y);
-	for (i = 0; i != 7; i++)
-		wprintf(L"%.2ls%s\n", wds.names[i], month.lines[i]);
+	for (i = 0; i != 7; i++) {
+		len = wcslen(wds.names[i]);
+		if (wcswidth(wds.names[i], len) == len)
+			wprintf(L"%.2ls%s\n", wds.names[i], month.lines[i]);
+		else
+			wprintf(L"%.1ls%s\n", wds.names[i], month.lines[i]);
+	}
 	if (flag_weeks)
 		printf("  %s\n", month.weeks);
 }
@@ -830,7 +835,7 @@ mkmonthb(int y, int m, int jd_flag, stru
 void
 mkweekdays(struct weekdays *wds)
 {
-	int i, len;
+	int i, len, width = 0;
 	struct tm tm;
 	wchar_t buf[20];
 
@@ -839,11 +844,15 @@ mkweekdays(struct weekdays *wds)
 	for (i = 0; i != 7; i++) {
 		tm.tm_wday = (i+1) % 7;
 		wcsftime(buf, sizeof(buf), L"%a", &tm);
-		len = wcslen(buf);
-		if (len > 2)
-			len = 2;
-		wcscpy(wds->names[i], L"   ");
-		wcsncpy(wds->names[i] + 2 - len, buf, len);
+		for (len = wcslen(buf); len > 0; --len) {
+			if ((width = wcswidth(buf, len)) <= 2)
+				break;
+		}
+		wmemset(wds->names[i], L'\0', 4);
+		if (width == 1)
+			wds->names[i][0] = L' ';
+		wcsncat(wds->names[i], buf, len);
+		wcsncat(wds->names[i], L"  ", 3 - wcswidth(wds->names[i], 2));
 	}
 }
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200906171447.n5HEl6Qg078846>