diff options
author | delthas <delthas@dille.cc> | 2022-08-19 22:39:17 +0200 |
---|---|---|
committer | delthas <delthas@dille.cc> | 2022-08-19 22:39:17 +0200 |
commit | c3a6d9c0abf6ac9ffbc130a79a1661997877ba9e (patch) | |
tree | 965fe3f61b35e84e96d4c01fc7d15d1513775cca | |
parent | Show the message date instead of its time on date change (diff) |
Highlight only on word boundaries
Preivously, we highlighted the user for any message that contained the
nick, even in the middle of a word.
Now, we only send a highlight if the user was mentioned in a single
word, by detecting word boundaries around the mention of the user.
For example, considering user "foo":
Previously, we would highlight on "foo" and "foolish".
Now, we highlight only on "foo".
This was inspired/copied from soju's handling [1].
[1]: https://git.sr.ht/~emersion/soju/tree/5e56cc30c538eb88c21cce732e2e357e80098164/item/irc.go#L473
Fixes: https://todo.sr.ht/~taiite/senpai/97
-rw-r--r-- | app.go | 31 |
1 files changed, 29 insertions, 2 deletions
@@ -11,6 +11,7 @@ import ( "sync" "time" "unicode" + "unicode/utf8" "golang.org/x/net/proxy" @@ -1006,14 +1007,40 @@ func isBlackListed(command string) bool { return false } +func isWordBoundary(r rune) bool { + switch r { + case '-', '_', '|': // inspired from weechat.look.highlight_regex + return false + default: + return !unicode.IsLetter(r) && !unicode.IsNumber(r) + } +} + +func isHighlight(text, nick string) bool { + for { + i := strings.Index(text, nick) + if i < 0 { + return false + } + + left, _ := utf8.DecodeLastRuneInString(text[:i]) + right, _ := utf8.DecodeRuneInString(text[i+len(nick):]) + if isWordBoundary(left) && isWordBoundary(right) { + return true + } + + text = text[i+len(nick):] + } +} + // isHighlight reports whether the given message content is a highlight. func (app *App) isHighlight(s *irc.Session, content string) bool { contentCf := s.Casemap(content) if app.highlights == nil { - return strings.Contains(contentCf, s.NickCf()) + return isHighlight(contentCf, s.NickCf()) } for _, h := range app.highlights { - if strings.Contains(contentCf, s.Casemap(h)) { + if isHighlight(contentCf, s.Casemap(h)) { return true } } |