indent.c - lchat - A line oriented chat front end for ii.
(HTM) git clone git://git.suckless.org/lchat
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
indent.c (1933B)
---
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <time.h>
6 #include <unistd.h>
7
8 #include "../util.h"
9
10 #define color1 37 /* message 1 */
11 #define color2 97 /* message 2 */
12 #define color3 31 /* bell match */
13 #define color4 2 /* meta data */
14
15 int
16 main(void)
17 {
18 char buf[BUFSIZ];
19 char timestr[BUFSIZ];
20 char old_nick[BUFSIZ] = "";
21 char *fmt = "%H:%M";
22 char *next, *nick, *word;
23 int cols = 80; /* terminal width */
24 int color = color1;
25 char *bell_file = ".bellmatch";
26
27 while (fgets(buf, sizeof buf, stdin) != NULL) {
28 time_t time = strtol(buf, &next, 10);
29 struct tm *tm = localtime(&time);
30
31 next++; /* skip space */
32
33 if (next == NULL || next[0] == '-' || time == 0) {
34 printf("\033[%dm%s\033[m", color4, buf);
35 fflush(stdout);
36 continue;
37 }
38
39 nick = strsep(&next, ">");
40 if (next == NULL) {
41 fputs(buf, stdout);
42 fflush(stdout);
43 continue;
44 }
45 nick++; /* skip '<' */
46 next++; /* skip space */
47
48 strftime(timestr, sizeof timestr, fmt, tm);
49
50 /* swap color */
51 if (strcmp(nick, old_nick) != 0)
52 color = color == color1 ? color2 : color1;
53
54 if (access(bell_file, R_OK) == 0 && bell_match(next, bell_file))
55 color = color3;
56
57 /* print prompt */
58 /* HH:MM nnnnnnnnnnnn ttttttttttttt */
59 // e[7;30;40m
60 printf("\033[%dm\033[K%s %*s", color, timestr, 12,
61 strcmp(nick, old_nick) == 0 ? "" : nick);
62
63 strlcpy(old_nick, nick, sizeof old_nick);
64
65 ssize_t pw = 18; /* prompt width */
66 ssize_t tw = cols - pw; /* text width */
67 bool first = true;
68
69 /* print indented text */
70 while ((word = strsep(&next, " ")) != NULL) {
71 tw -= strlen(word) + 1;
72 if (tw < 0 && !first)
73 printf("\n\033[%dm ", color);
74 if (tw < 0)
75 tw = cols - pw - strlen(word);
76
77 fputc(' ', stdout);
78 fputs(word, stdout);
79 first = false;
80 }
81 fputs("\033[0m\033[K", stdout); /* turn color off */
82 fflush(stdout);
83 }
84
85 return EXIT_SUCCESS;
86 }