mscan: adaptive date display

pull/1/merge
Christian Neukirchen 8 years ago
parent 444674ecbc
commit 9d8783df4d

@ -6,6 +6,7 @@
.Nd print a one line per message mail listing .Nd print a one line per message mail listing
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl I
.Ar msgs\ ... .Ar msgs\ ...
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
@ -29,6 +30,13 @@ the sequence number (if applicable),
the date, the date,
the originator, the originator,
and the subject of the message. and the subject of the message.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl I
Force ISO date output.
Use twice to force ISO date and time output.
By default, an adaptive date display is used.
.Sh MESSAGE FLAGS .Sh MESSAGE FLAGS
.Bl -tag -width 2n -compact .Bl -tag -width 2n -compact
.It Li \&> .It Li \&>

@ -25,6 +25,10 @@ static char *cur;
static char *aliases[32]; static char *aliases[32];
static int alias_idx; static int alias_idx;
static int Iflag;
static int curyear;
static int curyday;
void void
u8putstr(FILE *out, char *s, ssize_t l, int pad) u8putstr(FILE *out, char *s, ssize_t l, int pad)
{ {
@ -69,6 +73,8 @@ itsme(char *v)
void void
oneline(char *file) oneline(char *file)
{ {
int metawidth = 38;
static int init; static int init;
if (!init) { if (!init) {
// delay loading of the seq until we need to scan the first // delay loading of the seq until we need to scan the first
@ -92,8 +98,8 @@ oneline(char *file)
struct message *msg = blaze822(file); struct message *msg = blaze822(file);
if (!msg) { if (!msg) {
int p = cols-38-3-indent; int p = cols-metawidth-3-indent;
printf("%*.*s\\_ %*.*s\n", -38 - indent, 38 + indent, "", printf("%*.*s\\_ %*.*s\n", -metawidth - indent, metawidth + indent, "",
-p, p, file); -p, p, file);
return; return;
} }
@ -120,7 +126,7 @@ oneline(char *file)
else else
flag2 = ' '; flag2 = ' ';
char date[16]; char date[32];
char *v; char *v;
if ((v = blaze822_hdr(msg, "date"))) { if ((v = blaze822_hdr(msg, "date"))) {
@ -128,7 +134,17 @@ oneline(char *file)
if (t != -1) { if (t != -1) {
struct tm *tm; struct tm *tm;
tm = localtime(&t); tm = localtime(&t);
strftime(date, sizeof date, "%Y-%m-%d", tm);
if (Iflag > 1) {
strftime(date, sizeof date,
"%Y-%m-%d %H:%M:%S", tm);
metawidth += 9;
} else if (Iflag || tm->tm_year != curyear)
strftime(date, sizeof date, "%Y-%m-%d", tm);
else if (tm->tm_yday != curyday)
strftime(date, sizeof date, "%a %b %e", tm);
else
strftime(date, sizeof date, "%a %H:%M", tm);
} else { } else {
strcpy(date, "(invalid)"); strcpy(date, "(invalid)");
} }
@ -191,7 +207,7 @@ oneline(char *file)
for (z = 0; z < indent; z++) for (z = 0; z < indent; z++)
printf(" "); printf(" ");
} }
u8putstr(stdout, subjdec, cols-38-indent, 0); u8putstr(stdout, subjdec, cols-metawidth-indent, 0);
printf("\n"); printf("\n");
blaze822_free(msg); blaze822_free(msg);
@ -200,6 +216,22 @@ oneline(char *file)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int c;
while ((c = getopt(argc, argv, "I")) != -1)
switch(c) {
case 'I': Iflag++; break;
default:
fprintf(stderr, "Usage: mscan [-I] [msgs...]\n");
exit(1);
}
time_t now = time(0);
struct tm *tm = localtime(&now);
curyear = tm->tm_year;
curyday = tm->tm_yday;
setenv("TZ", "", 1);
tzset();
setlocale(LC_ALL, ""); // for wcwidth later setlocale(LC_ALL, ""); // for wcwidth later
if (wcwidth(0xFFFD) > 0) if (wcwidth(0xFFFD) > 0)
replacement = 0xFFFD; replacement = 0xFFFD;
@ -230,10 +262,10 @@ main(int argc, char *argv[])
} }
long i; long i;
if (argc == 1 && isatty(0)) if (argc == optind && isatty(0))
i = blaze822_loop1(":", oneline); i = blaze822_loop1(":", oneline);
else else
i = blaze822_loop(argc-1, argv+1, oneline); i = blaze822_loop(argc-optind, argv+optind, oneline);
fprintf(stderr, "%ld mails scanned\n", i); fprintf(stderr, "%ld mails scanned\n", i);
return 0; return 0;

Loading…
Cancel
Save