about summary refs log tree commit diff stats
path: root/src/ui
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2016-02-13 22:46:25 +0000
committerJames Booth <boothj5@gmail.com>2016-02-13 22:46:25 +0000
commit1a3dc91e119f029807a0e7156a869ce2ffcff0ed (patch)
treee195529f648c8c095b24564cf3ebee2623238e5b /src/ui
parent2f82f50a352ebdbe34c1712156766e0476e19e6c (diff)
downloadprofani-tty-1a3dc91e119f029807a0e7156a869ce2ffcff0ed.tar.gz
Highlight room trigger terms
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/console.c1
-rw-r--r--src/ui/mucwin.c71
-rw-r--r--src/ui/ui.h2
3 files changed, 69 insertions, 5 deletions
diff --git a/src/ui/console.c b/src/ui/console.c
index 00957aae..8164ccf1 100644
--- a/src/ui/console.c
+++ b/src/ui/console.c
@@ -2217,6 +2217,7 @@ cons_theme_properties(void)
     _cons_theme_prop(THEME_ROOMMENTION, "roommention");
     _cons_theme_prop(THEME_ROOMMENTION_TERM, "roommention.term");
     _cons_theme_prop(THEME_ROOMTRIGGER, "roomtrigger");
+    _cons_theme_prop(THEME_ROOMTRIGGER_TERM, "roomtrigger.term");
 
     _cons_theme_prop(THEME_ROSTER_HEADER, "roster.header");
     _cons_theme_prop(THEME_ROSTER_CHAT, "roster.chat");
diff --git a/src/ui/mucwin.c b/src/ui/mucwin.c
index 3d7263f3..fef02539 100644
--- a/src/ui/mucwin.c
+++ b/src/ui/mucwin.c
@@ -385,9 +385,71 @@ _mucwin_print_mention(ProfWin *window, const char *const message, const char *co
     g_free(message_lower);
 }
 
+static void
+_mucwin_print_triggers(ProfWin *window, const char *const message, GList *triggers)
+{
+    char *message_lower = g_utf8_strdown(message, -1);
+
+    // find earliest trigger in message
+    int first_trigger_pos = -1;
+    int first_trigger_len = -1;
+    GList *curr = triggers;
+    while (curr) {
+        char *trigger_lower = g_utf8_strdown(curr->data, -1);
+        char *trigger_ptr = g_strstr_len(message_lower, -1, trigger_lower);
+
+        // not found, try next
+        if (trigger_ptr == NULL) {
+            curr = g_list_next(curr);
+            continue;
+        }
+
+        // found, repace vars if earlier than previous
+        int trigger_pos = trigger_ptr - message_lower;
+        if (first_trigger_pos == -1 || trigger_pos < first_trigger_pos) {
+            first_trigger_pos = trigger_pos;
+            first_trigger_len = strlen(trigger_lower);
+        }
+
+        g_free(trigger_lower);
+        curr = g_list_next(curr);
+    }
+
+    g_free(message_lower);
+
+    // no triggers found
+    if (first_trigger_pos == -1) {
+        win_print(window, '-', 0, NULL, NO_DATE | NO_ME, THEME_ROOMTRIGGER, "", message);
+    } else {
+        if (first_trigger_pos > 0) {
+            char message_section[strlen(message) + 1];
+            int i = 0;
+            while (i < first_trigger_pos) {
+                message_section[i] = message[i];
+                i++;
+            }
+            message_section[i] = '\0';
+            win_print(window, '-', 0, NULL, NO_DATE | NO_ME | NO_EOL, THEME_ROOMTRIGGER, "", message_section);
+        }
+        char trigger_section[first_trigger_len + 1];
+        int i = 0;
+        while (i < first_trigger_len) {
+            trigger_section[i] = message[first_trigger_pos + i];
+            i++;
+        }
+        trigger_section[i] = '\0';
+
+        if (first_trigger_pos + first_trigger_len < strlen(message)) {
+            win_print(window, '-', 0, NULL, NO_DATE | NO_ME | NO_EOL, THEME_ROOMTRIGGER_TERM, "", trigger_section);
+            _mucwin_print_triggers(window, &message[first_trigger_pos + first_trigger_len], triggers);
+        } else {
+            win_print(window, '-', 0, NULL, NO_DATE | NO_ME, THEME_ROOMTRIGGER_TERM, "", trigger_section);
+        }
+    }
+}
+
 void
-mucwin_message(ProfMucWin *mucwin, const char *const nick, const char *const message, gboolean mention,
-    gboolean trigger_found)
+mucwin_message(ProfMucWin *mucwin, const char *const nick, const char *const message, gboolean mention, GList *triggers)
 {
     assert(mucwin != NULL);
 
@@ -398,8 +460,9 @@ mucwin_message(ProfMucWin *mucwin, const char *const nick, const char *const mes
         if (mention) {
             win_print(window, '-', 0, NULL, NO_ME | NO_EOL, THEME_ROOMMENTION, nick, "");
             _mucwin_print_mention(window, message, my_nick);
-        } else if (trigger_found) {
-            win_print(window, '-', 0, NULL, NO_ME, THEME_ROOMTRIGGER, nick, message);
+        } else if (triggers) {
+            win_print(window, '-', 0, NULL, NO_ME | NO_EOL, THEME_ROOMTRIGGER, nick, "");
+            _mucwin_print_triggers(window, message, triggers);
         } else {
             win_print(window, '-', 0, NULL, NO_ME, THEME_TEXT_THEM, nick, message);
         }
diff --git a/src/ui/ui.h b/src/ui/ui.h
index 70b01df3..8376cb90 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -158,7 +158,7 @@ void mucwin_occupant_role_and_affiliation_change(ProfMucWin *mucwin, const char
     const char *const role, const char *const affiliation, const char *const actor, const char *const reason);
 void mucwin_roster(ProfMucWin *mucwin, GList *occupants, const char *const presence);
 void mucwin_history(ProfMucWin *mucwin, const char *const nick, GDateTime *timestamp, const char *const message);
-void mucwin_message(ProfMucWin *mucwin, const char *const nick, const char *const message, gboolean mention, gboolean trigger_found);
+void mucwin_message(ProfMucWin *mucwin, const char *const nick, const char *const message, gboolean mention, GList *triggers);
 void mucwin_subject(ProfMucWin *mucwin, const char *const nick, const char *const subject);
 void mucwin_requires_config(ProfMucWin *mucwin);
 void mucwin_info(ProfMucWin *mucwin);