Implement highlighting of today in month view of cal/ncal just like

gnu cal does. This is currently disabled for year view because of hard
coded padding in that case. This will hopefully be fixed soon.

Reviewed by:	Simon 'corecode' Schubert <corecode fs.ei.tum.de>
Approved by:	ed
This commit is contained in:
Roman Divacky 2009-04-20 18:19:38 +00:00
parent 0f5d117e8c
commit e454a17158
2 changed files with 82 additions and 14 deletions

View File

@ -3,7 +3,7 @@
PROG= ncal
DPADD= ${LIBCALENDAR}
LDADD= -lcalendar
LDADD= -lcalendar -ltermcap
WARNS?= 1
LINKS= ${BINDIR}/ncal ${BINDIR}/cal

View File

@ -42,6 +42,8 @@ static const char rcsid[] =
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
#include <term.h>
#undef lines /* term.h defines this */
/* Width of one month with backward compatibility */
#define MONTH_WIDTH_B_J 27
@ -50,7 +52,7 @@ static const char rcsid[] =
#define MONTH_WIDTH_J 24
#define MONTH_WIDTH 18
#define MAX_WIDTH 28
#define MAX_WIDTH 64
typedef struct date date;
@ -159,6 +161,8 @@ char jdaystr[] = " 1 2 3 4 5 6 7 8 9"
int flag_weeks; /* user wants number of week */
int nswitch; /* user defined switch date */
int nswitchb; /* switch date for backward compatibility */
const char *term_r, *term_e;
int today;
char *center(char *s, char *t, int w);
wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
@ -199,6 +203,26 @@ main(int argc, char *argv[])
char *cp; /* character pointer */
char *flag_month = NULL; /* requested month as string */
const char *locale; /* locale to get country code */
char tbuf[1024], cbuf[512], *b;
time_t t;
struct tm *tm1;
term_e = term_r = NULL;
today = 0;
if (isatty(1) && tgetent(tbuf, getenv("TERM")) == 1) {
date dt; /* handy date */
b = cbuf;
term_r = tgetstr("mr", &b);
term_e = tgetstr("me", &b);
t = time(NULL);
tm1 = localtime(&t);
dt.y = tm1->tm_year + 1900;
dt.m = tm1->tm_mon + 1;
dt.d = tm1->tm_mday;
today = sndaysb(&dt);
}
/*
* Use locale to determine the country code,
@ -334,12 +358,14 @@ main(int argc, char *argv[])
if (flag_easter)
printeaster(y, flag_julian_cal, flag_orthodox);
else if (argc == 1 || flag_hole_year)
else if (argc == 1 || flag_hole_year) {
/* disable the highlight for now */
today = 0;
if (flag_backward)
printyearb(y, flag_julian_day);
else
printyear(y, flag_julian_day);
else
} else
if (flag_backward)
printmonthb(y, m, flag_julian_day);
else
@ -592,7 +618,7 @@ mkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
int dw; /* width of numbers */
int first; /* first day of month */
int firstm; /* first day of first week of month */
int i, j, k; /* just indices */
int i, j, k, l; /* just indices */
int last; /* the first day of next month */
int jan1 = 0; /* the first day of this year */
char *ds; /* pointer to day strings (daystr or
@ -640,17 +666,38 @@ mkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
* column is one day number. print column index: k.
*/
for (i = 0; i != 7; i++) {
for (j = firstm + i, k = 0; j < last; j += 7, k += dw)
l = 0;
for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
if (j == today && (term_r != NULL && term_e != NULL)) {
l = strlen(term_r);
if (jd_flag)
dt.d = j - jan1 + 1;
else
sdateb(j, &dt);
/* separator */
mlines->lines[i][k] = ' ';
/* the actual text */
memcpy(mlines->lines[i] + k + l,
ds + dt.d * dw, dw);
/* highlight on */
memcpy(mlines->lines[i] + k + 1, term_r, l);
/* highlight off */
memcpy(mlines->lines[i] + k + l + dw, term_e,
strlen(term_e));
l = strlen(term_e) + strlen(term_r);
continue;
}
if (j >= first) {
if (jd_flag)
dt.d = j - jan1 + 1;
else
sdate(j, &dt);
memcpy(mlines->lines[i] + k,
memcpy(mlines->lines[i] + k + l,
ds + dt.d * dw, dw);
} else
memcpy(mlines->lines[i] + k, " ", dw);
mlines->lines[i][k] = '\0';
memcpy(mlines->lines[i] + k + l, " ", dw);
}
mlines->lines[i][k + l] = '\0';
}
@ -676,7 +723,7 @@ mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
int dw; /* width of numbers */
int first; /* first day of month */
int firsts; /* sunday of first week of month */
int i, j, k; /* just indices */
int i, j, k, l; /* just indices */
int jan1 = 0; /* the first day of this year */
int last; /* the first day of next month */
char *ds; /* pointer to day strings (daystr or
@ -737,21 +784,42 @@ mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
* column is one day number. print column index: k.
*/
for (i = 0; i != 6; i++) {
l = 0;
for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
j++, k += dw)
j++, k += dw) {
if (j == today && (term_r != NULL && term_e != NULL)) {
l = strlen(term_r);
if (jd_flag)
dt.d = j - jan1 + 1;
else
sdateb(j, &dt);
/* separator */
mlines->lines[i][k] = ' ';
/* the actual text */
memcpy(mlines->lines[i] + k + l,
ds + dt.d * dw, dw);
/* highlight on */
memcpy(mlines->lines[i] + k + 1, term_r, l);
/* highlight off */
memcpy(mlines->lines[i] + k + l + dw, term_e,
strlen(term_e));
l = strlen(term_e) + strlen(term_r);
continue;
}
if (j >= first) {
if (jd_flag)
dt.d = j - jan1 + 1;
else
sdateb(j, &dt);
memcpy(mlines->lines[i] + k,
memcpy(mlines->lines[i] + k + l,
ds + dt.d * dw, dw);
} else
memcpy(mlines->lines[i] + k, " ", dw);
memcpy(mlines->lines[i] + k + l, " ", dw);
}
if (k == 0)
mlines->lines[i][1] = '\0';
else
mlines->lines[i][k] = '\0';
mlines->lines[i][k + l] = '\0';
}
}