commit 84eec82dccf223861142f19ca57b410ba414c98a
parent c857fde6775979c260b7b56c5271efee089d7722
Author: Jan Klemkow <j.klemkow@wemelug.de>
Date: Tue, 13 Mar 2018 23:46:24 +0100
add special color for bellmatch in text part of chat msg
Diffstat:
4 files changed, 63 insertions(+), 3 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,4 +1,4 @@
-include config.mk
+#include config.mk
.PHONY: all install filter clean test
@@ -21,8 +21,8 @@ lchat.o: lchat.c
-o $@ lchat.c
filter: filter/indent
-filter/indent: filter/indent.c
- $(CC) $(CFLAGS) -o $@ filter/indent.c
+filter/indent: filter/indent.c util.o util.h
+ $(CC) $(CFLAGS) -o $@ filter/indent.c util.o
sl_test.o: sl_test.c slackline.h
$(CC) $(CFLAGS) -c -o $@ sl_test.c
@@ -32,3 +32,6 @@ sl_test: sl_test.o slackline.o slackline.h
slackline.o: slackline.c slackline.h
$(CC) -c $(CFLAGS) -o $@ slackline.c
+
+util.o: util.c util.h
+ $(CC) -c $(CFLAGS) -o $@ util.c
diff --git a/filter/indent.c b/filter/indent.c
@@ -6,8 +6,11 @@
#include <time.h>
#include <unistd.h>
+#include "../util.h"
+
#define color1 34
#define color2 33
+#define color3 35
int
main(void)
@@ -19,6 +22,7 @@ main(void)
char *next, *nick, *word;
int cols = 80; /* terminal width */
int color = color1;
+ char *bell_file = ".bellmatch";
while (fgets(buf, sizeof buf, stdin) != NULL) {
time_t time = strtol(buf, &next, 10);
@@ -42,6 +46,9 @@ main(void)
if (strcmp(nick, old_nick) != 0)
color = color == color1 ? color2 : color1;
+ if (access(bell_file, R_OK) == 0 && bell_match(next, bell_file))
+ color = color3;
+
/* print prompt */
/* HH:MM nnnnnnnnnnnn ttttttttttttt */
// e[7;30;40m
diff --git a/util.c b/util.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2018 Jan Klemkow <j.klemkow@wemelug.de>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <err.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+bool
+bell_match(const char *str, const char *regex_file)
+{
+ FILE *fh = NULL;
+ char cmd[BUFSIZ];
+
+ if (access(regex_file, R_OK) == -1)
+ return true;
+
+ snprintf(cmd, sizeof cmd, "exec grep -qf %s", regex_file);
+
+ if ((fh = popen(cmd, "w")) == NULL)
+ err(EXIT_FAILURE, "popen");
+
+ if (fputs(str, fh) == EOF)
+ err(EXIT_FAILURE, "fputs");
+
+ if (pclose(fh) == 0)
+ return true;
+
+ return false;
+}
diff --git a/util.h b/util.h
@@ -0,0 +1,6 @@
+#ifndef _UTIL_H_
+#define _UTIL_H_
+
+bool bell_match(const char *str, const char *regex_file);
+
+#endif