start using ncurses

For supporting wide characters I'm guided by:
  http://yjlv.blogspot.com/2015/10/displaying-unicode-with-ncurses-in-c.html
This commit is contained in:
Kartik K. Agaram 2022-02-15 17:04:10 -08:00
parent c187d793c6
commit 60ddac472c
2 changed files with 28 additions and 24 deletions

View File

@ -2,7 +2,7 @@ PROG = ncal
SRC = ncal.c calendar.c easter.c SRC = ncal.c calendar.c easter.c
FLAGS = -D_GNU_SOURCE -include bsd/string.h FLAGS = -D_GNU_SOURCE -include bsd/string.h
LIBS += -ltinfo -lbsd LIBS += -ltinfo -lbsd -lncursesw
include config.mk include config.mk

50
ncal.c
View File

@ -35,6 +35,7 @@ __FBSDID("$FreeBSD: head/usr.bin/ncal/ncal.c 326276 2017-11-27 15:37:16Z pfg $")
#include <langinfo.h> #include <langinfo.h>
#include <libgen.h> #include <libgen.h>
#include <locale.h> #include <locale.h>
#include <ncurses.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -563,11 +564,14 @@ main(int argc, char *argv[])
/* And now we finally start to calculate and output calendars. */ /* And now we finally start to calculate and output calendars. */
if (flag_easter) if (flag_easter)
printeaster(y, flag_julian_cal, flag_orthodox); printeaster(y, flag_julian_cal, flag_orthodox);
else else {
initscr();
if (flag_backward) if (flag_backward)
monthrangeb(y, m, flag_julian_day, before, after, highlightdate); monthrangeb(y, m, flag_julian_day, before, after, highlightdate);
else else
monthranger(y, m, flag_julian_day, before, after, highlightdate); monthranger(y, m, flag_julian_day, before, after, highlightdate);
getch();
}
return (0); 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 */ /* Empty line between two rows of months */
if (m != m1) if (m != m1)
wprintf(L"\n"); printw("\n");
/* Year at the top. */ /* Year at the top. */
if (printyearheader && M2Y(m) != prevyear) { if (printyearheader && M2Y(m) != prevyear) {
sprintf(s, "%d", M2Y(m)); 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); prevyear = M2Y(m);
} }
/* Month names. */ /* Month names. */
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
if (printyearheader) if (printyearheader)
wprintf(L"%-*ls ", printw("%-*ls ",
mw, wcenter(ws, year[i].name, mw)); mw, wcenter(ws, year[i].name, mw));
else { else {
swprintf(ws, sizeof(ws)/sizeof(ws[0]), swprintf(ws, sizeof(ws)/sizeof(ws[0]),
L"%-ls %d", year[i].name, M2Y(m + i)); 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. */ /* Day of the week names. */
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
if (flag_weeks) 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[6], wdss, wds.names[0],
wdss, wds.names[1], wdss, wds.names[2], wdss, wds.names[1], wdss, wds.names[2],
wdss, wds.names[3], wdss, wds.names[4], wdss, wds.names[3], wdss, wds.names[4],
wdss, wds.names[5]); wdss, wds.names[5]);
else 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[6], wdss, wds.names[0],
wdss, wds.names[1], wdss, wds.names[2], wdss, wds.names[1], wdss, wds.names[2],
wdss, wds.names[3], wdss, wds.names[4], wdss, wds.names[3], wdss, wds.names[4],
wdss, wds.names[5]); wdss, wds.names[5]);
} }
wprintf(L"\n"); printw("\n");
/* And the days of the month. */ /* And the days of the month. */
for (i = 0; i != 6; i++) { for (i = 0; i != 6; i++) {
for (j = 0; j < count; j++) for (j = 0; j < count; j++)
wprintf(L"%-*s ", printw("%-*s ",
MW(mw, year[j].extralen[i]), MW(mw, year[j].extralen[i]),
year[j].lines[i]+1); year[j].lines[i]+1);
wprintf(L"\n"); printw("\n");
} }
m += mpl; 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. */ /* Empty line between two rows of months. */
if (m != m1) if (m != m1)
wprintf(L"\n"); printw("\n");
/* Year at the top. */ /* Year at the top. */
if (printyearheader && M2Y(m) != prevyear) { if (printyearheader && M2Y(m) != prevyear) {
sprintf(s, "%d", M2Y(m)); 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); prevyear = M2Y(m);
} }
/* Month names. */ /* Month names. */
wprintf(L" "); printw(" ");
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
if (printyearheader) if (printyearheader)
wprintf(L"%-*ls", mw, year[i].name); printw("%-*ls", mw, year[i].name);
else else
wprintf(L"%-ls %-*d", year[i].name, printw("%-ls %-*d", year[i].name,
mw - wcslen(year[i].name) - 1, M2Y(m + i)); mw - wcslen(year[i].name) - 1, M2Y(m + i));
wprintf(L"\n"); printw("\n");
/* And the days of the month. */ /* And the days of the month. */
for (i = 0; i != 7; i++) { for (i = 0; i != 7; i++) {
/* Week day */ /* Week day */
wprintf(L"%.2ls", wds.names[i]); printw("%.2ls", wds.names[i]);
/* Full months */ /* Full months */
for (j = 0; j < count; j++) for (j = 0; j < count; j++)
wprintf(L"%-*s", printw("%-*s",
MW(mw, year[j].extralen[i]), MW(mw, year[j].extralen[i]),
year[j].lines[i]); year[j].lines[i]);
wprintf(L"\n"); printw("\n");
} }
/* Week numbers. */ /* Week numbers. */
if (flag_weeks) { if (flag_weeks) {
wprintf(L" "); printw(" ");
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
wprintf(L"%-*s", mw, year[i].weeks); printw("%-*s", mw, year[i].weeks);
wprintf(L"\n"); printw("\n");
} }
m += mpl; m += mpl;