add indent filter - lchat - A line oriented chat front end for ii. | |
git clone git://git.suckless.org/lchat | |
Log | |
Files | |
Refs | |
README | |
--- | |
commit ddeeb5b062f0e611a54f1a9af28efc641a3d33af | |
parent dacb5b0a94bc0fed9ffb5a0fd70829ed6e7c2c5b | |
Author: Jan Klemkow <[email protected]> | |
Date: Tue, 15 Nov 2016 09:58:17 +0100 | |
add indent filter | |
Diffstat: | |
M Makefile | 3 +++ | |
A filter/indent.c | 50 +++++++++++++++++++++++++++++… | |
2 files changed, 53 insertions(+), 0 deletions(-) | |
--- | |
diff --git a/Makefile b/Makefile | |
@@ -24,6 +24,9 @@ lchat.o: lchat.c | |
$(CC) -c $(CFLAGS) -D_BSD_SOURCE -D_XOPEN_SOURCE -D_GNU_SOURCE \ | |
-o $@ lchat.c | |
+filter/indent: filter/indent.c | |
+ $(CC) $(CFLAGS) -o $@ filter/indent.c | |
+ | |
sl_test.o: sl_test.c slackline.h | |
$(CC) $(CFLAGS) -c -o $@ sl_test.c | |
diff --git a/filter/indent.c b/filter/indent.c | |
@@ -0,0 +1,50 @@ | |
+#include <err.h> | |
+#include <stdio.h> | |
+#include <stdlib.h> | |
+#include <string.h> | |
+#include <time.h> | |
+ | |
+int | |
+main(int argc, char *argv[]) | |
+{ | |
+ char buf[BUFSIZ]; | |
+ char timestr[BUFSIZ]; | |
+ char *fmt = "%H:%m"; | |
+ char *next, *nick, *word; | |
+ struct tm tm; | |
+ | |
+ while (fgets(buf, sizeof buf, stdin) != NULL) { | |
+ next = strptime(buf, "%Y-%m-%d %H:%M ", &tm); | |
+ | |
+ if (next == NULL || next[0] == '-') { | |
+ fputs(buf, stdout); | |
+ continue; | |
+ } | |
+ | |
+ nick = strsep(&next, ">"); | |
+ nick++; | |
+ next++; | |
+ | |
+ strftime(timestr, sizeof timestr, fmt, &tm); | |
+ | |
+ /* print prompt */ | |
+ printf("%s %*s", timestr, 12, nick); | |
+ | |
+ ssize_t pw = 18; | |
+ ssize_t tw = 104 - pw; | |
+ | |
+ /* print indented text */ | |
+ while ((word = strsep(&next, " ")) != NULL) { | |
+ tw -= strlen(word) + 1; | |
+ if (tw < 0) { | |
+ fputs("\n ", stdout); | |
+ tw = 80 - pw; | |
+ } | |
+ | |
+ fputc(' ', stdout); | |
+ fputs(word, stdout); | |
+ } | |
+ } | |
+ | |
+ return EXIT_SUCCESS; | |
+} |