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 10/13] cal: determine how many header lines are needed at initialization
Date: Sun, 10 Nov 2013 18:23:37 +0000	[thread overview]
Message-ID: <1384107820-498-11-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 | 53 ++++++++++++++++++++++-------------------------------
 1 file changed, 22 insertions(+), 31 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 185b7a9..0a6cd8d 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -247,16 +247,16 @@ struct cal_control {
 	size_t week_width;		/* 7 * day_width + possible week num */
 	struct cal_request req;		/* the times user is interested */
 	unsigned int	julian:1,	/* julian output */
-			yflag:1;	/* print whole year */
+			yflag:1,	/* print whole year */
+			header_hint:1;	/* does month name + year need two lines to fit */
 };
 
 /* 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, int header_hint,
+static int do_monthly(int day, int month, long year, struct fmt_st *out,
 		      const struct cal_control *ctl);
 static void monthly(const struct cal_control *ctl);
-static int two_header_lines(int month, long year, const struct cal_control *ctl);
 static void monthly3(const struct cal_control *ctl);
 static char *append_weeknum(char *p, int *dp, int month, long year, int cal, int row,
 			    const struct cal_control *ctl);
@@ -510,6 +510,10 @@ static void headers_init(struct cal_control *ctl)
 {
 	size_t i, wd;
 	char *cur_dh = day_headings;
+	char tmp[FMT_ST_CHARS];
+	size_t year_len;
+
+	year_len = snprintf(tmp, sizeof(tmp), "%ld", ctl->req.year);
 
 	for (i = 0; i < DAYS_IN_WEEK; i++) {
 		size_t space_left;
@@ -525,12 +529,16 @@ static void headers_init(struct cal_control *ctl)
 				     space_left, ctl->day_width - 1);
 	}
 
-	for (i = 0; i < MONTHS_IN_YEAR; i++)
+	for (i = 0; i < MONTHS_IN_YEAR; i++) {
 		ctl->full_month[i] = nl_langinfo(MON_1 + i);
+		/* The +1 after year_len is space in between month and year. */
+		if (ctl->week_width < strlen(ctl->full_month[i]) + year_len + 1)
+			ctl->header_hint = 1;
+	}
 }
 
 static int do_monthly(int day, int month, long year, struct fmt_st *out,
-		      int header_hint, const struct cal_control *ctl)
+		      const struct cal_control *ctl)
 {
 	int col, row, days[MAXDAYS];
 	char *p, lineout[FMT_ST_CHARS];
@@ -538,9 +546,7 @@ static int do_monthly(int day, int month, long year, struct fmt_st *out,
 
 	day_array(day, month, year, days, ctl);
 
-	if (header_hint < 0)
-		header_hint = two_header_lines(month, year, ctl);
-	if (header_hint) {
+	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++;
@@ -596,30 +602,18 @@ static void monthly(const struct cal_control *ctl)
 	int i, rows;
 	struct fmt_st out;
 
-	rows = do_monthly(ctl->req.day, ctl->req.month, ctl->req.year, &out, -1, ctl);
+	rows = do_monthly(ctl->req.day, ctl->req.month, ctl->req.year, &out, ctl);
 	for (i = 0; i < rows; i++) {
 		my_putstring(out.s[i]);
 		my_putstring("\n");
 	}
 }
 
-static int two_header_lines(int month, long year, const struct cal_control *ctl)
-{
-	char lineout[FMT_ST_CHARS];
-	size_t len;
-	snprintf(lineout, sizeof(lineout), "%ld", year);
-	len = strlen(lineout);
-	len += strlen(ctl->full_month[month - 1]) + 1;
-	if (ctl->week_width - 1 < len)
-		return 1;
-	return 0;
-}
-
 static void monthly3(const struct cal_control *ctl)
 {
 	char lineout[FMT_ST_CHARS];
 	int i;
-	int rows, two_lines;
+	int rows;
 	struct fmt_st out_prev;
 	struct fmt_st out_curm;
 	struct fmt_st out_next;
@@ -643,23 +637,20 @@ static void monthly3(const struct cal_control *ctl)
 		next_month = ctl->req.month + 1;
 		next_year  = ctl->req.year;
 	}
-	two_lines = two_header_lines(prev_month, prev_year, ctl);
-	two_lines += two_header_lines(ctl->req.month, ctl->req.year, ctl);
-	two_lines += two_header_lines(next_month, next_year, ctl);
-	if (0 < two_lines)
+	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, two_lines, ctl);
-	do_monthly(ctl->req.day, ctl->req.month, ctl->req.year, &out_curm, two_lines, ctl);
-	do_monthly(ctl->req.day, next_month, next_year, &out_next, two_lines, ctl);
+	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 < (two_lines ? 3 : 2); i++) {
+	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);
 	}
-	for (i = two_lines ? 3 : 2; i < rows; i++) {
+	for (i = ctl->header_hint ? 3 : 2; i < rows; i++) {
 		int w1, w2, w3;
 		w1 = w2 = w3 = ctl->week_width;
 
-- 
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 ` Sami Kerola [this message]
2013-11-10 18:23 ` [PATCH 11/13] cal: add month contents structure Sami Kerola
2013-11-10 18:23 ` [PATCH 12/13] cal: use month contents structure for --three printing Sami Kerola
2013-12-06 10:07   ` 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-11-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