]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
cal: Add --span option
authorDeiz <silverwraithii@gmail.com>
Sat, 10 Oct 2015 00:39:02 +0000 (20:39 -0400)
committerDeiz <silverwraithii@gmail.com>
Sat, 10 Oct 2015 00:39:02 +0000 (20:39 -0400)
This allows the date spanning behaviour of -3 to be used with other
month ranges.

Signed-off-by: Deiz <silverwraithii@gmail.com>
misc-utils/cal.1
misc-utils/cal.c

index e032b97cabedca7f38df12b3f0072fd71f71c5aa..071c693a0993a3aa02cb8167fb6ec93f428acdc9 100644 (file)
@@ -57,6 +57,9 @@ Display three months spanning the date.
 \fB\-n , \-\-months\fR \fInumber\fR
 Display \fInumber\fR of months, starting from the month containing the date.
 .TP
+\fB\-S, \fB\-\-span\fR
+Display months spanning the date.
+.TP
 \fB\-s\fR, \fB\-\-sunday\fR
 Display Sunday as the first day of the week.
 .TP
index 7441ecfc46120a747613dea32a257ce63d2770f9..62c5818dc9c78e382e473082cf7284aff82f7380 100644 (file)
@@ -297,6 +297,7 @@ int main(int argc, char **argv)
                {"monday", no_argument, NULL, 'm'},
                {"julian", no_argument, NULL, 'j'},
                {"months", required_argument, NULL, 'n'},
+               {"span", no_argument, NULL, 'S'},
                {"year", no_argument, NULL, 'y'},
                {"week", optional_argument, NULL, 'w'},
                {"color", optional_argument, NULL, OPT_COLOR},
@@ -365,7 +366,7 @@ int main(int argc, char **argv)
                ctl.weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % DAYS_IN_WEEK;
        }
 #endif
-       while ((ch = getopt_long(argc, argv, "13mjn:sywYVh", longopts, NULL)) != -1) {
+       while ((ch = getopt_long(argc, argv, "13mjn:sSywYVh", longopts, NULL)) != -1) {
 
                err_exclusive_options(ch, longopts, excl, excl_st);
 
@@ -398,6 +399,9 @@ int main(int argc, char **argv)
                        ctl.num_months = strtou32_or_err(optarg,
                                                _("invalid month argument"));
                        break;
+               case 'S':
+                       ctl.span_months = 1;
+                       break;
                case 'w':
                        if (optarg) {
                                ctl.req.week = strtos32_or_err(optarg,
@@ -736,16 +740,18 @@ static void cal_output_months(struct cal_month *month, const struct cal_control
 static void monthly(const struct cal_control *ctl)
 {
        struct cal_month m1,m2,m3, *m;
-       int i, rows, month = ctl->req.start_month ? ctl->req.start_month : ctl->req.month;
+       int i, rows, new_month, month = ctl->req.start_month ? ctl->req.start_month : ctl->req.month;
        int32_t year = ctl->req.year;
 
-       /* cal -3 */
-       if (ctl->num_months == 3 && ctl->span_months) {
-               if (month == 1){
-                       month = MONTHS_IN_YEAR;
+       /* cal -3, cal -Y --span, etc. */
+       if (ctl->span_months) {
+               new_month = month - ctl->num_months / 2;
+               if (new_month < 1) {
+                       month = new_month + MONTHS_IN_YEAR;
                        year--;
-               } else
-                       month--;
+               }
+               else
+                       month = new_month;
        }
 
        m1.next = (ctl->months_in_row > 1) ? &m2 : NULL;
@@ -958,6 +964,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
        fputs(_(" -1, --one             show only a single month (default)\n"), out);
        fputs(_(" -3, --three           show three months spanning the date\n"), out);
        fputs(_(" -n, --months <num>    show num months starting with date's month\n"), out);
+       fputs(_(" -S, --span            span the date when displaying multiple months\n"), out);
        fputs(_(" -s, --sunday          Sunday as first day of week\n"), out);
        fputs(_(" -m, --monday          Monday as first day of week\n"), out);
        fputs(_(" -j, --julian          output Julian dates\n"), out);