]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/cal.c
Merge branch 'PR/libmount-utab-event' of github.com:karelzak/util-linux-work
[thirdparty/util-linux.git] / misc-utils / cal.c
index 77782867cd9d68ce3758af8cb40a52e97315ff3e..e6f4a6e4f3a6a4061c9b4fb007e6106923a6faff 100644 (file)
 #define DOY_MONTH_WIDTH        27      /* -j month width */
 #define DOM_MONTH_WIDTH        20      /* month width */
 
-static const char *Senter = "", *Sexit = "";   /* enter and exit standout mode */
+enum {
+       CAL_COLOR_TODAY,
+       CAL_COLOR_HEADER,
+       CAL_COLOR_WEEKNUMBER,
+       CAL_COLOR_WORKDAY,
+       CAL_COLOR_WEEKEND
+};
+
+static const struct { const char * const scheme; const char * dflt; } colors[] =
+{
+        [CAL_COLOR_TODAY]      = { "today",      UL_COLOR_REVERSE },
+        [CAL_COLOR_WEEKNUMBER] = { "weeknumber", UL_COLOR_REVERSE },
+        [CAL_COLOR_HEADER]     = { "header",     ""               },
+       [CAL_COLOR_WORKDAY]    = { "workday",    ""               },
+       [CAL_COLOR_WEEKEND]    = { "weekend",    ""               }
+};
+
+static inline void cal_enable_color(int id)
+{
+       color_scheme_enable(colors[id].scheme, colors[id].dflt);
+}
+
+static inline const char *cal_get_color_sequence(int id)
+{
+       return color_scheme_get_sequence(colors[id].scheme, colors[id].dflt);
+}
+
+static inline void cal_disable_color(int id)
+{
+       const char *seq = cal_get_color_sequence(id);
+       if (seq && seq[0])
+               color_disable();
+}
+
+static inline const char *cal_get_color_disable_sequence(int id)
+{
+       const char *seq = cal_get_color_sequence(id);
+       if (seq && seq[0])
+               return UL_COLOR_RESET;
+       else
+               return "";
+}
 
 #include "widechar.h"
 
@@ -279,7 +320,7 @@ int main(int argc, char **argv)
                {"twelve", no_argument, NULL, 'Y'},
                {"help", no_argument, NULL, 'h'},
                {"vertical", no_argument, NULL,'v'},
-               {"column", required_argument, NULL,'c'},
+               {"columns", required_argument, NULL,'c'},
                {NULL, 0, NULL, 0}
        };
 
@@ -392,7 +433,8 @@ int main(int argc, char **argv)
                        if (strcmp(optarg, "auto") == 0)
                                cols = COLUMNS_AUTO;
                        else
-                               cols = strtosize_or_err(optarg, "foo");
+                               cols = strtosize_or_err(optarg,
+                                               _("failed to parse columns"));
                        break;
                case 'V':
                        print_version(EXIT_SUCCESS);
@@ -515,10 +557,7 @@ int main(int argc, char **argv)
        weekdays_init(&ctl);
        headers_init(&ctl);
 
-       if (colors_init(ctl.colormode, "cal") > 0) {
-               Senter = UL_COLOR_REVERSE;
-               Sexit = UL_COLOR_RESET;
-       } else {
+       if (colors_init(ctl.colormode, "cal") == 0) {
                /* disable */
                ctl.req.day = 0;
                ctl.weektype &= ~WEEK_NUM_MASK;
@@ -653,9 +692,9 @@ static void headers_init(struct cal_control *ctl)
        for (i = 0; i < DAYS_IN_WEEK; i++) {
                size_t space_left;
 
-               if (i)
-                       strcat(cur_dh++, " ");
                space_left = sizeof(day_headings) - (cur_dh - day_headings);
+               if (i && space_left)
+                       strncat(cur_dh++, " ", space_left--);
 
                if (space_left <= (ctl->day_width - 1))
                        break;
@@ -733,6 +772,8 @@ static void cal_output_header(struct cal_month *month, const struct cal_control
        char out[FMT_ST_CHARS];
        struct cal_month *i;
 
+       cal_enable_color(CAL_COLOR_HEADER);
+
        if (ctl->header_hint || ctl->header_year) {
                for (i = month; i; i = i->next) {
                        snprintf(out, sizeof(out), "%s", ctl->full_month[i->month - 1]);
@@ -763,6 +804,7 @@ static void cal_output_header(struct cal_month *month, const struct cal_control
                if (i->next != NULL)
                        printf("%*s", ctl->gutter_width, "");
        }
+       cal_disable_color(CAL_COLOR_HEADER);
        fputc('\n', stdout);
 }
 
@@ -802,11 +844,20 @@ static void cal_vert_output_header(struct cal_month *month,
        fputc('\n', stdout);
 }
 
+#define fput_seq(_s)   do { if ((_s) && *(_s)) fputs((_s), stdout); } while(0)
+
 static void cal_output_months(struct cal_month *month, const struct cal_control *ctl)
 {
        int reqday, week_line, d;
        int skip;
        struct cal_month *i;
+       int firstwork = ctl->weekstart == SUNDAY ? 1 : 0;       /* first workday in week */
+
+       /* Let's keep sequence cached rather than search it for each day */
+       const char *seq_wo_start = cal_get_color_sequence(CAL_COLOR_WORKDAY);
+       const char *seq_wo_end = cal_get_color_disable_sequence(CAL_COLOR_WORKDAY);
+       const char *seq_we_start = cal_get_color_sequence(CAL_COLOR_WEEKEND);
+       const char *seq_we_end = cal_get_color_disable_sequence(CAL_COLOR_WEEKEND);
 
        for (week_line = 0; week_line < MAXDAYS / DAYS_IN_WEEK; week_line++) {
                for (i = month; i; i = i->next) {
@@ -824,7 +875,10 @@ static void cal_output_months(struct cal_month *month, const struct cal_control
                        if (ctl->weektype) {
                                if (0 < i->weeks[week_line]) {
                                        if ((ctl->weektype & WEEK_NUM_MASK) == i->weeks[week_line])
-                                               printf("%s%2d%s", Senter, i->weeks[week_line], Sexit);
+                                               printf("%s%2d%s",
+                                                      cal_get_color_sequence(CAL_COLOR_WEEKNUMBER),
+                                                      i->weeks[week_line],
+                                                      cal_get_color_disable_sequence(CAL_COLOR_WEEKNUMBER));
                                        else
                                                printf("%2d", i->weeks[week_line]);
                                } else
@@ -837,14 +891,22 @@ static void cal_output_months(struct cal_month *month, const struct cal_control
 
                        for (d = DAYS_IN_WEEK * week_line;
                             d < DAYS_IN_WEEK * week_line + DAYS_IN_WEEK; d++) {
+
+                               int workday = d >= DAYS_IN_WEEK * week_line + firstwork &&
+                                             d <= DAYS_IN_WEEK * week_line + firstwork + 4;
+
                                if (0 < i->days[d]) {
+                                       fput_seq(workday ? seq_wo_start : seq_we_start);
+
                                        if (reqday == i->days[d])
                                                printf("%*s%s%*d%s",
                                                        skip - (ctl->julian ? 3 : 2),
-                                                       "", Senter, (ctl->julian ? 3 : 2),
-                                                       i->days[d], Sexit);
+                                                       "", cal_get_color_sequence(CAL_COLOR_TODAY), (ctl->julian ? 3 : 2),
+                                                       i->days[d], cal_get_color_disable_sequence(CAL_COLOR_TODAY));
                                        else
                                                printf("%*d", skip, i->days[d]);
+
+                                       fput_seq(workday ? seq_wo_end : seq_we_end);
                                } else
                                        printf("%*s", skip, "");
 
@@ -885,8 +947,11 @@ cal_vert_output_months(struct cal_month *month, const struct cal_control *ctl)
                                        if (reqday == m->days[d]) {
                                                printf("%*s%s%*d%s",
                                                       skip - (ctl->julian ? 3 : 2),
-                                                      "", Senter, (ctl->julian ? 3 : 2),
-                                                      m->days[d], Sexit);
+                                                      "",
+                                                      cal_get_color_sequence(CAL_COLOR_TODAY),
+                                                      (ctl->julian ? 3 : 2),
+                                                      m->days[d],
+                                                      cal_get_color_disable_sequence(CAL_COLOR_TODAY));
                                        } else {
                                                printf("%*d",  skip, m->days[d]);
                                        }
@@ -909,9 +974,10 @@ cal_vert_output_months(struct cal_month *month, const struct cal_control *ctl)
                        if (0 < m->weeks[week]) {
                                if ((ctl->weektype & WEEK_NUM_MASK) == m->weeks[week])
                                        printf("%s%*d%s",
-                                                Senter,
+                                                cal_get_color_sequence(CAL_COLOR_WEEKNUMBER),
                                                 skip - (ctl->julian ? 3 : 2),
-                                                m->weeks[week], Sexit);
+                                                m->weeks[week],
+                                                cal_get_color_disable_sequence(CAL_COLOR_WEEKNUMBER));
                                else
                                        printf("%*d", skip, m->weeks[week]);
                        } else
@@ -951,8 +1017,8 @@ static void monthly(const struct cal_control *ctl)
 
        rows = (ctl->num_months - 1) / ctl->months_in_row;
        for (i = 0; i < rows + 1 ; i++){
-               if (i == rows)
-                       for (int n = 0; n < ctl->num_months % ctl->months_in_row; n++)
+               if (i == rows && ctl->num_months % ctl->months_in_row > 0)
+                       for (int n = (ctl->num_months % ctl->months_in_row) - 1; n < ctl->months_in_row; n++)
                                ms[n].next = NULL;
 
                for (m = ms; m; m = m->next){
@@ -1230,8 +1296,8 @@ static void __attribute__((__noreturn__)) usage(void)
                "                         %s\n", USAGE_COLORS_DEFAULT);
 
        fputs(USAGE_SEPARATOR, out);
-       printf(USAGE_HELP_OPTIONS(23));
-       printf(USAGE_MAN_TAIL("cal(1)"));
+       fprintf(out, USAGE_HELP_OPTIONS(23));
+       fprintf(out, USAGE_MAN_TAIL("cal(1)"));
 
        exit(EXIT_SUCCESS);
 }