From 60ddac472c76a69bbec54299199842e67abaf992 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 15 Feb 2022 17:04:10 -0800 Subject: [PATCH] start using ncurses For supporting wide characters I'm guided by: http://yjlv.blogspot.com/2015/10/displaying-unicode-with-ncurses-in-c.html --- Makefile | 2 +- ncal.c | 50 +++++++++++++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 137b0ca..de27a74 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ PROG = ncal SRC = ncal.c calendar.c easter.c FLAGS = -D_GNU_SOURCE -include bsd/string.h -LIBS += -ltinfo -lbsd +LIBS += -ltinfo -lbsd -lncursesw include config.mk diff --git a/ncal.c b/ncal.c index 0e910ef..5c82e1b 100644 --- a/ncal.c +++ b/ncal.c @@ -35,6 +35,7 @@ __FBSDID("$FreeBSD: head/usr.bin/ncal/ncal.c 326276 2017-11-27 15:37:16Z pfg $") #include #include #include +#include #include #include #include @@ -563,11 +564,14 @@ main(int argc, char *argv[]) /* And now we finally start to calculate and output calendars. */ if (flag_easter) printeaster(y, flag_julian_cal, flag_orthodox); - else + else { + initscr(); if (flag_backward) monthrangeb(y, m, flag_julian_day, before, after, highlightdate); else monthranger(y, m, flag_julian_day, before, after, highlightdate); + getch(); + } return (0); } @@ -701,51 +705,51 @@ monthrangeb(int y, int m, int jd_flag, int before, int after, int highlightdate) /* Empty line between two rows of months */ if (m != m1) - wprintf(L"\n"); + printw("\n"); /* Year at the top. */ if (printyearheader && M2Y(m) != prevyear) { sprintf(s, "%d", M2Y(m)); - wprintf(L"%s\n", center(t, s, mpl * mw)); + printw("%s\n", center(t, s, mpl * mw)); prevyear = M2Y(m); } /* Month names. */ for (i = 0; i < count; i++) if (printyearheader) - wprintf(L"%-*ls ", + printw("%-*ls ", mw, wcenter(ws, year[i].name, mw)); else { swprintf(ws, sizeof(ws)/sizeof(ws[0]), L"%-ls %d", year[i].name, M2Y(m + i)); - wprintf(L"%-*ls ", mw, wcenter(ws1, ws, mw)); + printw("%-*ls ", mw, wcenter(ws1, ws, mw)); } - wprintf(L"\n"); + printw("\n"); /* Day of the week names. */ for (i = 0; i < count; i++) { if (flag_weeks) - wprintf(L" w| %s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ", + printw(" w| %s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ", wdss, wds.names[6], wdss, wds.names[0], wdss, wds.names[1], wdss, wds.names[2], wdss, wds.names[3], wdss, wds.names[4], wdss, wds.names[5]); else - wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ", + printw("%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ", wdss, wds.names[6], wdss, wds.names[0], wdss, wds.names[1], wdss, wds.names[2], wdss, wds.names[3], wdss, wds.names[4], wdss, wds.names[5]); } - wprintf(L"\n"); + printw("\n"); /* And the days of the month. */ for (i = 0; i != 6; i++) { for (j = 0; j < count; j++) - wprintf(L"%-*s ", + printw("%-*s ", MW(mw, year[j].extralen[i]), year[j].lines[i]+1); - wprintf(L"\n"); + printw("\n"); } m += mpl; @@ -800,44 +804,44 @@ monthranger(int y, int m, int jd_flag, int before, int after, int highlightdate) /* Empty line between two rows of months. */ if (m != m1) - wprintf(L"\n"); + printw("\n"); /* Year at the top. */ if (printyearheader && M2Y(m) != prevyear) { sprintf(s, "%d", M2Y(m)); - wprintf(L"%s\n", center(t, s, mpl * mw)); + printw("%s\n", center(t, s, mpl * mw)); prevyear = M2Y(m); } /* Month names. */ - wprintf(L" "); + printw(" "); for (i = 0; i < count; i++) if (printyearheader) - wprintf(L"%-*ls", mw, year[i].name); + printw("%-*ls", mw, year[i].name); else - wprintf(L"%-ls %-*d", year[i].name, + printw("%-ls %-*d", year[i].name, mw - wcslen(year[i].name) - 1, M2Y(m + i)); - wprintf(L"\n"); + printw("\n"); /* And the days of the month. */ for (i = 0; i != 7; i++) { /* Week day */ - wprintf(L"%.2ls", wds.names[i]); + printw("%.2ls", wds.names[i]); /* Full months */ for (j = 0; j < count; j++) - wprintf(L"%-*s", + printw("%-*s", MW(mw, year[j].extralen[i]), year[j].lines[i]); - wprintf(L"\n"); + printw("\n"); } /* Week numbers. */ if (flag_weeks) { - wprintf(L" "); + printw(" "); for (i = 0; i < count; i++) - wprintf(L"%-*s", mw, year[i].weeks); - wprintf(L"\n"); + printw("%-*s", mw, year[i].weeks); + printw("\n"); } m += mpl;