public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 12/13] cal: use month contents structure for --three printing
Date: Sun, 10 Nov 2013 18:23:39 +0000	[thread overview]
Message-ID: <1384107820-498-13-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1384107820-498-1-git-send-email-kerolasa@iki.fi>

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/cal.c | 136 +++++++++++--------------------------------------------
 1 file changed, 26 insertions(+), 110 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index a3eff48..a3c5c40 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -141,7 +141,6 @@ static int		Slen;		/* strlen of Senter+Sexit */
 #endif
 
 static const char	*Senter="", *Sexit="";/* enter and exit standout mode */
-static char		*Hrow;		/* pointer to highlighted row in month */
 
 #include "widechar.h"
 
@@ -262,8 +261,7 @@ struct cal_month {
 /* function prototypes */
 static int leap_year(long year);
 static void headers_init(struct cal_control *ctl);
-static int do_monthly(int day, int month, long year, struct fmt_st *out,
-		      const struct cal_control *ctl);
+static void set_consecutive_months(struct cal_month *month, int m, long y);
 static void cal_fill_month(struct cal_month *month, const struct cal_control *ctl);
 static void cal_output_header(struct cal_month *month, const struct cal_control *ctl);
 static void cal_output_months(struct cal_month *month, const struct cal_control *ctl);
@@ -548,64 +546,17 @@ static void headers_init(struct cal_control *ctl)
 	}
 }
 
-static int do_monthly(int day, int month, long year, struct fmt_st *out,
-		      const struct cal_control *ctl)
+static void set_consecutive_months(struct cal_month *month, int m, long y)
 {
-	int col, row, days[MAXDAYS];
-	char *p, lineout[FMT_ST_CHARS];
-	int pos = 0;
-
-	day_array(day, month, year, days, ctl);
-
-	if (ctl->header_hint) {
-		snprintf(lineout, sizeof(lineout), _("%s"), ctl->full_month[month - 1]);
-		center_str(lineout, out->s[pos], ARRAY_SIZE(out->s[pos]), ctl->week_width - 1);
-		pos++;
-		snprintf(lineout, sizeof(lineout), _("%ld"), year);
-		center_str(lineout, out->s[pos], ARRAY_SIZE(out->s[pos]), ctl->week_width - 1);
-		pos++;
-	} else {
-		/* TRANSLATORS: %s is the month name, %ld the year number.
-		 * You can change the order and/or add something here;
-		 * e.g. for Basque the translation should be "%2$ldko %1$s".
-		 */
-		snprintf(lineout, sizeof(lineout), _("%s %ld"),
-			ctl->full_month[month - 1], year);
-		center_str(lineout, out->s[pos], ARRAY_SIZE(out->s[pos]), ctl->week_width - 1);
-		pos++;
-	}
-
-	snprintf(out->s[pos++], FMT_ST_CHARS, "%s%s",
-				(ctl->weektype ? "   " : ""),
-				day_headings);
-
-	for (row = 0; row < DAYS_IN_WEEK - 1; row++) {
-		int has_hl = 0;
-		p = lineout;
-		if (ctl->weektype)
-			for (col = 0; col < DAYS_IN_WEEK; col++) {
-				int xd = days[row * DAYS_IN_WEEK + col];
-				if (xd != SPACE) {
-					int wn = week_number(xd & ~TODAY_FLAG,
-							month, year, ctl);
-					p = ascii_weeknum(p, wn, ctl);
-					break;
-				} else if (col+1 == DAYS_IN_WEEK)
-					p += sprintf(p,"   ");
-			}
-		for (col = 0; col < DAYS_IN_WEEK; col++) {
-			int xd = days[row * DAYS_IN_WEEK + col];
-			if (xd != SPACE && (xd & TODAY_FLAG))
-				has_hl = 1;
-			p = ascii_day(p, xd, ctl);
+	struct cal_month *i;
+	for (i = month; i; i = i->next) {
+		i->month = m++;
+		i->year = y;
+		if (MONTHS_IN_YEAR < m) {
+			m = 1;
+			y++;
 		}
-		*p = '\0';
-		snprintf(out->s[row+pos], FMT_ST_CHARS, "%s", lineout);
-		if (has_hl)
-			Hrow = out->s[row+pos];
 	}
-	pos += row;
-	return pos;
 }
 
 static void cal_fill_month(struct cal_month *month, const struct cal_control *ctl)
@@ -773,62 +724,27 @@ static void monthly(const struct cal_control *ctl)
 
 static void monthly3(const struct cal_control *ctl)
 {
-	char lineout[FMT_ST_CHARS];
-	int i;
-	int rows;
-	struct fmt_st out_prev;
-	struct fmt_st out_curm;
-	struct fmt_st out_next;
-	int prev_month, next_month;
-	long prev_year, next_year;
-
-	memset(&out_prev, 0, sizeof(struct fmt_st));
-	memset(&out_curm, 0, sizeof(struct fmt_st));
-	memset(&out_next, 0, sizeof(struct fmt_st));
+	struct cal_month m1, m2, m3, *i;
+	int first_month;
+	long first_year;
+
+	m1.next = &m2;
+	m2.next = &m3;
+	m3.next = NULL;
+
 	if (ctl->req.month == 1) {
-		prev_month = MONTHS_IN_YEAR;
-		prev_year  = ctl->req.year - 1;
-	} else {
-		prev_month = ctl->req.month - 1;
-		prev_year  = ctl->req.year;
-	}
-	if (ctl->req.month == MONTHS_IN_YEAR) {
-		next_month = 1;
-		next_year  = ctl->req.year + 1;
+		first_month = MONTHS_IN_YEAR;
+		first_year = ctl->req.year - 1;
 	} else {
-		next_month = ctl->req.month + 1;
-		next_year  = ctl->req.year;
-	}
-	if (ctl->header_hint)
-		rows = FMT_ST_LINES;
-	else
-		rows = FMT_ST_LINES - 1;
-	do_monthly(ctl->req.day, prev_month, prev_year, &out_prev, ctl);
-	do_monthly(ctl->req.day, ctl->req.month, ctl->req.year, &out_curm, ctl);
-	do_monthly(ctl->req.day, next_month, next_year, &out_next, ctl);
-
-	for (i = 0; i < (ctl->header_hint ? 3 : 2); i++) {
-		snprintf(lineout, sizeof(lineout),
-			"%s  %s  %s\n", out_prev.s[i], out_curm.s[i], out_next.s[i]);
-		my_putstring(lineout);
+		first_month = ctl->req.month - 1;
+		first_year = ctl->req.year;
 	}
-	for (i = ctl->header_hint ? 3 : 2; i < rows; i++) {
-		int w1, w2, w3;
-		w1 = w2 = w3 = ctl->week_width;
 
-#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW) || defined(HAVE_LIBTERMCAP)
-		/* adjust width to allow for non printable characters */
-		w1 += (out_prev.s[i] == Hrow ? Slen : 0);
-		w2 += (out_curm.s[i] == Hrow ? Slen : 0);
-		w3 += (out_next.s[i] == Hrow ? Slen : 0);
-#endif
-		snprintf(lineout, sizeof(lineout), "%-*s %-*s %-*s\n",
-		       w1, out_prev.s[i],
-		       w2, out_curm.s[i],
-		       w3, out_next.s[i]);
-
-		my_putstring(lineout);
-	}
+	set_consecutive_months(&m1, first_month, first_year);
+	for (i = &m1; i; i = i->next)
+		cal_fill_month(i, ctl);
+	cal_output_header(&m1, ctl);
+	cal_output_months(&m1, ctl);
 }
 
 static char *append_weeknum(char *p, int *dp,
-- 
1.8.4.2


  parent reply	other threads:[~2013-11-10 18:25 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-10 18:23 [PATCH 00/13] pull: cal: code revisit part I try II Sami Kerola
2013-11-10 18:23 ` [PATCH 01/13] cal: use control structure for run time configuration Sami Kerola
2013-11-10 18:23 ` [PATCH 02/13] cal: rename ascii_wnum() to ascii_weeknum() Sami Kerola
2013-11-10 18:23 ` [PATCH 03/13] cal: add input variable names to function prototypes Sami Kerola
2013-11-10 18:23 ` [PATCH 04/13] cal: make day_in_week() use same variable names as other functions Sami Kerola
2013-11-10 18:23 ` [PATCH 05/13] cal: simplify ascii_weeknum() function Sami Kerola
2013-11-10 18:23 ` [PATCH 06/13] cal: determine output width at beginning of run and reuse result Sami Kerola
2013-11-10 18:23 ` [PATCH 07/13] tests: cal: take account week numbers when determing month lenght Sami Kerola
2013-11-10 18:23 ` [PATCH 08/13] cal: make user request to be part of read-only control struct Sami Kerola
2013-11-10 18:23 ` [PATCH 09/13] tests: cal: do not let --week=<num> to adjust requested month Sami Kerola
2013-11-10 18:23 ` [PATCH 10/13] cal: determine how many header lines are needed at initialization Sami Kerola
2013-11-10 18:23 ` [PATCH 11/13] cal: add month contents structure Sami Kerola
2013-11-10 18:23 ` Sami Kerola [this message]
2013-12-06 10:07   ` [PATCH 12/13] cal: use month contents structure for --three printing Karel Zak
2013-11-10 18:23 ` [PATCH 13/13] cal: use month contents structure for --year printing Sami Kerola
2013-12-06 10:02 ` [PATCH 00/13] pull: cal: code revisit part I try II Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1384107820-498-13-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox