mirror of
https://github.com/Rudi9719/kbtui.git
synced 2026-03-22 05:17:27 +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:
21
colors.go
21
colors.go
@ -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
|
||||||
|
|||||||
2
main.go
2
main.go
@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user