Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 3 Mar 2010 06:38:28 +0000 (UTC)
From:      Edwin Groothuis <edwin@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-user@freebsd.org
Subject:   svn commit: r204621 - user/edwin/ncal
Message-ID:  <201003030638.o236cSdS070685@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: edwin
Date: Wed Mar  3 06:38:28 2010
New Revision: 204621
URL: http://svn.freebsd.org/changeset/base/204621

Log:
  Reimplement backwards compatible calendar with the new monthrange approach.

Modified:
  user/edwin/ncal/ncal.c

Modified: user/edwin/ncal/ncal.c
==============================================================================
--- user/edwin/ncal/ncal.c	Wed Mar  3 05:46:35 2010	(r204620)
+++ user/edwin/ncal/ncal.c	Wed Mar  3 06:38:28 2010	(r204621)
@@ -530,12 +530,8 @@ printmonthb(int y, int m, int jd_flag)
 		printf("%s\n", month.lines[i]+1);
 }
 
-void
-monthrangeb(int y, int jd_flag, int m, int before, int after)
-{
-	printf("BUUURP\n");
-}
-
+#define MW(mw, ms, ml) \
+	strlen(ms) > (ml) ? (mw) + 9 : (mw)
 #define	DECREASEMONTH(m, y) 		\
 		if (--m == 0) {		\
 			m = 12;		\
@@ -548,6 +544,93 @@ monthrangeb(int y, int jd_flag, int m, i
 		}
 #define	M2Y(m)	((m) / 12)
 #define	M2M(m)	(1 + (m) % 12) 
+
+void
+monthrangeb(int y, int jd_flag, int m, int before, int after)
+{
+	struct monthlines year[12];
+	struct weekdays wds;
+	char	s[80], t[80];
+	wchar_t	ws[80], wt[80];
+	char	*wdss;
+	int     i, j;
+	int     mpl;
+	int     mw;
+	int	m1, m2;
+	int	printyearheader;
+	int	prevyear = -1;
+
+	mpl = jd_flag ? 2 : 3;
+	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
+	wdss = (mpl == 2) ? " " : "";
+
+	while (before != 0) {
+		DECREASEMONTH(m, y);
+		before--;
+		after++;
+	}
+	m1 = y * 12 + m - 1;
+	m2 = m1 + after;
+
+	mkweekdays(&wds);
+
+	/*
+	 * The year header is printed when there are more than 'mpl' months
+	 * and if the first month is a multitude of 'mpl'.
+	 * If not, it will print the year behind every month.
+	 */
+	printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0;
+
+	m = m1;
+	while (m <= m2) {
+		int count = 0;
+		for (i = 0; i != mpl && m + i <= m2; i++) {
+			mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
+			count++;
+		}
+
+		/* Empty line between two rows of months */
+		if (m != m1)
+			printf("\n");
+
+		/* Year at the top */
+		if (printyearheader && M2Y(m) != prevyear) {
+			sprintf(s, "%d", M2Y(m));
+			printf("%s\n", center(t, s, mpl * mw));
+			prevyear = M2Y(m);
+		}
+
+		/* Month names */
+		for (i = 0; i < count; i++)
+			if (printyearheader)
+				wprintf(L"%-*ls  ",
+				    mw, wcenter(ws, year[i].name, mw));
+			else
+				wprintf(L"%-ls %-*d  ",
+				    wcenter(ws, year[i].name, mw - 4),
+				    mw - wcslen(year[i].name) - 1, M2Y(m + i));
+		printf("\n");
+
+		/* Day of the week names */
+		for (i = 0; i < count; i++) {
+			wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ",
+				wdss, wds.names[6], wdss, wds.names[0],
+				wdss, wds.names[1], wdss, wds.names[2],
+				wdss, wds.names[3], wdss, wds.names[4],
+				wdss, wds.names[5]);
+		}
+		printf("\n");
+
+		for (i = 0; i != 6; i++) {
+			for (j = 0; j < count; j++)
+				printf("%-*s  ", mw, year[j].lines[i]+1);
+			printf("\n");
+		}
+
+		m += mpl;
+	}
+}
+
 void
 monthrange(int y, int jd_flag, int m, int before, int after)
 {
@@ -589,14 +672,11 @@ monthrange(int y, int jd_flag, int m, in
 			count++;
 		}
 
-#define MW(mw, ms, ml) \
-	strlen(ms) > (ml) ? (mw) + 9 : (mw)
-
-		/* Empty line between two months */
+		/* Empty line between two rows of months */
 		if (m != m1)
 			printf("\n");
 
-		/* Year at the top, only for calendars which start at January */
+		/* Year at the top */
 		if (printyearheader && M2Y(m) != prevyear) {
 			sprintf(s, "%d", M2Y(m));
 			printf("%s\n", center(t, s, mpl * mw));



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