#include "optutils.h"
#include "timeutils.h"
#include "ttyutils.h"
+#include "xalloc.h"
#define DOY_MONTH_WIDTH 27 /* -j month width */
#define DOM_MONTH_WIDTH 20 /* month width */
-static int has_term = 0;
-static const char *Senter = "", *Sexit = ""; /* enter and exit standout mode */
-
-#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
-# if defined(HAVE_NCURSESW_TERM_H)
-# include <ncursesw/term.h>
-# elif defined(HAVE_NCURSES_TERM_H)
-# include <ncurses/term.h>
-# elif defined(HAVE_TERM_H)
-# include <term.h>
-# endif
-#endif
+enum {
+ CAL_COLOR_TODAY,
+ CAL_COLOR_HEADER,
+ CAL_COLOR_WEEKNUMBER,
+ CAL_COLOR_WORKDAY,
+ CAL_COLOR_WEEKEND
+};
-static int setup_terminal(char *term
-#if !defined(HAVE_LIBNCURSES) && !defined(HAVE_LIBNCURSESW)
- __attribute__((__unused__))
-#endif
- )
+static const struct { const char * const scheme; const char * dflt; } colors[] =
{
-#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
- int ret;
+ [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", "" }
+};
- if (setupterm(term, STDOUT_FILENO, &ret) != 0 || ret != 1)
- return -1;
-#endif
- return 0;
+static inline void cal_enable_color(int id)
+{
+ color_scheme_enable(colors[id].scheme, colors[id].dflt);
}
-static const char *my_tgetstr(char *ss
-#if !defined(HAVE_LIBNCURSES) && !defined(HAVE_LIBNCURSESW)
- __attribute__((__unused__))
-#endif
- )
+static inline const char *cal_get_color_sequence(int id)
{
- const char *ret = NULL;
+ return color_scheme_get_sequence(colors[id].scheme, colors[id].dflt);
+}
-#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
- if (has_term)
- ret = tigetstr(ss);
-#endif
- if (!ret || ret == (char *)-1)
+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 "";
- return ret;
}
#include "widechar.h"
WEEK_NUM_US=0x200,
};
+enum {
+ COLUMNS_MAX_THREE = -1,
+ COLUMNS_AUTO = -2,
+};
+
/* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */
static char day_headings[(WEEK_LEN + 1) * 6 + 1];
int main(int argc, char **argv)
{
struct tm local_time;
- char *term;
time_t now;
- int ch = 0, yflag = 0, Yflag = 0;
+ int ch = 0, yflag = 0, Yflag = 0, cols = COLUMNS_MAX_THREE;
static struct cal_control ctl = {
.reform_year = DEFAULT_REFORM_YEAR,
{"twelve", no_argument, NULL, 'Y'},
{"help", no_argument, NULL, 'h'},
{"vertical", no_argument, NULL,'v'},
+ {"columns", required_argument, NULL,'c'},
{NULL, 0, NULL, 0}
};
textdomain(PACKAGE);
close_stdout_atexit();
- term = getenv("TERM");
- if (term) {
- has_term = setup_terminal(term) == 0;
- if (has_term) {
- Senter = my_tgetstr("smso");
- Sexit = my_tgetstr("rmso");
- }
- }
-
/*
* The traditional Unix cal utility starts the week at Sunday,
* while ISO 8601 starts at Monday. We read the start day from
ctl.weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % DAYS_IN_WEEK;
}
#endif
- while ((ch = getopt_long(argc, argv, "13mjn:sSywYvVh", longopts, NULL)) != -1) {
+ while ((ch = getopt_long(argc, argv, "13mjn:sSywYvc:Vh", longopts, NULL)) != -1) {
err_exclusive_options(ch, longopts, excl, excl_st);
case 'v':
ctl.vertical = 1;
break;
+ case 'c':
+ if (strcmp(optarg, "auto") == 0)
+ cols = COLUMNS_AUTO;
+ else
+ cols = strtosize_or_err(optarg,
+ _("failed to parse columns"));
+ break;
case 'V':
print_version(EXIT_SUCCESS);
case 'h':
headers_init(&ctl);
if (colors_init(ctl.colormode, "cal") == 0) {
- /*
- * If standout mode available (Senter and Sexit are set) and
- * user or terminal-colors.d do not disable colors than
- * ignore colors_init().
- */
- if (*Senter && *Sexit && colors_mode() != UL_COLORMODE_NEVER) {
- /* let use standout mode */
- ;
- } else {
- /* disable */
- Senter = ""; Sexit = "";
- ctl.req.day = 0;
- ctl.weektype &= ~WEEK_NUM_MASK;
- }
+ /* disable */
+ ctl.req.day = 0;
+ ctl.weektype &= ~WEEK_NUM_MASK;
}
if (yflag || Yflag) {
if (ctl.num_months > 1 && ctl.months_in_row == 0) {
ctl.months_in_row = MONTHS_IN_YEAR_ROW; /* default */
- if (isatty(STDOUT_FILENO)) {
+ if (cols > 0)
+ ctl.months_in_row = cols;
+ else if (isatty(STDOUT_FILENO)) {
int w, mw, extra, new_n;
w = get_terminal_width(80);
extra = ((w / mw) - 1) * ctl.gutter_width;
new_n = (w - extra) / mw;
- if (new_n < MONTHS_IN_YEAR_ROW)
+ switch (cols) {
+ case COLUMNS_MAX_THREE:
+ if (new_n < MONTHS_IN_YEAR_ROW)
+ ctl.months_in_row = new_n > 0 ? new_n : 1;
+ break;
+ case COLUMNS_AUTO:
ctl.months_in_row = new_n > 0 ? new_n : 1;
+ break;
+ }
}
} else if (!ctl.months_in_row)
ctl.months_in_row = 1;
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;
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]);
if (i->next != NULL)
printf("%*s", ctl->gutter_width, "");
}
+ cal_disable_color(CAL_COLOR_HEADER);
fputc('\n', stdout);
}
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) {
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
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, "");
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]);
}
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
static void monthly(const struct cal_control *ctl)
{
- struct cal_month m1,m2,m3, *m;
+ struct cal_month *m, *ms;
int i, rows, month = ctl->req.start_month ? ctl->req.start_month : ctl->req.month;
int32_t year = ctl->req.year;
month = new_month;
}
- m1.next = (ctl->months_in_row > 1) ? &m2 : NULL;
- m2.next = (ctl->months_in_row > 2) ? &m3 : NULL;
- m3.next = NULL;
+ ms = xcalloc(ctl->months_in_row, sizeof(*ms));
+
+ for (i = 0; i < ctl->months_in_row - 1; i++)
+ ms[i].next = &ms[i + 1];
rows = (ctl->num_months - 1) / ctl->months_in_row;
for (i = 0; i < rows + 1 ; i++){
- if (i == rows){
- switch (ctl->num_months % ctl->months_in_row){
- case 1:
- m1.next = NULL;
- /* fallthrough */
- case 2:
- m2.next = NULL;
- /* fallthrough */
- }
- }
- for (m = &m1; m; m = m->next){
+ 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){
m->month = month++;
m->year = year;
if (MONTHS_IN_YEAR < month) {
if (i > 0)
fputc('\n', stdout); /* Add a line between row */
- cal_vert_output_header(&m1, ctl);
- cal_vert_output_months(&m1, ctl);
+ cal_vert_output_header(ms, ctl);
+ cal_vert_output_months(ms, ctl);
} else {
- cal_output_header(&m1, ctl);
- cal_output_months(&m1, ctl);
+ cal_output_header(ms, ctl);
+ cal_output_months(ms, ctl);
}
}
+ free(ms);
}
static void yearly(const struct cal_control *ctl)
fputs(_(" -Y, --twelve show the next twelve months\n"), out);
fputs(_(" -w, --week[=<num>] show US or ISO-8601 week numbers\n"), out);
fputs(_(" -v, --vertical show day vertically instead of line\n"), out);
+ fputs(_(" -c, --columns <width> amount of columns to use\n"), out);
fprintf(out,
_(" --color[=<when>] colorize messages (%s, %s or %s)\n"), "auto", "always", "never");
fprintf(out,
" %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);
}