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