]> Pileus Git - ~andy/lamechat/commitdiff
Add watch regex.
authorAndy Spencer <andy753421@gmail.com>
Mon, 30 Jul 2018 09:13:20 +0000 (09:13 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 30 Jul 2018 09:53:33 +0000 (09:53 +0000)
view.c

diff --git a/view.c b/view.c
index 89805e297ff7b2e29e20e7e9a0212c751596a078..9dded45823aac374c5e4d92f955480f5e9aa54eb 100644 (file)
--- a/view.c
+++ b/view.c
@@ -36,6 +36,8 @@
 #include "view.h"
 
 /* View constants */
+#define MATCHES    16
+#define RE_MFLAGS  (REG_EXTENDED)
 #define RE_FLAGS   (REG_EXTENDED|REG_NOSUB)
 
 /* Extra colors */
@@ -88,6 +90,9 @@ typedef struct {
        int pick;
        int prow;
        int pcol;
+       int mcnt;
+       int mstart[MATCHES];
+       int mstop[MATCHES];
 } printer_t;
 
 typedef enum {
@@ -114,6 +119,7 @@ static int    deadline;
 static int    theme;
 static int    abbrev;
 static int    draw;
+static char  *watch;
 
 static char   cmd_buf[4096];
 static int    cmd_pos;
@@ -125,6 +131,8 @@ static int    hdr_lines;
 static int    sts_lines;
 static int    cmd_lines;
 
+static regex_t    regex;
+
 static server_t   sys_srv;
 static channel_t  sys_chan;
 static window_t   sys_win;
@@ -136,6 +144,7 @@ static int   ncolors = 1;
 static short colors[256+1][256+1];
 static short color_title;
 static short color_date;
+static short color_watch;
 static short color_error;
 
 static const char *themes[] = {
@@ -273,6 +282,19 @@ static int str_color(const char *str)
        return color(x, -1);
 }
 
+static int match_all(const char *str, regex_t *preg, int *mstart, int *mstop)
+{
+       int si = 0, mi = 0;
+       regmatch_t pmatch = {};
+       while (mi < MATCHES && str[si] && !regexec(preg, &str[si], 1, &pmatch, 0)) {
+               mstart[mi] = si + pmatch.rm_so;
+               mstop[mi] = si + pmatch.rm_eo - 1;
+               si += pmatch.rm_eo;
+               mi += 1;
+       }
+       return mi;
+}
+
 /* Window functions */
 static int in_window(message_t *msg, window_t *win)
 {
@@ -563,6 +585,7 @@ static int irc_color(const char *txt, int *i)
 static void print_string(printer_t *pr, const char *txt)
 {
        int i, n;
+       int mi = 0;
        wchar_t wc;
        const char *l = 0, *r = 0;
 
@@ -575,6 +598,8 @@ static void print_string(printer_t *pr, const char *txt)
                        pr->prow = pr->row;
                        pr->pcol = pr->col;
                }
+               if (mi < pr->mcnt && i >= pr->mstart[mi])
+                       attron(color_watch | A_BOLD);
                if (irc_color(txt, &i))
                        continue;
                switch (txt[i]) {
@@ -604,6 +629,10 @@ static void print_string(printer_t *pr, const char *txt)
                                pr->col += 1;
                                break;
                }
+               if (mi < pr->mcnt && i >= pr->mstop[mi]) {
+                       attroff(color_watch | A_BOLD);
+                       mi++;
+               }
        }
        if (i <= pr->pick) {
                pr->prow = pr->row;
@@ -655,10 +684,18 @@ static void print_message(printer_t *pr, message_t *msg)
                print_format(pr, "*** ");
        }
 
+       /* Set matches */
+       if (watch)
+               pr->mcnt = match_all(msg->text, &regex, pr->mstart, pr->mstop);
+
        /* Print message */
        pr->indent = pr->col;
        print_string(pr, msg->text);
        pr->row++;
+
+       /* Clear matches */
+       if (watch)
+               pr->mcnt = 0;
 }
 
 static void print_status(printer_t *pr)
@@ -740,6 +777,11 @@ static int send_command(const char *text)
                        if (match(abbrevs[i], arg))
                                abbrev = i;
        }
+       else if (prefix(text, "/watch", &arg)) {
+               strset(&watch, arg);
+               if (watch)
+                       regcomp(&regex, watch, RE_MFLAGS);
+       }
        else if (match(text, "/open")) {
                win = find_window(focus->dest->name);
                if (!win || win == focus) {
@@ -1063,6 +1105,10 @@ static void process(wint_t chr)
 /* View init */
 void view_init(void)
 {
+       /* General setup */
+       if (watch)
+               regcomp(&regex, watch, RE_MFLAGS);
+
        /* System windows */
        sys_srv.name = strcopy("system");
        sys_srv.protocol = -1;
@@ -1107,6 +1153,7 @@ void view_init(void)
 
        color_title = color(COLOR_WHITE, COLOR_BLUE);
        color_date  = color(COLOR_BROWN, -1);
+       color_watch = color(COLOR_RED,   -1);
        color_error = color(COLOR_RED,   -1);
 
        /* Create signal FD */
@@ -1143,6 +1190,8 @@ void view_config(const char *group, const char *name, const char *key, const cha
                        abbrev = get_map(value, abbrevs);
                else if (match(key, "defocus"))
                        defocus = get_number(value);
+               else if (match(key, "watch"))
+                       watch = get_string(value);
        }
 
        if (match(group, "window")) {