Add "-wt" option to size calendar according to terminal window.

This commit is contained in:
Dianne Skoll
2022-09-24 10:26:06 -04:00
parent 74a3b3e73d
commit a8aa3b328b
2 changed files with 39 additions and 7 deletions

View File

@@ -127,7 +127,19 @@ The \fB\-w\fR option specifies the output width, padding and spacing
of the formatted calendar output. \fICol\fR specifies the number of
columns in the output device. If not specified, or specified as 0,
it defaults to the larger of 71 or the actual width of your terminal,
or to 80 if standard output is not a terminal.
or to 80 if standard output is not a terminal. If specified as the letter
\fBt\fR, then \fBRemind\fR attempts to get the width of the
\fB/dev/tty\fR terminal device. This is useful, for example, if you
pipe calendar output into \fBless\fR; even though standard output
is a pipe, you want the calendar to be sized correctly for your
terminal window:
.RS
.PP
.nf
remind -c -wt .reminders | less
.fi
.RE
.RS
.PP
\fIPad\fR specifies how many lines

View File

@@ -21,6 +21,10 @@
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/ioctl.h>
@@ -142,13 +146,14 @@ void InitRemind(int argc, char const *argv[])
int weeks;
int x;
int jul;
int ttyfd;
struct winsize w;
jul = NO_DATE;
/* If stdout is a terminal, initialize $FormWidth to terminal width-8,
but clamp to [20, 500] */
if (isatty(STDOUT_FILENO)) {
struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) {
FormWidth = w.ws_col - 8;
if (FormWidth < 20) FormWidth = 20;
@@ -510,11 +515,26 @@ void InitRemind(int argc, char const *argv[])
case 'w':
case 'W':
if (*arg != ',') {
PARSENUM(CalWidth, arg);
if (CalWidth != 0 && CalWidth < 71) CalWidth = 71;
if (CalWidth == 0) {
CalWidth = -1;
}
if (*arg == 't') {
arg++;
/* -wt means get width from /dev/tty */
ttyfd = open("/dev/tty", O_RDONLY);
if (!ttyfd) {
fprintf(stderr, "%s: `-wt': Cannot open /dev/tty: %s\n",
argv[0], strerror(errno));
} else {
if (ioctl(ttyfd, TIOCGWINSZ, &w) == 0) {
CalWidth = w.ws_col;
}
close(ttyfd);
}
} else {
PARSENUM(CalWidth, arg);
if (CalWidth != 0 && CalWidth < 71) CalWidth = 71;
if (CalWidth == 0) {
CalWidth = -1;
}
}
}
if (*arg == ',') {
arg++;