commit dc379ae7fa5c10ac69d0681d6ef2aa3020ea982e
parent b35d68c8a2e204afecf0496e7c39893da7020582
Author: Jan Klemkow <j.klemkow@wemelug.de>
Date: Wed, 22 Feb 2017 16:54:20 +0100
indent: fix some bugs
Diffstat:
1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/filter/indent.c b/filter/indent.c
@@ -1,17 +1,21 @@
#include <err.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
+#include <unistd.h>
int
-main(int argc, char *argv[])
+main(void)
{
char buf[BUFSIZ];
char timestr[BUFSIZ];
+ char old_nick[BUFSIZ] = "";
char *fmt = "%H:%M";
char *next, *nick, *word;
struct tm tm;
+ int cols = 80; /* terminal width */
while (fgets(buf, sizeof buf, stdin) != NULL) {
next = strptime(buf, "%Y-%m-%d %H:%M ", &tm);
@@ -29,23 +33,31 @@ main(int argc, char *argv[])
strftime(timestr, sizeof timestr, fmt, &tm);
/* print prompt */
- printf("%s %*s", timestr, 12, nick);
+ /* HH:MM nnnnnnnnnnnn ttttttttttttt */
+ printf("%s %*s", timestr, 12,
+ strcmp(nick, old_nick) == 0 ? "" : nick);
- ssize_t pw = 18;
- ssize_t tw = 104 - pw;
+ strlcpy(old_nick, nick, sizeof old_nick);
+
+ ssize_t pw = 18; /* prompt width */
+ ssize_t tw = cols - pw; /* text width */
+ bool first = true;
/* print indented text */
while ((word = strsep(&next, " ")) != NULL) {
tw -= strlen(word) + 1;
- if (tw < 0) {
+ if (tw < 0 && !first) {
fputs("\n ", stdout);
- tw = 80 - pw;
+ tw = cols - pw;
+ first = true;
}
fputc(' ', stdout);
fputs(word, stdout);
- fflush(stdout);
+ first = false;
}
+
+ fflush(stdout);
}
return EXIT_SUCCESS;