1
0
mirror of https://github.com/Rudi9719/kbtui.git synced 2026-03-22 06:27:24 +00:00

Fix bug in colorRegex() that was highighting usernames in the middle of strings when a user wasn't actually being mentioned

This commit is contained in:
Sam
2019-10-24 22:20:25 -04:00
committed by Sam Hofius
parent 1a75ac8a49
commit f09f2969d9
2 changed files with 12 additions and 11 deletions

View File

@ -195,22 +195,23 @@ func (t StyledString) replaceString(match string, value string) StyledString {
t.message = strings.Replace(t.message, match, value, -1) t.message = strings.Replace(t.message, match, value, -1)
return t return t
} }
func (t StyledString) replaceRegex(match string, value StyledString) StyledString {
var re = regexp.MustCompile("(" + match + ")")
t.message = re.ReplaceAllString(t.message, value.stringFollowedByStyle(t.style))
return t
}
// Overrides current formatting // Overrides current formatting
func (t StyledString) colorRegex(match string, style Style) StyledString { func (t StyledString) colorRegex(match string, style Style) StyledString {
re := regexp.MustCompile("(" + match + ")") re := regexp.MustCompile("(" + match + ")")
subStrings := re.FindAllString(t.message, -1) locations := re.FindAllStringIndex(t.message, -1)
for _, element := range subStrings { var newMessage string
cleanSubstring := style.stylize(removeFormatting(element)) var prevIndex int
t.message = strings.Replace(t.message, element, cleanSubstring.stringFollowedByStyle(t.style), -1) for _, loc := range locations {
cleanSubstring := style.stylize(removeFormatting(string(t.message[loc[0]:loc[1]])))
newMessage += t.message[prevIndex:loc[0]]
newMessage += cleanSubstring.stringFollowedByStyle(t.style)
prevIndex = loc[1]
} }
// Append any string after the final match
newMessage += t.message[prevIndex:len(t.message)]
t.message = newMessage
return t return t
// Old versionreturn t.replaceRegex(match, style.stylize(`$1`))
} }
// Appends the other stylize at the end, but retains same style // Appends the other stylize at the end, but retains same style

View File

@ -407,7 +407,7 @@ func formatChannel(ch keybase.Channel) StyledString {
} }
func colorReplaceMentionMe(msg StyledString) StyledString { func colorReplaceMentionMe(msg StyledString) StyledString {
return msg.colorRegex("(@?"+k.Username+")", mentionColor) return msg.colorRegex(`(@?\b`+k.Username+`\b)`, mentionColor)
} }
func colorUsername(username string) StyledString { func colorUsername(username string) StyledString {
var color = messageSenderDefaultColor var color = messageSenderDefaultColor